;

My Favorite "New" Git Commands

2020-08-28

I don’t consider myself a Git expert. I’ve basically memorized the commands I use 99% of the time:

 1git checkout main
 2git pull
 3git checkout -b new-branch
 4git add .
 5git commit
 6git commit -m 'my comment text'
 7git rebase main
 8git rebase -i HEAD~4
 9git push
10git push -f

However, I’ve recently “discovered” two new commands that I’m finding extremely useful:

Push to Branch Without Checking it Out

I frequently want to push my main branch to my prod branch. Using my small set of memorized commands, I used to do this as follows:

1git checkout prod
2git rebase main
3git push
4git checkout main

I’ve recently discovered that I can replace those 4 lines with the following one-liner:

1git push origin main:prod

Set File Contents from a Different Branch

Let’s say you’re in branch my-branch and you edited 10 files. You want to set one of the files back to the way it looks in the main branch. You can do this like so:

1git checkout main my-directory/my-file.txt