Home > front end >  Git saying no changes when trying to merge
Git saying no changes when trying to merge

Time:03-09

master branch commits:
git log output
removedc1Changes(latest commit)
c2
c1

Now I created a new branch named staging from master using c1 commit hash: git checkout c1

staging branch commits :
git log output
c1

Now if I try to merge staging branch to master, it says no changes..

I know it must be because same commit exists in both branches but in the master branch, it has been reverted..

CodePudding user response:

What you have is this:

                        master
                           v
*---*---*---*---c1---c2---c1r
                ^
             staging

If you want to merge staging into master, git is correct in telling you there is nothing to do.

Everything on staging, this:

*---*---*---*---c1
                ^
             staging

is already on master, here:

 ---- here ------        master
|                |         v
*---*---*---*---c1---c2---c1r

what you need to do instead is to revert the removal of c1, this command:

git revert c1r

(c1r = hash of the commit you call "removedc1Changes")

This will give you this:

                              master
                                 v
*---*---*---*---c1---c2---c1r---c1rr
                ^
             staging

and in this scenario, you will again see the changes introduced by c1.

You don't need the staging branch.

A different approach would be to reset master to before the removal of c1, like this:

git reset --hard c2

This would give you this:

                   master
                     v
*---*---*---*---c1---c2
                ^
             staging

If your scenario is 100% accurate, the commit at the tip of master is the removal of the changes from c1, I would do the reset. If you have other commits after it, I would instead do the revert.


The reason why git merge doesn't do anything is that git doesn't try to merge state, it merges changes. Since no changes was done on staging that also wasn't already on master, there were no changes to merge, hence nothing to do.

CodePudding user response:

"A thing is just the thing it is, and not some other thing." Git is what Git is, and not what you imagine Git to be or wish Git to be. To use Git, you need to know what Git is. A merge, in Git, is a new commit that enacts all changes in both branches since the point of divergence.

The point of divergence between your branches is c1. Since then, one branch has added c2 and removedc1Changes. The other branch has added ... nothing.

Just as Git tells you, your staging branch has no changes on it since the point of divergence (because you have made no new commits on the staging branch). Thus there is nothing to merge.

The only changes since c1 are c2 and removedc1Changes on master, and they are already on master. So there is nothing for a merge to do. The changes enacted by both branches since the point of divergence are already present on master. The only way to perform the merge would be for Git to create a new commit that is effectively empty, ie it is the same as removedc1Changes.

(Here's another way to look at it. It is true, as you say, that both branches contain c1. But since then, master has undone c1, and staging has done nothing to contradict that.)

Thus your pseudomerge is not a viable strategy for undoing the revert you did earlier. The way to undo a revert is to revert it, or to reset hard back to before the revert to erase it.

CodePudding user response:

Judging by the discussion, you want to re-apply the C1 commit changes. I think you can use git cherry-pick of the C1 commit which will make a copy of the original commit, essentially re-applying it.

git cherry-pick <C1 commit sha>

You can do this on your staging branch before merging to master (or creating the PR).

Note this will keep the history as is and just add a new commit on top.

  • Related