Advanced Git Concepts You Should Know
Tags: git, github, productivity, programming
Published: 2022-01-02 06:34:05

Have you gotten accustomed to the basics of `git`, but the advanced concepts make you _scratch your head_?

![head scratch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eboh3yha9sgh0xi2u79f.gif)

This article got you covered, not only will it _introduce you to the advanced `git` concepts_, but also _show you how to use them in a real-world scenario_! Let's dive in.

# Stash

Let's first check the definition:

> Git stash is a built-in command with the distributed version control tool in Git that locally stores all the most recent changes in a workspace and resets the state of the workspace to the prior commit state.

The **stash** can be thought of as a _temporary storage_ for the _changes you made, but did not **commit**_. 

The real-world use case for this feature would be when you are _not done working on the changes_, but _need to pull the updates from the remote repository_.

To use **stash**, you need to _add the files to the **staging area**_

```bash
git add .
```

and push it to the **stash**

```bash
git stash push

     OR

git stash push -m "<stash message>"
```

To get back the changes you made, use:

```bash
git stash apply

     OR

git stash pop
```

The difference between `apply` and `pop` is `pop` applies the changes in the **stash** and removes it from the **stash** too, but `apply` keeps the changes in the **stash** even after applying it.

To view the items in the **stash** use:

```bash
git stash list
```

![Stash](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vjk1jhlh1aj6wc84ditj.jpg)

If you have multiple **stashed** changes, you can use the index to select the one you need

```
git stash apply stash@{<n>}

     OR

git stash apply <n>
```

# Get Back Deleted Commits

Ever used the `reset` command with the `--hard` flag? If you have, it's the right time to freak out, as it _completely removes the number of commits specified_. 

![freak out](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/10v9u3ivu87p8supv4vn.gif)

Don't panic, `reflog` got you covered! To view the changes, you recently made, run:

```
git reflog show HEAD

     OR

git reflog
```

You can now view the changes you made recently.

![find commit](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bwy9hugh885lirz2ttt4.PNG)

Now you can directly get back the commit using:

```
git reset --hard <commit hash>
```

**NOTE**: If you have any _local modifications_, _the command will destroy it_ as well, so it would be wise to use the **stash** before **resetting**.

# Cherry Pick Commits

Need a feature introduced in a **commit** in another branch, but the branch is not ready to be **merged** yet? No, you don't have to take the _500-year long nap_ till the branch is merged!

You can just **Cherry Pick** the commits you require

```
git cherry-pick -x <commit hash>

     OR

git cherry-pick <commit hash>
```

It's highly suggested that you do use `-x` flag as it will generate a _standardized commit message_, informing users where it was `cherry-picked` from.

# Rebase

**Rebasing** is the process of _moving or combining a sequence of commits to a new base commit_. The primary reason you would like to use `rebase` in your project is to _maintain a linear project history_, making it much easier to view the logs.

![Git Rebase](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hqtnc6n0x5ji8vmqo1b3.png)

Thus `rebase` allows you to work off the _latest changes on any branch, even though you started working before the changes were introduced_. It also helps you in the case of **Fast Forward Merge** discussed in the **Merge Strategies** section.

To rebase, use

```
git rebase <source branch>
```

Thus to _recreate the example in the image_, you would move to the **feature** branch and run:

```
git rebase main
```

### Caution

**Rebasing creates new commits while rewriting the history.** Thus you should **avoid rebasing once the branch is pushed to a public repository** as it might cause issues where _the old commits with new ones_ and _it would look like that part of your project history abruptly vanished_.

# Merge Strategies

_"Why bother learning about the **Merge Strategies**?"_ you may ask. My answer would be, I wanted to add 5 concepts instead of 4, so here we are 😛. PS: Don't forget to drop a ❤️ for honesty.

This is one of the **good-to-know** concepts, with _little repercussion if you don't know it_. It can help you a bit while _navigating the commit logs_ though, where otherwise you might end up _wondering how the merge took place without commits_.

There are several **Merge Strategies**:
- Fast Forward
- Recursive
- Ours
- Octopus
- Resolve
- Subtree

The commonly used strategies are **Fast Forward** and **Recursive**, which we will be looking at.

### Fast Forward Merge

The **Fast Forward Merge** takes place when the _destination branch of the merge does not contain any new commits_. In such a case, only the **pointer** of the branch is _moved forward to the required commit_. **It does not add any new commits!**

![Fast Forward Merge](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kk5iw6uaxiku1pypwq0e.png)

Example of merging **feature** on **master**

### Recursive Merge

The **Recursive Merge** takes place where both the _source & destination branches of the merge contain new commits_. In such a case, a _new commit is introduced in the destination branch_, merging all changes.

![Recursive Merge](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zhgt19t8xnp6ot90nq4q.png)

Example of merging **master** on **feature**

You can also force **Recursive Merge** using:

```
git merge --no-ff
```

# Wrapping Up

Starting the year with advanced `git` skills in your arsenal, you are ready to take on the world!

**Happy Developing!**

Finding **personal finance** too intimidating? Checkout my **Instagram** to become a [**Dollar Ninja**](https://www.instagram.com/the.dollar.ninja/)

# Thanks for reading

Need a **Top Rated Front-End Development Freelancer** to chop away your development woes? Contact me on [Upwork](https://www.upwork.com/o/profiles/users/~01c12e516ee1d35044/)

Want to see what I am working on? Check out my [Personal Website](https://tapajyoti-bose.vercel.app) and [GitHub](https://github.com/ruppysuppy)

Want to connect? Reach out to me on [LinkedIn](https://www.linkedin.com/in/tapajyoti-bose/)

I am a freelancer who will start off as a **Digital Nomad** in mid-2022. Want to catch the journey? Follow me on [Instagram](https://www.instagram.com/tapajyotib/)

Follow my blogs for **Weekly new Tidbits** on [Dev](https://dev.to/ruppysuppy)

**FAQ**

These are a few commonly asked questions I get. So, I hope this **FAQ** section solves your issues.

1. **I am a beginner, how should I learn Front-End Web Dev?**
   Look into the following articles:

    1. [Front End Development Roadmap](https://dev.to/ruppysuppy/front-end-developer-roadmap-zero-to-hero-4pkf)
    2. [Front End Project Ideas](https://dev.to/ruppysuppy/5-projects-to-master-front-end-development-57p)

2. **Would you mentor me?**
   Sorry, I am already under a lot of workload and would not have the time to mentor anyone.

3. **Would you like to collaborate on our site?**
   As mentioned in the _previous question_, I am in a time crunch, so I would have to pass on such opportunities.