Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to fast forward another branch without checking it out?

Tags:

git

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)

like image 205
TN. Avatar asked Nov 04 '14 14:11

TN.


People also ask

How do you fast-forward in another branch?

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.

How do I pull changes from another branch without merging?

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.

What does it mean to fast-forward a branch?

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.”

How do you switch between branches?

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.


2 Answers

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.

like image 62
Andrew C Avatar answered Oct 23 '22 07:10

Andrew C


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.

like image 42
LaChRiZ Avatar answered Oct 23 '22 05:10

LaChRiZ