Search code examples
gitmergecherry-pick

Git - how to merge a branch, but ignore one of the changes


I have two C# solutions. One is a nuget project - a library (CodeFirstWebFramework) that the other solution (AccountServer) uses.

On my main Account Server branch, the solution contains just AccountServer, with a nuget reference to CodeFirstWebFramework.

However, I have lately been making extensive changes to CodeFirstWebFramework (CFWF), and I wanted to test them against AccountServer (AS). Because of the way nuget works, every time I want to test a change to CFWF, I have to increment the build number, then update the package in AS.

I found an easier way of working was to remove the nuget reference, and include the CFWF project in the AS solution. I did this in a branch, because I have other people using the code, and I want them to be able to continue using nuget.

When I have completed testing, I wanted to merge all the changes to AS back to the main branch, excluding the one commit which changed the solution file.

I know I can use cherry pick to do this, but I hope to use this again, so I would like to leave git in a state where it thinks it has merged the two branches, so future merges just carry on.

What I did was to merge the branch, then revert the one commit. However, I am now in the situation where I have new changes to AS master, that I want to merge back in to the branch, so I can create and test some more changes to CFWF. So I am in the same situation - I want to merge master into the branch, excluding the commit that reverted the solution change.

I this even possible?

Master------------------------------merge-revert change solution-change code
 \                                  /                                       \
  Branch-change solution-change code----------------------------------------???

Solution

  • When you want to merge and exclude a given change (in contents of files), you can run git merge --no-commit, then edit the file in the ways you want (the change will be applied, so you can edit the file the way you want) and then wrap it up with git add the-file; git merge --continue. This way, the offending commit will remain in history, of course.

    If you would rather have that commit removed from history (like it never happened in the first place) then rewrite that branch so that it looks like the commit was never carried out. Rewriting history is always technically possible but you should be careful not to rewrite commits that have already been shared and used by other people. In general, in a feature branch that no one else is using, it's ok to rewrite history to remove such a commit. The easy way to do it is by using git rebase -i:

    git rebase -i <commit-to-delete>~ # careful: keep the pigtail!!!
    

    You will get a list of commits and the first one should be the one you want to delete..... just comment out that line, save, exit and let git run it's magic. When finished, the commit should no longer be in the history of the branch.... follow your usual from this point.