Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
git revert
3.
git reset
4.
git rebase
5.
git reflog
6.
FAQs
7.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

Git Change Commands

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Git keeps track of every change you make to a file, which allows you to revert to a specific version if needed. Additionally, Git facilitates collaboration by making it easy to merge changes made by multiple people into a single repository. If you make a mistake, you can easily undo any changes in a repository with Git. 

Here we will see how we can use the following git change commands:

  1. git reset
  2. git revert
  3. git rebase
  4. git reflog

git revert

The git revert command is used to revert a commit in Git. As a result, a new commit with inverse content is created. Thus, we should only use git revert to reverse a particular commit. It does not remove all subsequent commits, just  undoes the most recent commit.

Unlike git checkout and git reset, reverting does not move the reference pointers to the commit we're reverting. This command instead moves the HEAD reference pointer to the specified commit.

Here is how we use this command. This repository has two text files: index.txt and example.txt, with the following content.
 


 

 
 

Assume you have made some changes to your file and committed them using the commit command.


 

Let's use the command git log -oneline to see what we have done so far.
 


 

As you realize that the changes you made in the master file recently aren't required, you need to revert them by using the following command: git revert *commit ash> where commit hash refers to the code you wrote along with the changes. 
 


 

Using this method, we're reverting the most recent commit. In the end, we used git revert to verify the commits were reverted by viewing the file's output.
 

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

git reset

There may be times when you realize that the changes you made weren't that good. Modify a few files, add and delete several lines, and now you want to undo it. Simply put, you need to roll back the changes you made and go back to the original files. Developers can use "reset to HEAD" to fix bugs quite quickly.

You can use this command to rewrite the history to look as if you never made the commit. It is used when you have committed but haven't shared it with anyone else.

Many commands are available for resetting, such as:

$ git reset --hard HEAD		-To go back to HEAD
$ git reset --hard HEAD^		-To go back to the commit before HEAD
$ git reset --hard HEAD~1		-equals to to "^."
$ git reset --hard HEAD~2		-To go back two commits before HEAD

 

To check what changes have been made, write “git log –oneline.”
 


To move the Head one step back, use the command: "$ git reset --hard Head~1". 
 


 

We have undone the previous revert action "Revert changes to example file again.".
 


 

We have been able to retrieve the file's content by doing this.
 

git rebase

In software development, rebasing refers to moving or combining a series of commits into a single commit. Changing the base of a branch means turning it into if it had been made from a different commit. A Git commit is applied to a base by creating a new commit. Although the branch looks the same, it consists of new commits.

This is an alternative to merging because it gives us a cleaner history of the repository, and a clearer history can help us find bugs faster. There are two branches here, the master branch and the "new_branch."
 


 

Next, create a file "new_file.txt" in the "new_branch."
 


 

The contents of the "new_branch" are shown here.
 


 

After changing to the master branch, "new_file.txt" is no longer visible because it is no longer part of the master branch but only part of the "new_branch."
 


 

A new file has been added to the master branch named 'new_file_1'.
 


As soon as you make changes to the "master" branch, you can use the git rebase command to integrate the changes into "new_branch." 

Before performing git rebase, the repository will look like this:



 

If you perform git rebase, you'll see that "new_file_1" is also added to "new_branch."
 

git reflog

The reflog maintains a record of when branch tips are modified. This command aims to maintain that information for about 90 days. In the reflog, you will find a record of every Git operation you perform where data is stored. Even though Git works hard to keep your data safe if, for some reason, you believe it has, chances are you can retrieve it using the git reflog command. 

Thus, you wouldn't need to worry about losing your work if you merged, rebased, or used some other action since this command will bring it back.

You'll typically use this command if you just did a git reset and updated your HEAD a few commits ago. Then you realize that you left that bit of code in your second commit. What do you do? you do? You probably want to run git reflog as follows:

 

Check out most important Git Interview Questions here.

FAQs

  1. How do git reverts and git resets differ?
    Git reset, and git revert is two different commands that reset the branch state to a previous state by removing all the changes since the desired commit, whereas git revert will create new commits that reverse the selected commit. You should use git revert rather than git reset.
     
  2. What are the differences between git merge and git rebase?
    Git rebase and merge allow you to merge changes between two branches. The difference is that git rebase moves a feature branch into the master. Git merge preserves history while adding new commits.
     
  3. Make a comparison between git log and git reflog.
    Reflogs differ significantly from logs in that the log presents a public accounting of the repository's commit history, whereas reflogs offer a private and workspace-specific accounting.
     
  4. What is soft and hard git resets?
    Your files will not be deleted, and all changes will be staged back automatically when you run git reset --soft. With git reset --hard, all changes will be wiped out and removed from the local directory. Please only use this if you are confident of what you're doing.

Key Takeaways

In this blog, we have seen that git change commands are used to restore or delete the changes we had made in the files using various commands.

We hope that this blog has helped you enhance your knowledge about Git Change Commands and if you would like to learn more, check out our articles on the link. Do upvote our blog to help other ninjas grow. 

Happy Coding!

Previous article
Git Activity Command
Next article
Git branch commands
Live masterclass