0

I have a project where I have the following commit sequence:

* 800cedc (HEAD, origin/master, origin/HEAD, master) Commit 6 * 1d51716 Commit 5 * 5232f4b Commit 4 * 01838a5 Commit 3 * 3aeb34d Commit 2 * 9b75b72 Commit 1 

For development reasons, what I need to do is the following: 1. Go to Commit 5. 2. Make some small changes. 3. Publish the changes to production. 4. Add the changes to the the commits.

The problem comes when I see that the file that I have changed, it's also changed in Commit 6.

What can I do to change the file in Commit 5 and merge the changes with commit 6 to keep good track of everything?

Thank you!!

6
  • Search git soft reset Commented Jun 20, 2019 at 8:06
  • 1
    Since you have already pushed all commits (and it sounds like they also went into production), you should not go back and fix the commit. Instead, own the mistake and fix it in a new commit Commented Jun 20, 2019 at 8:06
  • 1
    Also worth noting: you cannot change an existing commit. You can go back to commit 4 (5232f4b), make a new and improved variant of commit 5 (the original commit will still be 1d51716 and you'll have some new big ugly hash ID now), and then copy commit 6 to a new and improved version that won't be 800cedc800cedc will still be what it is, permanently linked to 1d51716. Then you can ask your Git and every other Git (such as the one at origin) to please stop using 800cedc and switch to whatever your last commit is, but there's no guarantee they'll do that. Commented Jun 20, 2019 at 8:49
  • @torek Maybe I explained myself wrong. I'm not intending to change a commit. In this case, I found a bug that needs to be fixed in Commit 5, as Commit 6 is not in production yet. Commented Jun 20, 2019 at 12:48
  • This is mostly a caution: if you think of "fixing a bug" as "changing a commit" you're headed down a bad path that may trip you up someday. If you think of it as replacing the commit (with a new and improved one), you're headed down a good path, because that's what Git really does under the hood and you won't be surprised to find that the original commit is still being used by others. So the words you use, even in your own head just thinking about these things, matter. Commented Jun 20, 2019 at 16:20

3 Answers 3

4

As @NilsWerner 's comment says, it's not a good idea to rewrite a published history. But if you really want to,

git reset 1d51716 --hard # modify the file and git add path/to/file git commit --amend # apply Commit 6 again git cherry-pick 800cedc # there might be conflicts and if any, # resolve the conflicts, and then git add path/to/file git cherry-pick --continue # force push "master" git push origin -f master 

After force-push, do tell other contributors that you have rewritten master and they should force-update their local master and apply their unpushed commits onto their new local master, which is quite a troublesome task.

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

1 Comment

Thanks!! Will try this.
1

You can do below steps.

  1. Create new branch out of commit5.

    git branch branchname <sha1-of-commit5> git checkout branchname 
  2. You cannot do cherrypick for individual file. You can only do cherrypick at commit level. do a local copy of the specific file of Commit5(say file_a), which you want to merge from commit6. After doing that, checkout the specific file from commit6.

    git checkout <sha1-ofcommit6> path/to/file_a

  3. Now,manually do the merge of commit5 file (local copy) with commit6 file (in git branch)

  4. After doing the changes, stage them and push to remote.

    git add . git commit -m "manual merge for file_a" git push origin branchname``` 

Comments

0

Try:

git revert 1d51716 git revert --no-commit HEAD 

then, make your change.

Not sure, please be careful to verify.

BTW: I think the best way is just make a new commit :-)

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.