Git Rebase, Merge, Stash, Revert and Reset
Last Updated on July 25, 2023 by Editorial Team
Author(s): Muttineni Sai Rohith
Originally published on Towards AI.
Git rebase is an advanced feature in git which helps us when we are dealing with multiple branches. Rebasing is a bit more advanced, but incredibly useful when you need it. When you perform a rebase, you are changing the base of your branch. In essence, a rebase will look at each commit on your branch and update the code to make it seem like you’ve been working off the new base all along. Although they have similar uses, a rebase differs from a merge in that a rebase updates your branch by tweaking each commit, and a merge will update your branch by creating a new commit at the tip of your branch.
This is the third addition to the series of articles about Git —
Complete Git Tutorial for Beginners with Examples. The link is available below —
Complete Git Tutorial for Beginners with Examples
Git is a version control system that lets us track the changes we make to our files over time.
muttinenisairohith.medium.com
The second article is All About Git Branches… And Git pull vs fetch…
Link Below —
All About Git Branches… And Git pull vs fetch
With Branches in Git, we can create a copy of the file we would like to work on without messing up with the original…
muttinenisairohith.medium.com
So coming back to understanding Git Rebase, let’s start from scratch and develop the new code in git. Let’s create branch1 and add some new code to it.
git checkout -b branch1
Here we are adding add function in the code –
def add(a,b):
return a+b
a,b = 5,6
print(add(a,b))
Now let’s push the code to our branch1
git add .
git commit -m “adding calculator1 code”
git push origin branch1
Now let’s suppose another developer(developer2) is working on this code by creating branch2 based on branch1 and changing the features while you are working on implementing more functionalities in branch1
Suppose developer2 adds two commits to the code by adding the input() function and adding offset +5 to the add function in branch2
And at the same time, you add two commits sub() function and mul() function in your branch1
After two commits in branch1, the code looks like this —
def add(a,b):
return a+b
def sub(a,b):
return a-b
def mul(a,b):
return a*b
a,b = 5,6
print(add(a,b))
print(sub(a,b))
print(mul(a,b))
And after two commits in Branch2, the code looks like this —
def add(a,b):
return a+b+10
a = int(input())
b = int(input())
print(add(a,b))
our Repo Branch Flow Looks like this –
Now let’s suppose developer2 wanted to merge his branch branch2 with branch branch1, so that he is in sync with the existing project, then in this case using the merge function will require us to stash our changes as pulling the code from branch1 will override our changes. So instead of using stashing, pulling, and then merging we can simply use rebase and merge the branches.
Syntax:
git rebase branch1
After executing the above command from local branch2, If there are merge conflicts, manually accept the change, and once the change is done
Add the code and commit the code.
Once you feel the rebasing is done, we can give the below command to complete rebasing –
git rebase --continue
If there are no existing merge conflicts, then rebase will be completed, and then we can push the code.
Now our branch2 Flow looks like this –
This is how git rebase will be useful.
Git stash
For suppose developer2 made some changes in Branch2, and he came to know code is changed by developer1 in Branch1, and Developer2 has an idea that he can add some small block of code in Branch1 and later pull the code from Branch1 to Branch2 and later commit his code in order to avoid merge conflicts.
In this case, if you try to checkout to Branch1 with uncommitted changes in Branch2, Git will not allow us to do that –
In this case, Git stash will help us –
Stashing allows you to save your current unstaged changes and bring your branch back to an unmodified state. When you stash, your changes are pushed onto a stack. This is especially useful if you need to quickly switch to another branch without having to commit your incomplete changes.
We can stash our code in Branch2 using the command –
git stash
And later, our work is done in branch1. We can check out to branch2 and use the stash to apply the command to get our changes back.
git stash apply
Later we can use commands add, commit and push to push our changes.
Git stash apply vs. git stash pop
Git stash pop will remove the changes from the stack after applying changes.
Git stash pop = Git stash apply + Git stash drop
Git Revert vs. Git Reset
Git Reset:
Git reset is used when we want to unstage a file and bring our changes back to the working directory. It can also be used to remove commits from the local repository.
git reset --soft branch2~1
git reset –soft HEAD~1
This command will remove the commit but will not unstage a file. Our changes would still be in the staging area.
git reset –mixed HEAD~1 or git reset HEAD~1
This is the default command that we will use, which removes the commit as well as unstages the file, and our changes are stored in the working directory.
git reset –hard HEAD~1
This command removes the commit as well as the changes from your working directory. This command can also be called destructive command, as we would not be able to get back the changes, so be careful while using this command.
Note: Git reset will work only when the commits are not pushed in the remote repository.
Git revert:
Similar to Git reset, but the only difference is Git revert can be used when we want to revert commits from the remote repository, whereas git reset will only work for the local repository.
Git revert will create a new commit that is the opposite of everything in the given commit.
We can revert the latest commit by using the head alias like this:
git revert HEAD
Unlike git reset, where we can rollback last n local commits, we can use commit-id and revert any commit from the remote repository using the command git revert
git revert HEAD commit-id-here
I hope in this series of Articles about Git, we have covered all the topics about Git.
You can find the reference Git cheatsheet here .
I hope this is helpful.
Happy Learning…
Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming a sponsor.
Published via Towards AI