Home > OS >  How to revert changes to deleted and then recreated file with same content in git to it's state
How to revert changes to deleted and then recreated file with same content in git to it's state

Time:02-22

How to revert changes to deleted and then recreated file in git branch? I want these changes not to appear in git diff with main branch I've diverged from.

My steps were the following:

  1. diverged from main branch to branch test
  2. deleted file src/patches/axios.patch (not actual name, I'm using patch-package npm package so names are correct in my project for sure)
  3. committed change and pushed to remote
  4. generated new patch for axios with same name in other place (axios.patch)
  5. copied new patch from step 4 to src/patches as axios.patch
  6. committed changes and pushed to remote
  7. now in Github PR page I see same file deleted and same file added enter image description here

I know that I can revert a file to some specific state at some specific commit. In my case it would be main branch state when I've diverged from it ( if file changed in meanwhile )?

I've also checked git diff main on my branch and there are these changes also present, so it's not Github UI glitch:

diff --git a/src/patches/axios.patch b/src/patches/axios.patch
deleted file mode 120000
index fcc8ab7fb..000000000
--- a/src/patches/axios.patch
    /dev/null
@@ -1  0,0 @@
-../../patches/axios.patch
\ No newline at end of file
diff --git a/src/patches/axios.patch b/src/patches/axios.patch
new file mode 100644
index 000000000..fcc8ab7fb
--- /dev/null
    b/src/patches/axios.patch
@@ -0,0  1 @@
 ../../patches/axios.patch
\ No newline at end of file

CodePudding user response:

When you added the file you did not just undo an earlier removal. Notice these two lines in the provided output:

deleted file mode 120000

and

new file mode 100644

They mean that you deleted a symbolic link and added a regular file.

To undo the earlier removal, you really have to use

git checkout -f main -- src/patches/axios.patch

(Modern Git has git restore to do the same thing, but I do not know how to operate it---I'm an old-timer.)

  •  Tags:  
  • git
  • Related