Home ยป Git revert files to a specific previous version

Git revert files to a specific previous version

git revert multiple files

In this article, we will learn how to revert files to a specific previous version or commit id.


Have you ever faced a situation where you have committed some files in git, pushed them to the remote server, and then later wanted to revert some of the files to their previous states?

If yes, then it is very simple to git revert files to a specific previous version without making separate commits for those files.


Git revert files to a specific previous version

For this, you just need file names and the commit id to which you want to revert those files.
And then, you need to run the below command using the above details.

git checkout <commit-id> -- <filename-1> <filename-2> ... <filename-n>

In the above command,

  • <commit-id> is the commit id to which want to revert the files.
    You can get the commit id using git log command.
  • <filename-1>, <filename-2> are the file names that you want to revert.

Now, your files are ready to be committed to the previous version.


Push new changes to the same latest commit without creating a new one

Using the below command, you can just edit the latest commit id with these changes so that you don’t have to create a new commit id.

git commit --amend --no-edit

In the above command,

  • `–amend` is to edit the latest commit id and add the new changes in that same commit.
  • `–no-edit` is to tell the command that you do not want any other edits like commit message or anything so it will just add those new changed files to the latest commit id.

I hope you like this article and helps you to solve your problems.

Visit Techtalkbook to find more related topics.


References


Leave a Reply

Your email address will not be published.