-1

I gave up trying to use git revert in my computer. Whenever I try to use it, I get an error Unlink of file Failed (Unlink of file' failed. Should I try again? (y/n)), and I tried implementing different solutions given on this website without much success.

So my question is: Is there any alternative that will create a new commit ID when reversing changes so that it's safe to use for remote collaboration? (Not reset, or restore since they don't create a new commit ID)

Could git rebase be a solution? Since it essentially makes a copy of previous commit IDs? I'm just trying to think of alternatives at this point.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
tadm123
  • 8,294
  • 7
  • 28
  • 44
  • Can you recreate a situation that causes this "unlink of file failed" error? I use `git revert` regularly and have never seen this issue before. I'm not saying it's not an issue. I'm just curious about what are the exact conditions that cause it. – Code-Apprentice May 01 '23 at 22:53
  • Are you reverting a commit that added a brand new file? Or one that contains changes to an existing file? I more commonly do the later. If you are doing the former, is the file open in another application or otherwise being used so that the OS is blocking git from deleting it? It seems to me that the situtation that causes the "unlink of file failed" has a deeper cause than `git revert` itself. – Code-Apprentice May 01 '23 at 22:55
  • 3
    "Is there an alternative ..." - Of course, you can just undo the changes of a commit manually and then `git add`/`git commit` them. – mkrieger1 May 01 '23 at 22:57
  • Or `git show SHA | patch -R -p1` , but I'm more curious about this mystery unlink error. – Useless May 01 '23 at 23:04
  • @Code-Apprentice the contect is that I added a submodule, then reverting it would delete that submodule. This is where I get that error. – tadm123 May 02 '23 at 03:14
  • @mkrieger1 is there a more efficient alternative than going over and reversing it manually? In some cases I make lots of changes in the commit I want to revert. It's just very strange that I can't do that, it would save me lots of time if I this would work.. – tadm123 May 02 '23 at 03:17
  • submodules add a layer of complexity. I don't use them so I'm not be able to give any suggestions. It will help a lot if you can create a sequence of commands that causes the error. Then someone might be able to provide better suggestions. – Code-Apprentice May 02 '23 at 04:53
  • 1
    (interactive) rebase could also be used to recreate the history with the commits that add the submodules omitted. – mkrieger1 May 02 '23 at 06:53
  • @mkrieger1 can you give an example how would this work. For example if I added two submodules in two consecutive commits and I want to revert these commits so that my repository doesn't have them. How would you do it with rebase instead of revert? – tadm123 May 02 '23 at 20:38
  • 1
    Here are some examples: https://stackoverflow.com/a/1338758/ – mkrieger1 May 02 '23 at 21:10

1 Answers1

1

One option to create a new commit, with the same content of the previous commit (or you can also change the content) and generate a new commit ID is to use git commit --amend.

With git commit --amend, a text editor will open after you run this command, the commits are going to be in it and their corresponding commit message, change the commit message, save the file and close it, to change just the commit message.

also running git commit --amend -m "an updated commit message" it will change the commit message of the most recent commit.

To add new changes to the commit:

git add desiredfile.java

git commit --amend --no-edit

--no-edit stands for not editing the commit message.

Now, you can push the new commit to the remote. It will generate a new commit ID.

Rodrigo
  • 61
  • 1
  • 12