1

I have 8 commits but I need to go back and change one of my commits. I need to remove two files from it.

What I tried was

git checkout 3043b71 //this is the commit that I want to change git rm src/tcp.rs git rm src/ip.rs git commit --amend git checkout master git push -f 

But it seems that nothing has changed, what did I do wrong?

4 Answers 4

2

If you have already pushed these changes, and others have pulled from the central repo, you probably do NOT want to do this. This will rewrite the git history and cause problems for others that have pulled after you pushed the original commits. In this case, you should use git revert on the commit in question, then make only the changes you want to keep from that commit. Then commit and push.

If you have not pushed the changes, then you are OK and are free to fix your local repo any way you want before pushing.

Sign up to request clarification or add additional context in comments.

Comments

1

Once you've made your changes to your commit, your tree will look something like this:

(A) - (B) - (C) - (D) \ (B') 

What you need to do it rebase your changes C & D onto your change B'. You can do that easily.

git rebase B' D 

That will make it look like

(A) - (B) - (C) - (D) 

UPDATE:

Just to make this more clear. You should follow the OP steps up git commit --amend then do what I mentioned. So your entire workflow looks like this

git checkout B <make changes> git commit --amend git rebase B' D 

UPDATE 2:

@gtrig made a good point in another answer. This is rewriting history, so if you've already pushed to another repository (or someone else has pulled from you), this is a very bad idea and you should use git revert instead to make a new change that backs out the previous one. This will make your history more ugly, but it won't mess up everyone else's repository. This is only for local changes before they are pushed remotely.

4 Comments

how do I get the name of B'?
You can see your commit graph with all of your branches using git log --graph --decorate --abbrev-commit --pretty=oneline. I put this in my .gitconfig file as an alias for git graph.
Wouldn't this rebase B onto B' as well? (i.e. Shouldn't it be git rebase B D --onto B'?)
That might work too. I don't claim to be a git master, but I know that for the changes I've been making lately, what I said above has worked for me.
1

I think you need to reset the commit reset git

$ git reset --soft HEAD^ <1> $ edit <2> $ git commit -a -c ORIG_HEAD <3> 

Comments

0
git checkout 3043b71 git rm src/tcp.rs git rm src/ip.rs git commit --amend git rebase 3043b71 master --onto HEAD // <-- [1] git push -f 

[1] cherry-pick the remaining commits from master onto the new commit and reset master.

Or, instead of rebase, something slightly easier to follow:

... git cherry-pick 3043b71..master git checkout -B master ... 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.