These are a bunch of Git tricks that I've found useful. I'm writing them down so I remember them and so that they might be helpful for anyone else.
Creating a branch based off of another one
git checkout base-branch -b new-branch
This used to be my workflow:
git checkout base-branch
git checkout -b new-branch
OR
git branch new-branch; git checkout new-branch
The first command allows me to combine it all into one.
Removing a file from the previous commit
git reset --soft HEAD~1
git reset file-to-exclude.cpp
git commit -c ORIG_HEAD
This will exclude the file and open an editor with the original commit message that was there before the reset.
Removing a file from a commit in the middle of a bunch of other commits
Figure out the commit that the file was added in:
D -> C -> B -> A
Let's say we're looking to remove a file from C
. It's the 3rd commit from A
(including A
) so we'll use HEAD~3
.
git rebase -i HEAD~3
Something like this will pop up:
pick c43c93b Commit A
pick 76c25a0 Commit B
pick 9d8dfbe Commit C
Replace pick
with edit
(or e
) for c43c93b
. Save and close the file. Repeat the step from above. Then run git rebase --continue
.
Cherry picking a range of commits
The non-inclusive way of cherry-picking commits is:
git cherry-pick c43c93b..9d8dfbe
However, this doesn't include c43c93b
.
To inclusively cherry-pick:
git cherry-pick c43c93b^..9d8dfbe
This will include c43c93b
as well. However, this still doesn't work on Windows (cmd.exe
) because ^
is a reserved symbol.
To escape ^
we need to use ^^
. So, on Windows use:
git cherry-pick c43c93b^^..9d8dfbe