
Essential Git Commands
Git is the part of the software development cycle that is often overlooked.
It is not just a utility tool for saving code, but a powerful system for tracking changes, collaborating with others, and managing the history of your project.
This cheatsheet covers the essential commands that every developer should know.
ā Configuration ā Creating repositories ā Staging and committing ā Branching ā Merging and rebasing ā Remote repositories ā Inspecting history ā Undoing changes
#git #versioncontrol #commands #branching #merge #commit #github #webdev #coding #tips
Git is a version control tool that tracks the changes you make to your code. You can create branches, stage changes, and collaborate with others.
| Command | Description |
|---|---|
git config | Read and write Git settings such as user identity, defaults, and aliases. |
git init | Create a new Git repository in the current directory. |
git clone | Copy a remote repository (or a specific branch) to your local machine. |
git status | Show which files are modified, staged, or untracked. |
git add | Move changes from the working directory to the staging area. |
git commit | Save a snapshot of staged changes to the repository history. |
git branch | Manage branches: list, create, rename, or delete them. |
git checkout | Switch between branches or create a new one. |
git merge | Combine the history of another branch into the current branch. |
git rebase | Replay your commits on top of another branch for a linear history. |
| Command | Description |
|---|---|
git remote | Manage connections to remote repositories. |
git fetch | Download changes from a remote without merging them. |
git pull | Download and merge changes from a remote branch. |
git push | Upload local commits, branches, or tags to a remote. |
git log | Browse the commit history, optionally as a compact graph. |
git show | Inspect the changes introduced by a specific commit. |
git diff | Compare changes between the working directory, staging area, or commits. |
git restore | Unstage files or discard uncommitted changes. |
git revert | Undo a commit by creating a new commit that reverses it. |
git reset | Move the branch pointer to a previous commit, optionally discarding changes. |
git stash | Temporarily shelve uncommitted changes and restore them later. |
git tag | Mark a specific commit, typically for a release. |
Configuration
Set your identity and default editor.
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch mainCheck your configuration.
git config --listCreating repositories
Initialize a new Git repository in the current directory.
git initClone an existing repository.
git clone https://github.com/user/repo.gitClone a specific branch.
git clone -b branch-name https://github.com/user/repo.gitStaging and committing
Check the status of your working directory.
git statusStage files for the next commit.
git add filename.txt
git add .Commit staged changes with a message.
git commit -m "Add new feature"Stage and commit in one command.
git commit -am "Fix typo"Branching
List branches.
git branch
git branch -aCreate a new branch.
git branch feature-branchSwitch to a branch.
git checkout feature-branchCreate and switch in one command.
git checkout -b feature-branchRename the current branch.
git branch -m new-nameDelete a branch.
git branch -d feature-branch
git branch -D feature-branchMerging and rebasing
Merge a branch into the current branch.
git merge feature-branchRebase the current branch onto another.
git rebase mainContinue rebasing after resolving conflicts.
git rebase --continueAbort an in-progress rebase.
git rebase --abortRemote repositories
List remote connections.
git remote -vAdd a remote repository.
git remote add origin https://github.com/user/repo.gitFetch changes from a remote.
git fetch originPull changes from a remote branch.
git pull origin mainPush changes to a remote branch.
git push origin mainPush a local branch for the first time.
git push -u origin feature-branchInspecting history
Show commit history.
git log
git log --onelineShow a compact graph of branches.
git log --oneline --graph --allShow changes in a specific commit.
git show commit-hashShow changes between working directory and staging area.
git diffShow changes already staged for commit.
git diff --stagedUndoing changes
Unstage a file.
git restore --staged filename.txtDiscard changes in the working directory.
git restore filename.txtAmend the last commit.
git commit --amend -m "Updated message"Revert a commit by creating a new commit.
git revert commit-hashReset to a previous commit.
git reset --soft HEAD~1
git reset --mixed HEAD~1
git reset --hard HEAD~1Stashing
Temporarily save changes without committing.
git stashList stashes.
git stash listApply the most recent stash.
git stash popApply a stash without removing it.
git stash applyTags
Create a tag for a release.
git tag -a v1.0.0 -m "Version 1.0.0"Push tags to a remote.
git push origin --tagsFull-Stack AI Developer Roadmap
From HTML & CSS to working with AI models, all in one structured roadmap.