Reset another branch to the current one without cash

I am writing some scripts for my Git workflow.

I need to reset another (existing) branch to the current one without checking.

Before:

CurrentBranch: commit A OtherBranch: commit B 

After:

  CurrentBranch: commit A OtherBranch: commit A 

Equivalent

  $ git checkout otherbranch $ git reset --soft currentbranch $ git checkout currentbranch 

(Note --soft : I don't want to affect the working tree.)

Is it possible?

+86
git
Oct 19 '09 at 20:56
source share
4 answers

The workflows you cited are not equivalent: when you reset --hard you lose all the changes in the working tree (you can do this reset --soft ).

You need

 git update-ref refs/heads/OtherBranch refs/heads/CurrentBranch 
+70
Oct. 19 '09 at 21:25
source share

Install otherbranch to specify the same latch as currentbranch by running

 git branch -f otherbranch currentbranch 

The -f (force) otherbranch tells git branch yes, I really want to overwrite any existing otherbranch link with a new one.

In the documentation:

-f
--force

Reset if it already exists. Without the -f branch, git refuses to modify the existing branch.

+169
Jul 11 '13 at 22:56
source share

You can synchronize your branches with this command at any time.

 $ git push . CurrentBranch:OtherBranch -f 

Also without replacing this set of commands

 $ git checkout OtherBranch $ git merge CurrentBranch $ git checkout CurrentBranch 

This can be useful when you don’t need to commit all your files to CurrentBranch, and therefore you cannot switch to other branches.

+18
Jul 05 '13 at 14:37
source share

I came here looking for a more flexible solution for setting the state of a branch.

Here is a way to do a full reset. Please make sure you know what you are doing. It may be dangerous.

 git checkout <branchToReset> git reset --hard <branchThatHasCorrectState> git push --force 

branchToReset is now just like branchThatHasCorrectState branchToReset has been updated on the remote repo to have a new state. Everything that was in branchToReset and did not exist in branchThatHasCorrectState no longer exists. You destroy the story, hence the dangerous part.

-5
Apr 16 '18 at 16:25
source share



All Articles