Here's a list of commands that I've had the need to use for the last month or so while working. Most of them are for more specific scenarios, but some are just very convenient options that I didn't know about.
For an even better list of scenarios and very helpful tips, I'd recommend Seth Robertson's pages on the topic1.
Reverse cherry-picking
As we approached release, we made release branches for our products, but realized too late that some features were not supposed to be included. So we had to reverse cherry-pick out the feature and it's commits. Consulting the internet we found that git revert COMMIT_SHA
is the command we were looking for. Since the commits were spread out in we could not revert a range of commits, but were you as lucky as that then the workflow is even easier. Reverting a range of commits is possible with the ..
range selector2.
git revert -n OLDER_COMMIT^..NEWER_COMMIT
That^
right there reads as "parent of", so that would make the command: from parent of older commit to newer commit.
Fixing previous commit
I of course would never commit things that shouldn't be committed, or make any mistakes, but I found that writing "fix" or even worse "asdf2" really gets tedious after the nth time, and looks bad on your unofficial coding resume. Luckily everybody else makes mistakes too, so git of course has a --fixup
option for commits. git commit --fixup COMMIT_SHA
appends a "fixup!" before the message of the commit you passed. To fully utilize this feature, you can then use git rebase --interactive --autosquash
and voilà. Just like sweeping all the dirt under the rug, out of sight, out of mind.
If you combine parts of the command above with this one, you can even write git commit --fixup HEAD
and that's even easier, since you don't have to search for the commit hash. And even more cool is using :/
with which you can make it based on a commit message3. Say for instance that you have a couple of commits:
054808a Hello there
7434f0b These are
c538016 Some commits
And you need to fix the first commit. Instead of copying the hash, or the parent's parent of HEAD, you could just use the :/
notation.
git commit --fixup :/commits
This then searches for the first commit that has the string "commits" in the message. Isn't that just wonderful?
Discard all work since previous push
So was fixing something (trying to be quick and dirty, but those words and git don't quite match most of the time) and was just about to push the fix to the remote repository, when it occurred that I didn't have the latest commits pulled. So of course it automatically created an ugly merge that I otherwise might not have cared about, but this was about my professional pride. So I scoured the internet and found that you can pass @{u}
(Which refers to the upstream branch that the current branch is tracking. So basically a shorthand for a longer string, more here) as a reference for the last time you pushed to the origin. So a git reset --hard @{u}
later I had a clean slate and could pull the new commits, apply my one line fix, and push to the server. No one saw anything I swear.
Gitconfig
Ok, ok, I should have known better, of course there's a .gitconfig file that you can make. Useful things include:
# so you don't have to write --autosquash every time you want to rebase your fix
[rebase]
autosquash = true
# saves you writing the branch name when you're pushing to the remote
[push]
default = current
But Bob's your uncle so go wild and configure it just the way you like it.
So there you have it. Git is equally scary and amazing, and I don't think I'll ever remember any of these when I actually need them, but now I know where I put the commands, and maybe you, dear search engine user had some use for this. As I stated in the beginning, Seth Robertson1 seems to me like a Git God (or was it Git Gud) so definitely take a look at what he writes on the topic.
Top comments (0)