In git, I have a branch A
. I create another branch B
where I create few commits. Then I want to fast forward branch A
to branch B
.
I know that I can checkout A
and do fast forward to the latest commit of B
. This operation requires two modifications of working copy: the first to return to A
and then revert working copy to B
. If new commits on B
contain a lot of changes, the operation could be quite slow.
Is it possible to do fast forward another branch without changing the working copy? (in other words, to move just the pointer of A
)
When using git, I sometimes want to fast-forward some branchA , but currently I'm on branchB . The simplest way to do this is to checkout branchA , do a pull (or another command to fast-forward branchA ), and then to checkout branchB again.
You could use git checkout from the branch you want to transfer the changes to: git checkout <branch name> . That will change all files to match the version in the desired branch. Then you can commit, change, discard whatever you want.
When you try to merge one commit with a commit that can be reached by following the first commit's history, Git simplifies things by moving the pointer forward because there is no divergent work to merge together – this is called a “fast-forward.”
You can use the git switch - command to undo any changes you make and return to your previous branch. If you instead want to keep your changes and continue from here, you can use git switch -c <new-branch-name> to create a new branch from this point.
Git provides the update-ref command for this purpose
git update-ref A B
Note: This command is one of the lower-level Git commands and does not provide any safeguards for you to keep from screwing yourself up. If B is not a fast-forward of A it will still update A, potentially making commits unreachable and subject to garbage collection.
If you want to fast forward move another branch to the current HEAD, you can also:
git fetch . HEAD:another-branch
Unfortunately this doesn't work with commit IDs like:
git fetch . TARGET_ID:another-branch
But it works with branch or tag names.
So, my solution is to create a temporary target branch:
git branch target <your-target-ref-id>
git fetch . target:another-branch
git branch -d target
You could do this in one line:
git branch t <your-target-ref-id> && git fetch . t:another-branch && git branch -d t
<your-target-ref-id> could be a commit id, tag or branch name.
Finally you can put this in a script or git alias.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With