Sirop4ik
Sirop4ik

Reputation: 5273

How to exclude commits from git?

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.

Upvotes: 1

Views: 829

Answers (1)

jo_
jo_

Reputation: 9460

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

Upvotes: 1

Related Questions