Life-saver Git commands

Life-saver Git commands

The Git version control system can be the arch-enemy of developers if they are unfamiliar with the commands and their use cases. Also, it can be your best friend if you devote time to learning commands one by one, but that would be tiresome.

That’s why I gather 6 life-saver commands for Git in this post to make you use this VCS as a professional.


Restoring previous commits

The case is you want to implement a new feature to the application you are developing, but unfortunately, you created a huge bug. Instead of telling people this bug is the new functionality, you go back to the latest commit, where your code was clean and less buggy.

Git is the best tool to use in this situation with the following commands👇

#Shows the name and the index of prrrevious commits
git reflog

git reset HEAD~{index}

git reflog will show all commits with their HEAD@{index}. This index will be used for replacing the current code with the commit you selected.

Rename the latest commit

You used the wrong naming convention and your boss get mad at you. No worries, the amend flag of git commit was made for renaming the previous commit on the current branch.

git commit --amend
Avoid renaming commits on a public branch that you didn’t commit.

Moving commits between branches

You pushed the latest commit to the wrong branch and it broke your code in production. There are two ways to move your commit between branches.

  1. Cherry-pick

  2. Reset and stash

Let’s look at the first option. To get started you must create a new branch if you did not have one already. Switch to the correct branch and grab the last commit from the wrong branch. Finally, checkout to the wrong branch undo, and remove the changes you made.

# If you have created the correct branch omit this command
git branch correct-branch

#Grab the code from the wrong-branch
git checkout correct-branch
git cherry-pick wrong-branch

#Remove changes on the wring-branch
git checkout wrong-branch
git reset HEAD~ --hard

The second alternative uses a combination of the reset and stash commands.

First, undo the changes you have made on the wrong branch, but keep the changes. To move them to the other branch you have to stash them, switch to the correct branch, and pop the stashed changes. Lastly, stage these modifications and commit to the proper branch.

#Reset the commit but keep changes
git reset HEAD~ --soft
git stash

git checkout correct-branch
#Adds the changes to the current branch
git stash pop
git add --all
git commit -m “I-fixed-it-myself-I-swear😉”

Creating a branch from a previous commit

You want to alternate the layout of a former version of a landing page, but you need a separate branch to evade breaking things. Since you read this article you know how to create a brand new branch from a previous commit.

  1. Create a new branch from the latest version of your master branch and switch to it

  2. Reflog and reset the new branch to the commit you want to use as a base

  3. Makes some changes and commit them

git branch new-branch
#Switch to the new branch
git checkout new-branch

#Go back in time to the commit you want to use
git reflog
git reset --hard HEAD~{index}

Rebasing

From the previous trick, you alternated the landing page and you got fond of it. Now you want to see it in production mode but do not know how could you apply your changes to the master branch.

This is where rebase comes into the image. With this command, you can place a separate branch on top of another.

#git rebase <which-branch> <to-top-of-another-branch>
git rebase new-branch master
#The changes on the new-branch are living on master now

Displaying the network of branches

There are many plugins for IDEs to show you the network of a repository, but did you know you can display it using a single Git command?

git log can show all commits from every branch and make them appear as a graph.

git log --graph --all

Wrap up

The tricks I showed you are just the tip of the iceberg, but I hope you learned something new today.

Thank you for taking the time to read this post. I look forward to our next encounter 👋

Did you find this article valuable?

Support Gergő Pásztor by becoming a sponsor. Any amount is appreciated!