DEV Community

Discussion on: What is your favourite Git command?

Collapse
 
codeluggage profile image
Matias • Edited

git checkout -b == make a new branch off of your current branch and switch to it
git branch -m == rename your current branch

Branches are "free" in git. When I have mentored other engineers, the most empowering and liberating moment for them is almost always when they see git as a safety net. That tends to go hand in hand with the realization of how powerful branches are.

Got a nasty rebase coming up? Just git checkout -b with the same branch name first, but with some descriptive naming like -before-rebasing at the end. If things get messed up during the rebase, you can just undo everything and go back to before you started the rebase.

Coworker force pushed a branch you were branched off of, and now you can't pull easily due to conflicts? Rename your branch, then checkout their branch from upstream, and you can take your time and compare and fix it locally between 2 branches instead of in the middle of a pull.

Done some git acrobatics and aren't 100% sure then changes are right, but still want to push them up to remote? Make a new branch off of the remote branch so you have a local copy if anything goes wrong.

Made some large changes that you want to keep, but may be going in the wrong direction? Make a new branch, commit the changes, then go back to the original branch and continue working.

I highly recommend naming branches the same, but appending dashes with more explanatory comments, like -refactor or -test-stubs.

Git is a safety net that let's you relax and not worry about the state of your local folder, and liberally creating tons of branches is the key.