Git tricks and loving git resource –source and github pull requests splitting

Ok this has always required a git checkout and then copy some files somewhere and then go back, but with July 2019 Git 2.3, you can now use –source in git restore to get a single file back.

git switch your-dev branch
# make some stupid changes like linting all the files
git restore --source main main.py

The place where this really comes in handy is that if you do a git pull request and realize that you have changed too many files, then split the pull request restore the ones you want and create a separate branch so you end up with multiple branches, these are cleaner to PR.

If you have a linear commits to be cut in two

The mechanism is a little hard if you have lots of commits, but one thing to do is to separate the commits into separate branches. This means you have to get all the commits you want in branch A there and this is the simple case where say the first 3 commits should be in one PR and the second in another.

# figure out where you want to cut the first set of commits off
# so if you want the last three commits to be in the second-branch 
git switch mongo-branch
git branch first-branch
git reset --hard HEAD~3
git rebase --onto main first-branch second-branch
# first-branch is the first N and second is the last 3

If you have commits in random orders

If you have commits in random order, then rebase -I is your friend to do interactive rearranging

# get to the mongo branch
git rebase main -
# now go interactively and rearrange the first branch and second branch commits in the right order
# Now you can do the linear split.

If you forget what files changed in a commit

Then you have a friend in git log to show what commits are different between branches and git show to show what files changes in a commit.

And then finally make the commit messages more accurate with

# find out what commits your are talking about
git switch mongo-branch
git log mongo-branch..main
git show --name-only {what every commit you want}
# to fix the message in the last three commit
git rebase -i HEAD~3 
# when you are all done will need a force
git push -f

I’m Rich & Co.

Welcome to Tongfamily, our cozy corner of the internet dedicated to all things technology and interesting. Here, we invite you to join us on a journey of tips, tricks, and traps. Let’s get geeky!

Let’s connect