🍴 Useful git commands for everyday use
❤️ Support my apps ❤️
- Push Hero - pure Swift native macOS application to test push notifications
- PastePal - Pasteboard, note and shortcut manager
- Quick Check - smart todo manager
- Alias - App and file shortcut manager
- My other apps
❤️❤️😇😍🤘❤️❤️
Check the status of working directory and staging area
git status
Show changes between HEAD and working directory
git diff
Show the list of commits in one line format
git log --oneline
Show commits that make add or remove a certain string
git log -S 'LoginViewController'
Search commits that contain a log message
git log — all — grep=’day of week’
Checkout a tag
git checkout TAG
git checkout -b BRANCH TAG
Checkout a branch
git checkout BRANCH
Use -m if there is merge conflict
git checkout -m BRANCH
Checkout a commit
git checkout COMMIT_HASH
Checkout into a new branch
git checkout -b BRANCH HEAD~4
git checkout -b BRANCH COMMIT_HASH
Checkout a file in a commit
git checkout COMMIT -- FILE_PATH
Checkout file at a specific commit and open in Visual Studio Code
git show BRANCH_TAG_COMMIT:FILE_PATH | code -
Make and apply diff file between 2 branches
git diff BRANCH ANOTHER_BRANCH > file.diff
git apply file.diff
Find bug in commit history in a binary search tree style:
git bisect start
git bisect good
git bisect bad
List all tags
git tag
Tag a commit
git tag -a TAG -m "TAG MESSAGE"
Delete remote tags
git push --delete REMOTE TAG
git push origin :TAG
Push tag to remote
git push REMOTE TAG
Rename tag
git tag NEW_TAG OLD_TAG
git tag -d OLD_TAG
Move tag from one commit to another commit
git push origin :TAG
git tag -fa TAG
git push REMOTE --tags
List all remote
git remote
Rename remote
git remote rename OLD_REMOTE NEW_REMOTE
Remove stale remote tracking branches
git remote prune REMOTE
List all branches
git branch
Create the branch on your local machine and switch in this branch
git checkout -b BRANCH
Create branch from commit
git branch BRANCH COMMIT_HASH
Push the branch to remote
git push REMOTE BRANCH
Rename other branch
git branch -m OLD_BRANCH NEW_BRANCH
Rename current branch
git branch -m NEW_BRANCH
Rename remote branch
git branch -m OLD_BRANCH NEW_BRANCH # Rename branch locally
git push origin :OLD_BRANCH # Delete the old branch
git push --set-upstream REMOTE NEW_BRANCH # Push the new branch, set local branch to track the new remote
Delete a branch locally and remote
git branch -D BRANCH
git push REMOTE :BRANCH
Delete all local branches but master
git branch | grep -v "master" | xargs git branch -D
Undo last commit
git reset --hard HEAD~1
Squash last n commits into one commit
git rebase -i HEAD~5
git reset --soft HEAD~5
git add .
git commit -m "Update"
git push -f origin master
Move last commits into new branch:
git branch newbranch
git reset --hard HEAD~3 # Go back 3 commits. You *will* lose uncommitted work.*1
git checkout newbranch
Make changes to older commit
git rebase -i HEAD^^^
// change from pick to edit, then :wq
git add .
git rebase --continue
Get their changes during git merge
git pull -X theirs git checkout --theirs FILE_PATH git checkout --theirs . git add .
git checkout BRANCH_A git merge -X theirs BRANCH_B
Merge commits from master into feature branch
git checkout BRANCH
git merge --no-ff BASE_BRANCH
After commit, in interactive rebase, specify fixup
instead of pick
git commit --fixup HEAD~1
git rebase HEAD~2 -i --autosquash
git commit --fixup fb2f677
git rebase -i --autosquash ac5db87
Add some commits to the top of the current branch:
git cherry-pick COMMIT_HASH_A COMMIT_HASH_B
Revert the previous commit
git revert HEAD
Revert the changes from previous 3 commits without making commit
git revert --no-commit HEAD~3..
Amend previous commit
git commit --amend
git commit --amend --no-edit
git commit --amend -m "COMMIT_MESSAGE"
Changing git commit message after push
git commit --amend -m "COMMIT_MESSAGE"
git push --force REMOTE BRANCH
Show reflog
git reflog
Get commit
git reset --hard COMMIT_HASH
git cherry-pick COMMIT_HASH
Rebase the current branch onto another branch
git rebase BASE_BRANCH
Continue rebase
git rebase --continue
Abort rebase
git rebase --abort
Get their changes during git rebase
git checkout --ours FILE_PATH
git add FILE_PATH
Remove untracked files
git clean
Remove file from index
git reset FILE_PATH
Reset the index to match the most recent commit
git reset
Reset the index and the working directory to match the most recent commit
git reset --hard
Un-track files that have just been declared in .gitignore
git rm -r --cached .
git add .
git commit -am "COMMIT_MESSAGE"
Save a change to stash
git stash save "STASH_NAME"
git stash
List all stashes
git stash list
Apply a stash
git stash pop
git stash apply
git stash apply stash@{2}
This is just scratching the surface of what git can do, if you want to learn more, here are some links to get started.
- Atlassian Git Tutorial: overview of how to set up a repository (repo) under Git version control
- git-cheat-sheet: Git cheat sheet saves you from learning all the commands by heart.
- Learn Enough Git to Be Dangerous
- Git Workflows for Pros: A Good Git Guide
- Git from the inside out: The essay focuses on the graph structure that underpins Git
- git-game: terminal game to test git skills
- Introduction to Git — talk by Scott Chacon
- Git Tutorial — Git Fu With The Command Line
- Git Immersion: The surest path to mastering Git is to immerse oneself in its utilities and operations, to experience it first-hand
- git-flight-rules Flight rules for git
- gitflow Git extensions to provide high-level repository operations for Vincent Driessen’s branching model
- diff-so-fancy Good-lookin’ diffs with diff-highlight and more
- github-cheat-sheet A list of cool features of Git and GitHub
- git tips Most commonly used git tips and tricks
- Little Things I Like to Do with Git