Git – Changing History

Git provides a very important option of altering the commit history. As the master of git, you can control and make it function according to you. Git gives you the authority to change the order of commits, change messages, remove commits entirely. Moreover, it also allows you to manage the project’s history. However, it is advisable to commit the work after reviewing it thoroughly. Here you will know about the following things.

  • git commit ––amend
  • git rebase
  • git filter-branch

git commit – – amend
This command allows you to change your most recent commit. It helps to alter the message or to add or remove files. The command loads the previous commit message into an editor session, where you can make changes to the message and save those changes and exit. When you save and close the editor, the editor writes a new commit containing that updated commit message and makes it your new last commit. This command entirely replaces the last commit with a new commit.

  • git commit – – amend -m “Updated message” This command directly updates the message without opening the editor.
  • git commit – – amend –no-edit This option helps you to commit remaining changes if any without changing it’s commit message.

git rebase This command is used to modify the commit return the commit history. Rebase tool is used to rebase a series of commits onto the HEAD they were originally based on instead of moving them to another one. Rebase is like changing the base or route of the branch from one commit to another, making it look like you took this route instead of the other one.

  • git rebase -i Here “i” stands for interactive. This option allows you to stop after each commit that is being rechanneled to modify and change the message, add or remove files and many other options. This command must know how far you have to track back. This is done by git rebase -i HEAD~3 command. Where Head~3 refers to previous three commits.

  • git rebase – – continue This option restarts the rebasing process after having resolved a merge conflict.
  • git rebase – – abort This option aborts the rebasing process and reset head to original branch/route. If “branch” was provided when the rebase operation was started, then HEAD will be reset to “branch”. Otherwise, HEAD will be reset to where it was when the to rebase operation was started.
  • git rebase – – quit This option abort the process but do not reset head to original branch.

git filter-branch This is another option to rewrite a large number of commits. This is not commonly used as it can change a broad strip of history. However, it has some important uses like removing files form every commit, Making a Sub-directory the New Root and changing email address globally.

Summarizing: We learnt that if you want the alter the most recent commit, git commit –ammend command is used. This command enables the user to modify messages and files from the last commit. git rebase is used when a thread of commits’ history needs to be modified. git rebase -i gives you command to control and manage your project’s history in a mannered way. git filter-branch command also have basic functionality like rebase command i.e. to operate history of the larger number of commits but is used occasionally due to its strict operation.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.