Home > Software design >  How can I "git pull" just to sync two branches without actually pulling any file changes?
How can I "git pull" just to sync two branches without actually pulling any file changes?

Time:10-20

Let's say I have branch "A", from which a more advanced branch "B" was created.

Usually all the changes that are implemented on branch "A" should be just pulled into branch "B", but due to some differences between the branches a specific feature was implemented on "A" and "B" separately and in different ways.

What I want to do is to stand on branch "B" and do "git pull origin A", but without actually pulling any file changes, so that git will just think that B and A are synced at this specific point, and any additional "git pull origin A" command will just return that there's nothing pull and no changes detected (until the next commit to branch A).

Is it possible to do so?

CodePudding user response:

The usual pattern I follow here is to first do:

# on branch B
git fetch origin

This will update all your tracking branches, including origin/A, which is the tracking branch for A. Now you may do whatever operation you wanted from the B branch. For example, you could merge A into B:

# still from B
git merge origin/A

Note that you were able to achieve all of this without ever changing branches.

CodePudding user response:

It can be done with a little bit of tweeking. First run the merge and let it finish, as you normally do (any workflow.... pull, merge).

git checkout B
git merge -m "Merging A" A

After doing that, let's get back the content that we had in B before the merge

git restore --staged --worktree --source=@~ -- .
git commit --amend --no-edit
  •  Tags:  
  • git
  • Related