Git Commands That Will Save Your Job 
Tags: productivity, webdev, beginners, devops
Published: 2021-12-02 12:59:01

In this article, we are going to learn some life-saving Git commands.

We will not only learn about them, but we will also construct a mental model by understanding how they work behind the scenes.

Some of these you may have used before, while others may be new to you, but they are all lifesavers.

# The Real Fault Is to Have Faults And Not to AMEND Them

> “You will be remembered in your typos and edit history”
> 
> — Anonymous

Typos happen all the time!

Git gives us the option to correct our mistakes.

When committing, sometimes we can hit “Enter” too quickly and have a typo somewhere in the commit message.

![](https://miro.medium.com/max/700/1*Itfh8uj_l0LAbGH9Q9W2bg.png)</figure>

![](https://miro.medium.com/max/700/1*fX6dZBNM2d_etdA6-UaITQ.png)

<figcaption class="hz ia cu cs ct ib ic bo b fm bq br" data-selectable-paragraph="" style="">use -amend command to fix the typo</figcaption>

</figure>

Perfect! Now, your co-workers will not see that your “**commit**” was originally spelled “**committ**”.

# Well Crafted Thing Is a Thing to Behold

You might have used the command `git add <file>` or `git add .` many times but doing so doesn’t allow us to commit just a section of a file.

<figure class="hm hn ho hp hq hr cs ct paragraph-image">![](https://miro.medium.com/max/700/1*gSwWWmLhWgzttSPlHcTJ7g.png)</figure>

`git add -p` allows you to stage parts of a changed file, instead of the entire file.

This allows you to make concise, well-crafted commits that make for an easier-to-read history.

Doing so can improve the quality of the commits.

It also makes it easy to remove parts of the changes in a file that was only there for debugging purposes — before the commit without having to go back to the editor.

Once this command runs, we’ll be asked about “hunks”.

![](https://miro.medium.com/max/700/1*3SLl1UnyheC5E_m0QXB0mQ.png)

<figcaption class="hz ia cu cs ct ib ic bo b fm bq br" data-selectable-paragraph="" style="">“Hunks” are the _chunks_ of code that we’ll decide what to do with.</figcaption>

</figure>

You’ll see the question, “Stage this hunk [y, n, g, a, d, e, ?]?” at the bottom of the terminal.

![](https://miro.medium.com/max/700/1*Sj4F5okGIW-K60QjbRoHNg.png)</figure>

When finished with all the “hunks”, we will be able to see that our commit is ready and we can continue to the push!

## Bonus Tip:

`git commit -p` combines `git add -p` and `git commit` in one command.

# Your Mistakes Don’t Define You

You made a commit but now you regret it? Maybe you committed secrets by accident — not a good idea — or maybe you want to add more tests to your code changes.

These are all legit reasons to undo your last commit.

Git gives us a command to undo our last commit.

<figure class="hm hn ho hp hq hr cs ct paragraph-image">![](https://miro.medium.com/max/700/1*1KBq39Wsd0HJWO4v0q8WNQ.png)</figure>

The command `git reset` has a variety of things you can trail onto it, let’s talk about the "soft" trailing.

`git reset —soft HEAD~` _removes the commit from the current branch, but keep the changes!_

**The cool thing about this command is that the changes won’t disappear**!

All the changes will stay in the working tree!

![](https://miro.medium.com/max/700/0*05owIT6H4P6xfyf2.gif)

<figcaption class="hz ia cu cs ct ib ic bo b fm bq br" data-selectable-paragraph="" style="">We ran the command git reset — soft HEAD~2 — it moved the HEAD to the previous two commits.</figcaption>

</figure>

If you don’t want to keep the changes that were introduced by certain commits then use `git reset —hard HEAD~`

![](https://miro.medium.com/max/700/0*ue43AYghqHFJHrg5.gif)</figure>

## **Bonus Tip:**

The git [pre-commit hook](https://gist.github.com/eyarz/64770d343b9cab442b257869f497af9e) is a built-in feature that lets you define scripts that will run automatically before each commit. Use it to reduce the need to cancel commits.

# Look for your choices, pick the best one, then go with it.

Suppose you are working with a team of developers on a medium to large-sized project.

Another team member proposed some changes, and you want to apply a few of them to your main project, not _all_.

Since managing the changes between several Git branches can become a complex task, and you don’t want to merge a whole branch into another branch.

You only need to pick one or two specific commits. To pick some changes into your main project branch from other branches is called cherry-picking.

<figure class="hm hn ho hp hq hr cs ct paragraph-image">![](https://miro.medium.com/max/700/1*nvKsXUB33j-s5zSlHDsNaQ.png)</figure>

In this command, we can pick a particular branch that has a commit and pull it into another. Let’s walk through this.

A commit is a snapshot of your git repository at one point in time and each commit cumulatively forms your repo history.

![](https://miro.medium.com/max/700/1*DrwJLoomyKrBbVQa1OBFog.gif)</figure>

The `cherry-pick` command takes changes from a target commit and places them onto the HEAD of your currently checked-out branch.

![](https://miro.medium.com/max/700/1*jB7KWUr2iKnMu0EEI73jJA.gif)</figure>

From here, you may either continue working with the changes in your working directory or you may commit the cherry-picked changes immediately.

![](https://miro.medium.com/max/700/1*o2HlQEG1lWkZpRHK9oOHdg.gif)</figure>

This is helpful if you accidentally make a commit to the wrong branch.

![](https://miro.medium.com/max/700/1*r4Rn2Hf9o0CDJ8lBsXGGLQ.gif)</figure>

With cherry-picking, you can get those changes onto the correct branch without redoing all that work again.

![](https://miro.medium.com/max/700/1*TQ4V0IN1v9preGrIOLXQwA.gif)</figure>

However, use `cherry-pick` sparingly!

Overusing `cherry-pick` can lead to duplicate commits, and sometimes a merge may be preferred to preserve commit history.

![](https://miro.medium.com/max/700/1*lfz_nmKf_TfNAMue4eKO9Q.gif)</figure>

## Real-Life Scenario:

Let’s say we have a branch `76d12` and we have committed all of our changes.

There’s another branch, we’ll just use `master` but this can be any branch, and we want to pull in `76d12`’s commit.

By using the command, `git cherry-pick 76d12`we can get that entire commit into another branch.

![](https://miro.medium.com/max/700/0*0uzNtqDd8KiuSCI9.gif)

<figcaption class="hz ia cu cs ct ib ic bo b fm bq br" data-selectable-paragraph="" style="">`cherry-pick b`_ring an entire commit from one branch into another._</figcaption>

</figure>

# Don’t Want All Of It? No Problem — Just Checkout

Want to Pick a specific file from a branch and bring it into the current branch?

<figure class="hm hn ho hp hq hr cs ct paragraph-image">![](https://miro.medium.com/max/700/1*1qnjBHU7Q6n5ZKJ93xVFxg.png)</figure>

Som, I use `git checkout` mostly for switching from one branch to another.

Yes, I know but we can also use it for checking out files and commits.

It’s like `git cherry-pick` but rather than "cherry-picking" an entire commit, we are diving even deeper into a branch and picking just a particular file that we want to be merged.

![](https://miro.medium.com/max/700/1*poZDlGmqOwvJSleVGbiN0g.png)</figure>

While staying in `master` the entire time, we can run the command `git checkout Som-test index.js` to grab that particular committed file in that particular branch and bring it over to master to eventually push.

That’s pretty neat when a particular branch’s changes are many and we’re only looking for one file to test or push.

## How does it work:

`git checkout` tells Git which branch or commit to apply your new changes.


![](https://miro.medium.com/max/700/1*yL68gbUi3Yr4jYw3jM3fhg.gif)</figure>

A branch is just a pointer to 1 specific commit, and a commit is a snapshot of your repository at a certain point in time.

![](https://miro.medium.com/max/700/1*VF0e86kTT4PIhyTNB_W0vw.gif)</figure>

The branch pointer moves along with each new commit you make.

![](https://miro.medium.com/max/700/1*I8NzBgYrvHoeUTsuAxP8AA.gif)</figure>

If you want to make changes on a branch that you don’t currently have checked out, you first need to checkout the branch.

![](https://miro.medium.com/max/700/1*nL8uzFZw9GAG0MN0m2vhaQ.gif)</figure>

Checking out a branch will update your repo’s files to match the snapshot of whichever commit the branch points to.

![](https://miro.medium.com/max/700/1*TziopasHJcCOarYEDN8aGQ.gif)</figure>

From here, the branch pointer will follow each new commit that you make on the branch.

![](https://miro.medium.com/max/700/1*vU_NQyoorJvW5NHXrzr5xw.gif)</figure>

It’s also possible to directly checkout a commit, which will update your repo’s files to match the snapshot of whichever commit you’ve checked out.

![](https://miro.medium.com/max/700/1*RlHBw8VmAUyELW1U2uRUQg.gif)</figure>

Be aware that checking out a commit moves the HEAD pointer to a commit instead of a branch, putting your repo into what is called a “detached head state” where any changes will not be committed to a branch.

![](https://miro.medium.com/max/700/1*sjv7VIu4_XGrNBAmInUQ9A.gif)</figure>

While git checkout is great for getting to work with branches, it’s also a useful tool for reviewing old commits since it switches the version of your repo to this older snapshot.

# Someday…But Not Yet, Not Yet

Suppose we have a bunch of changes to a file but need to go back and test something out before we commit and push this or we’re just not ready to commit our changes.

That’s where `git stash` comes into play.

<figure class="hm hn ho hp hq hr cs ct paragraph-image">![](https://miro.medium.com/max/700/1*HBo3QdLBcH13_5RqQUpolw.png)

<figcaption class="hz ia cu cs ct ib ic bo b fm bq br" data-selectable-paragraph="" style="">Save uncommitted changes and stash them for later. This is useful for when you want to commit and push, but aren’t ready to commit the changes since the last commit.</figcaption>

</figure>

When we run the command `git stash` it takes those saved, uncommitted changes and "stashes" them for later.

We can then move around different branches, pull changes into master, etc.

Once we have finished testing or doing whatever we needed to do, we can go back into our branch, run the command `git stash pop` and our changes will come back! It’s like we never even left!

# It Is Time For You To Get Some Help.

There are a plethora of git commands to choose from. And it’s critical that we don’t run commands without knowing what they’ll do to our code.

If we were to run just `git help <command>` then we would get an explanation of the command right there in our terminal.

## _Bonus Tip:_

Running the command `git help -w <command>` takes us directly to the website that we can read up on all the things with the command in question.

# Conclusion

Git is an extremely useful tool. It may be a nightmare if handled incorrectly. I hope these hidden treasures make your Git experience a little more productive.

What is your go-to Git command? Please let me know in the comments!

# Note of Gratitude

I wanted to take this last opportunity to say thank you.

Thank you for being here! I could not be able to do what I do without people like you who follow along and take that leap of faith to read my post.

I hope you’ll [**join me**](https://polymathsomnath.medium.com/subscribe) in [**my future blog post**](https://polymathsomnath.medium.com/subscribe) and stick around because we have something great here.

And I hope I will help you along in your career for many more years to come!

See you next time. Bye!