Git useful commands
2 min readSep 8, 2019
Git is one of the most important development tool. I’d like to share list with useful commands:
How to delete a Git branch both locally and remotely?
git branch -d <branch_name>
git push <remote_name> --delete <branch_name>
Review / edit the last commit
git commit --amend
Set up git to pull and push all branches
git push --all <remote>
What’s the simplest way to get a list of conflicted files?
git diff --name-only --diff-filter=U
How to undo last commit(s) in Git?
git reset --hard HEAD~1
How can I undo git reset — hard HEAD~1?
git reset --hard <sha1 of desired commit>
How can I merge two commits into one?
git reset --soft "HEAD^"
git commit --amend
Cherry-Picking specific commits from another branch
# Initial statedd2e86 - 946992 - 9143a9 - a6fd86 - 5a6057 [master]
\
76cada - 62ecb4 - b886a0 [feature]git checkout master
git cherry-pick 62ecb4# Outputdd2e86 - 946992 - 9143a9 - a6fd86 - 5a6057 - 62ecb4 [master]
\
76cada - 62ecb4 - b886a0 [feature]
How to checkout master to all submodules (sub repositories) of project ?
# For git 1.8.2 or above the option --remote was added to support updating to latest tips of remote branches:
git submodule update --recursive --remote
# For older, git 1.6.1 or above you can use something similar to (modified to suit):
git submodule foreach git pull origin master
Is it possible to completely empty a remote Git repository?
touch README.md
git init
git commit -m "I am removing all previous commits"
git push <remote_name> <branch_name> -f
I want to share list with useful training sessions.
- Introduction to Git with Scott Chacon of GitHub
- http://learngitbranching.js.org
- Got 15 minutes and want to learn Git?
- Git Cheatsheet
- Escape the git mess
- Are git forks actually git clones?
- Git Tips & Tricks
- Processing JIRA Software issues with Smart Commit messages
- Mozilla: Committing rules and responsibilities
- Trunk base development
- GOTO 2017 • Feature Branches and Toggles in a Post-GitHub World • Sam Newman