2

I work on a feature ( branch ), during the progress I deleted a file no longer used, but for some reason, I need it back.

So I did this :

git checkout hashofcommit0 -- path/to/my/file

It works but I've lost all annotations on the file. and I really need annotations.

Of course, I can create a new branch, apply all diff done with my last branch ( except the file removed ), and be happy.

(0) -> (1) -> (..) -> (n)

Is there a solution to go back to a commit(0), undo the remove, and go back to last commit(n) with no conflict?

oxmolol
  • 125
  • 1
  • 1
  • 10

2 Answers2

3
git rebase -i hashofcommit0

mark as edit the commit where you removed the file

when the rebase operation arrives to that commit, it should give you control and a shell

git add name_of_file
git commit --amend
git rebase --continue

let me know

Carpao 67
  • 41
  • 2
  • file deleted was at first commit. what shall I do ? – oxmolol Nov 28 '17 at 16:06
  • git rebase -i --root (if it is just the root commit without parents...) But I didn't understand... how is it possible that you delete a file since first commit... it means that you never added it... – Carpao 67 Nov 28 '17 at 16:09
  • --root with a 60k+ commit repo ? – oxmolol Nov 28 '17 at 16:12
  • if you have other doubts, please give me more information (e.g., a partial history around the rebase point) We have no information about what is your size and numbers of commit – Carpao 67 Nov 28 '17 at 16:13
  • simply, I created a banch from master ( 65k commits ), my first commit was ( in part ) my wanted `deleted file`. Now I'm at commit n6 ( of my branch ). – oxmolol Nov 28 '17 at 16:15
  • Follow the instructions of carpao but rebasing on the parent of your first commit – Carlo Bellettini Nov 28 '17 at 17:04
2

Elaboration of the Carpao answer

git rebase -i hashofcommit0^   (note the ^ that indicate the father of your first commit)

you will be presented with a list of commit starting from the older to the more recent

pick hashofcommit0  comment of commit0
pick hashofcommit1  comment of commit1
pick hashofcommit2  comment of commit2
pick hashofcommit3  comment of commit3
pick hashofcommit4  comment of commit4
pick hashofcommit5  comment of commit5
...

change just the first pick to edit

you will be prompted wit a shell in a situation corrispondent to the faulty commit

recover the missed file for example with the command:

git checkout HEAD^ -- nameOfMissedFile  

then re-add it and amend the commit before to continue with the rebase

git add nameOfMissedFile
git commit --amend        (confirm or change the comment of the commit)
git rebase --continue 
Carlo Bellettini
  • 1,130
  • 11
  • 20
  • ok nice, so I made a list, I have 2 commit to edit and 1 to drop. In one commit I need to undo the rm, In another one I need to undo the add, and the last commit can be droped. Is It better/easier to do this 3 times for each commit, or all in one ? thanks for the ^ trick ! – oxmolol Nov 29 '17 at 09:19
  • it is good to make them all together but as an additional suggestion, before starting the rebase create a branch pointing to the original one so you can restart from a clean situation if something goes wrong – Carlo Bellettini Nov 29 '17 at 09:22
  • ok, all is fine, I have all I want, but because of the rebase, I have a "diverged" message, that tell me to git pull, but if I do it, I will loose my file back! – oxmolol Nov 29 '17 at 13:25
  • Did you push your branch in the past? You didn't say that...if it is a branch where just you worked, change its name and push again with the new name... Usually when you already made public your history it is not a good idea to change it, and it is more convenient use git revert command – Carlo Bellettini Nov 29 '17 at 13:29
  • Yep, I already pushed it ( to dont have only local backup ). So I change the name and push It like a new branch ? many thanks btw – oxmolol Nov 29 '17 at 13:33
  • It is the easier solution... Clearly it does not delete the old one – Carlo Bellettini Nov 29 '17 at 13:35
  • ok, so You agree with "branch -m" to rename, and 'push --set-upstream origin newbranchname' ? – oxmolol Nov 29 '17 at 13:40
  • it's ok, I would add to give again the old name to the brach so that it corresponds to the remote... If after that it is all working... follow instruction in https://stackoverflow.com/questions/2003505/how-do-i-delete-a-git-branch-both-locally-and-remotely to remove the old branch from the remote repository – Carlo Bellettini Nov 29 '17 at 13:56
  • when you finish with your operation, if you are happy, please mark one of the asnwers (Carpao's or mine) as correct answer to your question and give an "up" to the other, because I think that both helped you to find the solution – Carlo Bellettini Nov 29 '17 at 14:01