Git Rebasing - 14 days of Git
Tags: 14daysofgit, git, devops
Published: 2022-09-30 11:02:15

It is Day 11 of my learning journey into Git.  So far on my learning journey has taken me on the following path:

* [What is version control?](https://www.techielass.com/what-is-version-control)
* [What is Git?](https://www.techielass.com/what-is-git)
* [Installing Git](https://www.techielass.com/installing-git/)
* [Basic Git commands to get started](https://www.techielass.com/basic-git-commands-to-get-started)
* [Inspect a Git repository](https://www.techielass.com/inspect-a-git-respository)
* [Git File Operations](https://www.techielass.com/git-file-operations)
* [Undoing commits and changes](https://www.techielass.com/undoing-commits-changes)
* [Rewriting history](https://www.techielass.com/rewriting-git-history/)
* [Git Branches](https://www.techielass.com/git-branches/)
* [Merging with Git](https://www.techielass.com/merging-with-git)

Are you joining me on this learning journey? Would love to hear how you are doing.

Today's learning objective is around Git rebasing, what it is and why you would use it.

### **What is Git Rebasing?**

Rebasing your Git repository rewrites its history.  It can be a harmful command, so it is one to watch when you use it.

Typically, you would use the _git rebase_ command for one of the following reasons:

* Edit a previous commit message
* Combine multiple commits into one
* Delete or revert commits that are no longer necessary

### **Why do we need Git rebase?**
Rebasing rewrites the project's history.  It gives you a much cleaner project history. Rebasing eliminates the unnecessary merge commits required by git merge.  It gives a much linear history when you are looking back at your logs.

### **Rebase a Git repository**
We have a repository with the main branch and then a feature branch. We've been working away on different things on this feature branch and have several commits.  If we look at the graph of this, it will look something along the lines of this.

![Git repository history graph](https://community.ops.io/remoteimages/uploads/articles/a0qqjua8szzrbaip50n9.jpg)

We want to bring all the commits and cool new features we've been creating from our feature branch into our main branch.  But we want to make the history as linear as possible. So instead of doing a _git merge_ we are going to use _git rebase_.

The commands we want to use are:
```bash
git checkout main
git rebase featurebranch
```

The first command makes sure we are in our main branch and the second command rebases that main branch to include the commits from feature.

Our git graph log would look like this:

![Git repository after rebase graph view](https://community.ops.io/remoteimages/uploads/articles/i9vo191br696ilgus4mi.jpg)

You can see our history has been rebased into the main branch in a linear way.

If we'd used the git merge command instead this is what our repository history would look like:

![Git merge graph view](https://community.ops.io/remoteimages/uploads/articles/6gef1emmay3n346ekcsf.jpg)

Using the git rebase command gives you a much clearer history, especially if you have a large team of developers working on different features and bugs.  It can become a real message if you are only using _git merge_.  The history really becomes hard to read and understand.

_Git rebase_ isn't always the right option though, as it does rewrite history.  It's important as a team you understand the right use case for _git rebase_ over _git merge_ and use it appropriately.


### **14 days of Git**

I'll be honest, this one took a little time to understand and really appreciate what it was for.  I still think there is more I can dig into on this one. Definitely booking marking this one so I can come back to it and do some more learning.

Tomorrow is all about stashing so that should be interesting! Be sure to[ subscribe](https://www.techielass.com/newsletter) and join me for that step in the learning journey!

You can follow along here:[ https://github.com/weeyin83/14daysofgit](https://github.com/weeyin83/14daysofgit)