When I was getting started with git, I developed a “greatest hits” of commands and ideas that helped me understand what was going on. Here are a few of them — more to be added.
- Whatever else you do, remember that commits are forever! If you make a mistake, don’t think of it as ‘deleting’ the commit. You’re just pushing a new commit with new code that cancels out the old commit’s work. The more comfortable you are with this though, the better — for reverting, cherry picking, etc.
- Change the name of a commit you just made:
git commit --amend - Cherry pick a set of commits:
git cherry-pick A^..Bwhere A is the oldest commit you want and B is the newest. This is inclusive! If you want A through B excluding A and B, just useA..B. - Undo your last commit (only if you haven’t pushed!!):
git reset HEAD~ - Undo a commit you already pushed:
git revert commit_name- Never use
rebaseon a commit that’s already been pushed especially in a shared repository! Rebase rewrites history, and that’s not clean, sustainable, or kind to your teammates 😉
- Never use
- To bring your feature branch up-to-date with a base branch (e.g., develop or master):
git checkout developgit pullgit checkout feature/your_feature_branchgit merge --no-ff develop
- To remove untracked files, use
git clean -f. Add the-dtag if you want to remove untracked directories as well. - Don’t revert
mergecommits! This is a fast way to mess up your history. Reverting a bunch of individual commits can seem like a pain, but it takes less time than trying to undo amergereversion. - This is also a great resource for undoing (almost) everything in Git.
More later!
