I’ve been thinking about some of the common git tasks I’ve been struggling with and created a list to help me remember them:
Undo staging of a file (undo git add)
- git reset filename
Add all files in a directory
- git add directory
Undo the last commit (not pushed)
- git reset –hard HEAD~
Undo the last commit (was pushed)
- git revert HEAD
Undo a commit that’s not the most recent one
- git revert sha1
Change branches without commiting files
- git stash
Undo changes made to one file
- git checkout HEAD~ filename
Replace a particular file with one in a different branch
- git checkout branch filename
Look at a remote branch (without detached state)
- git checkout -b localname remote/branchname
When there’s a merge conflict, use mine/theirs to resolve the conflict
- git checkout –ours filename
- git checkout –theirs filename
In addition, here are some resources that intermediate git users will find useful:
- Git Immersion is a great tutorial that walks you through some of the common tasks
- Githug is a great command-line game to practice git commands
I’d also recommend creating either shell aliases or git aliases for the git commands you use frequently. git autocomplete is a life saver for autocompleting branch names and commands!