1

Yesterday in git history appeared commits that should not be there.

Here is an screenshot

enter image description here

It happened because of it was needed to clean files that were under .gitignore but still were tracked by git. In order to do it we used this answer on SO

https://stackoverflow.com/a/23839198/5709159

And all is works like it should. But we realized that actually not all the files that were under .gitignore (mistake) should be deleted...

Issue is - that for now these commits were pushed and we need to exclude them (revert)

Are there some ideas how to do it?

Is there a way to take all these files that were deleted (we can see them in commit) and include it again in new commit?

P.S. Files were in git, but then they were deleted and pushed to git. Now we need to get them back.

For example we have such commit history A - B - C - D. We have some important files in commit A then in commit B these files were deleted, then in commit C - D we make regular commits with logic implementation and so on. So, files that were deleted in commit B we need to get back. We need to exclude commit B and leave commits C - D. Something like this A - C - D. Or another way is A - B - C - D - B.

Sirop4ik
  • 4,543
  • 2
  • 54
  • 121

1 Answers1

1

of course you can revert

git revert <SHA of erroneous commit>

or, you can rewrite the complete history BUT this will update all SHA which can be dangerous (if someone else is allready working on the branch)

for this you would have to freeze the others developpers to work during this

git checkout branchA   // one of the problematic branch
git rebase -i <sha1 befor the pb>
-->   mark the "remove ignored file" commit as `edit`
// this will stop into the "guilty commit"
// at this point we undo the commit 
git reset HEAD^ 
// and then redo do the job like it should have 
....
// including the  "git commit "
// you can even do more than one git "commit"
git rebase --continue

// then 
git push -f origin branchA

Other dev can now pull again

Note : to do it more safely you can create a work branch and drop it if result is not as expected

git checkout branchA   // one of the problematic branch
git checkout -b branchA-before-rework // just in case

git checkout -b A-rework-1
git revert <SHA1 erroneous commit>

// ? happy :)
git branch -f branchA   // will force branchA to here
git push -f origin branchA
// not happy try something else

git checkout branchA   // one of the problematic branch
git checkout -b A-rework-2
// do above "git rebase -i" stuff EXCEPT push -f
// ? not happy drop it
git checkout branchA   // one of the problematic branch
git checkout -b A-rework-n
// ? happy :)
git branch -f branchA   // will force branchA to here
git push -f origin branchA
Rudedog
  • 4,323
  • 1
  • 23
  • 34
jo_
  • 8,324
  • 1
  • 18
  • 14
  • editing the history of a repository is a very bad thing to do: all commits that are child of the ones you remove will get a new SHA. Each branch born from those commits will need rebase. Each working directory might corrupt badly. In short: anybody that use any commit downstream wrt the ones you delete will see bulldozers lifting their foundations. – Daemon Painter May 14 '20 at 09:41
  • @DaemonPainter Sure this is a disadvantage, do you know more safely way? – Sirop4ik May 14 '20 at 09:50
  • As far as I understood I can mark these lines as `edit` (if I want edit them) or just delete them (if want delete them without editing), right? – Sirop4ik May 14 '20 at 10:00
  • And also, for example I deleted all these lines (commints) from rebase file and I have 15 commits, I need them all, so I need to paste `git rebase --skip` for each commit (like 15 times) or there is a command like `take them all`? – Sirop4ik May 14 '20 at 10:03
  • all the new commit sha will be modified yes its a price to pay... – jo_ May 14 '20 at 12:59
  • you can just remove the commit, yes – jo_ May 14 '20 at 13:00
  • What do you think about `git revert` ? I think it could be used here, no? – Sirop4ik May 14 '20 at 13:05
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/213866/discussion-between-jo-and-aleksey-timoshchenko). – jo_ May 14 '20 at 15:11