Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move branch pointer to different commit without checkout

Tags:

git

git-branch

To move the branch pointer of a checked out branch, one can use the git reset --hard command. But how to move the branch pointer of a not-checked out branch to point at a different commit (keeping all other stuff like tracked remote branch)?

like image 303
Mot Avatar asked Mar 29 '11 10:03

Mot


People also ask

What is the git command to move the branch pointer to a different commit without checkout?

To move the branch pointer of a checked-out branch, one can use the git reset --hard command.

How do I move a branch head to a previous commit?

Make sure you are on the branch to which you have been committing. Use git log to check how many commits you want to roll back. Then undo the commits with git reset HEAD~N where “N” is the number of commits you want to undo. Then create a new branch and check it out in one go and add and commit your changes again.

How do I move a master to another commit?

Use git branch <branch> to create a new branch at the tip of the current master . Use git reset HEAD~<n> --hard to rewind back <n> commits and discard changes. Use git checkout <branch> to switch to the new branch. Only works if the changes have only been committed locally and not pushed to the remote.

What does git branch F do?

This command moves the master pointer to the sub-branch pointer.


2 Answers

git branch --force <branch-name> [<new-tip-commit>] 

If new-tip-commit is omitted, it defaults to the current commit.

new-tip-commit can be a branch name (e.g., master, origin/master).

like image 138
Chris Johnsen Avatar answered Oct 21 '22 21:10

Chris Johnsen


You can do it for arbitrary refs. This is how to move a branch pointer:

git update-ref -m "reset: Reset <branch> to <new commit>" refs/heads/<branch> <commit> 

where -m adds a message to the reflog for the branch.

The general form is

git update-ref -m "reset: Reset <branch> to <new commit>" <ref> <commit> 

You can pick nits about the reflog message if you like - I believe the branch -f one is different from the reset --hard one, and this isn't exactly either of them.

like image 21
13 revs, 11 users 21% Avatar answered Oct 21 '22 20:10

13 revs, 11 users 21%