1. Checkout a single file from another branch
Have you ever destroyed a file and just wished you could have a fresh start? Or needed the...
For further actions, you may consider blocking this person and/or reporting abuse
Nice post Jacob!
I also like to use
git reset --hard HEAD
.My fav git aliases that are always in my
~/.gitconfig
:Thanks for sharing!
Love this, incorporating some of them into my git tricks!
Building on your alias list command, I have an alias called alias:
alias.alias !alias(){ git config --get-regexp "^alias.${1}"; }; alias
This'll list all aliases matching the provided prefix, or just all aliases with no arguments:
git commit -v --amend
Do not do this if you've already pushed that commit. If a push ever requires
--force
, you need to think long and hard about whether or not it's something you should do.One thing that I use a lot on Windows is Posh-git. It places branch, commit and index status in the command prompt
A few weeks ago, I used #9 to figure out "how long has this feature been in our code before someone made a complaint about it."
Specifically, we do build-automation. Someone requested we include tool in the resultant builds. I'd looked at the system the person was complaining didn't have the component, only to find that it was present ...just that the associated run-time service wasn't activated. Ultimately, found that the tool had been in the build for nearly four years in this "installed but not automatically started" state. Was able to tell the requestor, "good news: the tool has been in place four nearly four years; I just had to update the automation to ensure that it actually starts on boot. The only remediation you need for old systems is to enable the service rather than having to figure out how to deploy and install it."
...All this because the maker of the tool had a botch in their installer that caused the enablement to not actually happen. :p
Stories like this are the reason I think it makes sense to learn git at a deeper level than branch, add, commit, repeat.
True, but that could be said for many (most?) tools. That said, a lot of people never have reason to use those advanced features (and, on the rare occasion that they do, if their Google-Fu is half-decent, they'll find what they need that way).
Two other big ones come to mind that I often see underutilized, but find indispensable on a regular basis:
Interactively stage individual parts of files for a commit
Sometimes, you end up fixing bugs in nearby code that you didn't notice until you were testing something else. Or maybe you end up needing to split out a big bunch of changes into separate logical steps. Or perhaps you just forgot to commit something before starting on the next thing.
In any case, you can use:
git add -i
It will bring up a menu-based interface to step through individual hunks of the current set of changes to the working tree and let you select only the parts you want to include in the commit.
Rearrange (or completely rewrite) the history of a branch
git rebase
also has an interactive mode (also enabled with the-i
switch). You can use this to edit history back to a certain commit without moving the branch you're on by running:git rebase -i <commit>
The commit you specify should be the last one before the commit you want to start editing things at. Just like a normal rebase, you can specify a tag or a branch name instead of a commit here (or use a commit-like form like
HEAD~8
). This will open an editor with a list of instructions to git for how to perform the rebase (it defaults to what it would do if you were doing a regular rebase), as well as some comments below that explaining what each instruction means.Things you can do with this include:
pick
at the beginning of the lines todrop
.break
to the list where you want to put the new commit, the rebase will stop there and drop to a shell, where you can make your code changes and commit them just like normal, then rungit rebase --continue
to resume the rebase. You can also use this to merge branches or cherry pick commits into arbitrary locations in your history.pick
withedit
, the commit will be applied, but the rebase will stop immediately afterwards so you can usegit commit --amend
to update the commit. This is essentially a shorter form of adding abreak
after the commit.pick
withsquash
.fixup
instead ofsquash
and it will just use the commit message from the first commit as-is instead of prompting you to enter a new one for the single commit it produces.git reset HEAD~
when the rebase stops to let you amend that commit. This will remove that commit from the history, but leave all the changes it made to the working tree in place, and you can then split them out into new commits however you want (possibly usinggit add -i
to simplify the process).exec
wherever you want in the sequence of commands. The rebase process will pass the rest of that line to the shell to be run as a command (essentially, it callssh -c
followed by the rest of the line) at that point during the rebase, and will stop the rebase if the command fails (just like if a merge failed) so that you can investigate what happened and either cancel the rebase or continue it as you see fit.Nice list! I've been using git consistently for five or six years now and still learn new stuff about it all the time.
Related to #5, this is probably my single most used git alias and quite possibly the most-used CLI command on my system:
I have it zsh aliased to 'ggd' so I can quickly check the history of my current branch and whether certain other branches are in the history (basically seeing at what point I last rebased on master, or if I need to again).
Emergency servival kit: ohshitgit.com/
That's awesome.
Always an option:
Or just:
To avoid moving out of the directory, overwriting it, or losing changes
For #8, you can simply type
git alias
to list all the configured aliases for you.
Hmm. Are you sure that's not a plugin or something? It doesn't seem to work for me.
EDIT: Found it! github.com/tj/git-extras/blob/mast...
Oh yes! This plugin has integrated so seamlessly with my flow that I almost forgot I had it installed.
git reflog
is an absolute saviour for me too!I'm disappointed π to not see here one of my favorite git commands
git worktree add <path-to> <branch-name>
ref π git-scm.com/docs/git-worktree
This is great!
What is the purpose of -- in the first command? Seems an unnecessary argument.
It is kind of superfluous. It is just a more formal way to indicate that you are specifying the file name. I included it here in case someone took the example and made an alias out of it.
Imagine you had a file called
master
and you wanted to check that file out,git checkout master
would probably do something different than you wanted, butgit checkout -- master
would check that file out from the current branch.It signifies the end of command options. It is useful with transferring control to sub commands. In D you can provide compiler options then pass arguments to the compiled program.
D will compile foo then call
Thank you!
great article :D
Point 7 (about date ranges) was pretty much unknown for me. Thanks for including it.
P.S. all the tips are really very helpful. Thanks again.
Super glad it's helpful!