git merging two commits

I am not sure if this is a duplicate and I am not a git expert, so forgive me for what may seem like a stupid question.

I have a file. I edited it, saved and committed those changes.

I forgot I had done this and edited the original version of the file (which happened to be open in another editor window), saved and committed a different set of changes.

I therefore have two consecutive commits.
I would like to produce a git merge style file with the conflicts between the two so that I can edit and then resolve the conflict.

Git assumes it can just use all the newer text and discard the old.
I have tried combinations of git merge and git merge -no-ff but can't really work out what to do.

Nor am I sure what search terms to use.

Update:

Just to be clear, what I wanted to end up with is a file with git merge conflicts in it. I can easily see what the diff is between the two files using git diff but the output of that is not then easy to edit. I thought git might have an easy way to say "these two commits conflict in this way..." since it must be able to do that at some level.


If you just try to merge the two commits, then Git will (correcly) recognize that one is based on the other and will not end up creating a conflict for it. After all, for Git, the history looks like this:

                   master
                     ↓
* ---- X ---- A ---- B

B is a commit that changes something that A had. Git cannot know that you created B while looking at the file state from version X .

So you have to change this fact, so Git will use X as the base of both A and B . So let's create a branch for the base first:

git branch base X

This results in this:

      base         master
       ↓             ↓
* ---- X ---- A ---- B

Now, we switch to base and checkout the file from B and commit that change (this essentially copies the state of the file from B )

git checkout base
git checkout master -- file.ext
git add file.ext
git commit
                   master
                     ↓
* ---- X ---- A ---- B
        
         
          C
          ↑
         base

Now you can merge A in and produce a conflict as you desired:

git merge A

After solving the conflict, this results in this:

                   master
                     ↓
* ---- X ---- A ---- B
              
               
          C ---- M
                 ↑
                base

At this point, you have the desired state for the file. You now have three options:

  • Merge the base into master to keep the whole history we created artificially. For the conflict for the file (which you will receive here), you could use the theirs strategy to just keep the state of the solved solution.
  • Copy the file contents, switch to master , and make a new commit with the merged content after B .
  • Reset master to M (throwing away B ), and just use this new created history to continue. Of course this removes the commit B so it essentially removes history (like a rebase) which you should avoid if you already published B .

  • Since you are just trying to merge a single file, you're probably better of just creating a copy of each state, and merging it manually (or with some merge toool).


    If you made 2 commits after one another and git didn't complain about anything, that means you have no conflicts.

    I suggest you

  • soft reset the 2nd commit
  • diff against the 1st commit to see what changes the 2nd commit introduces
  • keep/discard the changes as you see fit and add / rm them
  • amend the 1st commit.
  • This way you will end up with one commit that contains exactly the changes you want it to contain.

    In commands, it would be like this

    $ git checkout sha-commit-2  # or branch name, depends on how your tree looks
    $ git reset HEAD^  # soft reset, remove commit but keep changes
    $ git diff  # diff against previous commit (should be the first one you mention)
    --make your changes and add them--
    $ git commit --amend
    

    If you only did the commit and you haven't pushed your changes you can revert the commit that you have done, here:

    How to undo last commit(s) in Git?

    you will find how to undo your last commit:

    $ git commit -m "Something terribly misguided"
    $ git reset --soft HEAD~
    << edit files as necessary >>
    $ git add ...
    $ git commit -c ORIG_HEAD
    

    Another similar info with other amenities How do I reverse a commit in git?

    If the usage of git it is complicated for you an alternative is: place yourself in the branch where you edited the file, copy and save the edited file in another folder eg. "/home/your_file_here", then change your branch to master and move your edited file "/home/your_file_here" to master. Then you will have the file updated and you can check the changes using "git diff your_edited_file".

    After updating your file then you can just delete the branch where you modified the file doing "git branch -d branch_to_be_removed". Additional info here: How do I delete a Git branch both locally and remotely?

    So my recommendation is that instead of merging the two commits that you have, you should undo one of them, in this case the one that you have done in master. If you want to read more about 'merging' you will find really interesting info here: Git-Branching-Basic-Branching-and-Merging and here: Git-Tools-Advanced-Merging

    链接地址: http://www.djcxy.com/p/23462.html

    上一篇: 将两个提交合并为一个,并推送到远程回购

    下一篇: git合并两个提交