Nabeel Sulieman

My Favorite "New" Git Commands


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

git checkout main
git pull
git checkout -b new-branch
git add .
git commit
git commit -m 'my comment text'
git rebase main
git rebase -i HEAD~4
git push
git 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:

git checkout prod
git rebase main
git push
git checkout main

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

git 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:

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