Site logo

git Cheat sheet

Git Configuration
Set Your Name
git config --global user.name "Your Name"
Set Your Email
git config --global user.email "your.email@example.com"
Check Configuration
git config --list
Basic Commands
Initialize a Repository
git init
Clone a Repository
git clone <repository_url>
Check Repository Status
git status
Add Changes to Staging Area
git add <file(s)>
Commit Changes
git commit -m "Your commit message"
Branching
Create a New Branch
git branch <branch_name>
Switch to a Branch
git checkout <branch_name>
Switch to a Branch Came From a Remote Repo
git checkout --track origin/<branch_name>
Create and Switch to a New Branch
git checkout -b <new_branch_name>
List All Local Branches
git branch
List All Remote Branches
git branch -r
List All Local and Remote Branches
git branch -a
Merge Changes from Another Branch
git merge <branch_name>
Delete a local Branch
git branch -d <branch_name>
Delete a local Branch with force
git branch -D <branch_name>
Delete a Remote Branch with force
git push <remote_name> --delete <branch_name>
Remote Repositories
Add a Remote Repository
git remote add <remote_name> <repository_url>
Set Remote Repository URL
git remote set-url origin <repository_url>
List Remote Repositories
git remote -v
Fetch Changes from a Remote Repository
git fetch <remote_name>
Pull Changes from a Remote Repository
git pull <remote_name> <branch_name>
Push Changes to a Remote Repository
git push <remote_name> <branch_name>
Stage & Snapshot
Show modified files in working directory, staged for your next commit
git status
add a file as it looks now to your next commit (stage)
git add [file]
add all files as it looks now to your next commit (stage)
git add .
diff of what is changed but not staged
git diff
diff of what is staged but not yet commited
git diff --staged
Commit
commit your staged content as a new commit snapshot
git commit -m "commit message"
run git add and commit your staged content as a new commit snapshot
git commit -a -m "commit message"
Amend the Last Commit (Change Message or Add Staged Changes)
git commit --amend
Amend the Last Commit (Change Message or Add Staged Changes)
git commit --amend -m "new commit message"
Amend the Last Commit (Add Staged Changes without Changing Message)
git commit --amend --no-edit
Undoing Changes
Unstage a file while retaining the changes in working directory
git reset <file>
Discard Changes in Working Directory
git checkout -- <file(s)>
Unstage Changes
git reset HEAD <file(s)>
Unstage Changes, Keep Them in Working Directory, and Move HEAD to a Specific Commit
git reset --soft HEAD^
Discard Changes, Unstage, and Move HEAD to a Specific Commit
git reset --hard HEAD^
Merge
Merge Changes from Another Local Branch
git merge <source_branch>
Merge Changes from Another Local Branch(enforce creating a merge commit)
git merge --no-ff <source_branch>
Pull
Pull Changes from a Remote Repository
git pull <remote_name> <branch_name>
Pull Changes from a Remote Repository without prompting
git pull <remote_name> <branch_name> --no-edit
Pull and rewrite history from a remote
git pull --rebase <remote_name> <branch_name>
Rebase
Rebase Changes onto Another Branch
git rebase <base_branch>
Switch to Merge
git config --global pull.rebase false
Switch to Rebase
git config --global pull.rebase true
Squash multiple commits into one for a cleaner git log. change the word pick to either 'f' or 's'
git rebase -i $commit_ref
Rebase your changes on top of the remote master
git pull --rebase upstream <base_branch>
Interactive Rebase
git rebase -i <base_branch>
Logging
View Commit History
git log
View Commit History (Compact)
git log --oneline
View Commit History (decorate)
git log --decorate
View Commit History (graph lines)
git log --graph
View Changes in a Commit
git show <commit_hash>
Search in commit messages
git log --grep="fixes things"
search in code
git log -S"window.alert"
search in code (regex)
git log -G"foo.*"
Stash
Stash Changes in the Working Directory
git stash
Stash Changes with a Descriptive Message
git stash save "Your stash message"
List all Stashes
git stash list
Apply the Latest Stash to the Working Directory
git stash apply
Apply a Specific Stash to the Working Directory
git stash apply stash@{n}
Apply the Latest Stash and Remove it from the Stash List
git stash pop
Remove a Specific Stash from the Stash List
git stash drop stash@{n}
Remove all Stashes
git stash clear
Tracking path changes
Delete the file from project and stage the removal for commit
git rm [file]
Change an existing file path and stage the move
git mv [existing-path] [new-path]
Cherry Pick
Apply Changes from a Specific Commit to the Current Branch
git cherry-pick <commit>
Apply Changes from a Specific Commit (Do Not Commit Immediately)
git cherry-pick -n <commit>
Apply Changes from a Specific Commit and Add a Note with Original Commit ID
git cherry-pick -x <commit>
Edit the Commit Message During Cherry Pick
git cherry-pick --edit <commit>
Abort the Cherry Pick Operation
git cherry-pick --abort
Submodules
Initialize Submodules, import .gitmodules
git submodule init
Add a Submodule
git submodule add <repository_url>
Update Submodules
git submodule update
Sync Submodule URL
git submodule sync
Run a Command in Each Submodule
git submodule foreach '<command>'