Set otherbranch to point at the same commit as currentbranch by running
git branch -f otherbranch currentbranch
The -f (force) option tells git branch yes, I really mean to overwrite any existing otherbranch reference with the new one.
From the documentation:
Answer from Colin D Bennett on Stack Overflow-f
--forceReset to if exists already. Without -f git branch refuses to change an existing branch.
Set otherbranch to point at the same commit as currentbranch by running
git branch -f otherbranch currentbranch
The -f (force) option tells git branch yes, I really mean to overwrite any existing otherbranch reference with the new one.
From the documentation:
-f
--forceReset to if exists already. Without -f git branch refuses to change an existing branch.
The workflows you describe are not equivalent: when you perform reset --hard you lose all the changes in the working tree (you might want to make it reset --soft).
What you need is
git update-ref refs/heads/OtherBranch refs/heads/CurrentBranch
this is how i did it with basic Git commands:
git checkout hotfixes
git reset --hard master
git push --force origin hotfixes
of course it's important to notify everyone working on hotfixes. most likely they will have to delete their local copy and start from a fresh one. an alternative, less invasive idea is to create a new branch:
git checkout master
git branch -tb hotfixes-2 # this creates branch `hotfixes-2` from a copy of `master`
git push origin HEAD # this creates `hotfixes-2` on the remote server
You mean you want to push your local master to the remote hotfixes branch? Like this:
git push origin +master:hotfixes
However, this requires that you are allowed to re-write the history on the remote side.
How do I overwrite one branch with another branch?
Switch Git branch without files checkout - Stack Overflow
Ability to move branch pointer to different commit without checkout
What's the difference between "git reset" and "git checkout"? - Stack Overflow
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).
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.
Yes, you can do this.
git symbolic-ref HEAD refs/heads/otherbranch
If you need to commit on this branch, you'll want to reset the index too otherwise you'll end up committing something based on the last checked out branch.
git reset
Using basic git commands only:
This answer is a bit longer than that of Charles, but it consists solely of basic git commands that I can understand and thus remember, eliminating the need to keep looking it up.
Mark your current location (commit first if needed):
git checkout -b temp
Reset (moves) the marker to the other branch without changing working dir:
git reset <branch where you want to go>
now temp and other branch point to the same commit, and your working dir is untouched.
git checkout <branch where you want to go>
since your HEAD is already pointing to the same commit, working dir is not touched
git branch -d temp
Note that these commands are also readily available from any graphical client.
git resetis specifically about updating the index, moving the HEAD.git checkoutis about updating the working tree (to the index or the specified tree). It will update the HEAD only if you checkout a branch (if not, you end up with a detached HEAD).
(actually, with Git 2.23 Q3 2019, this will begit restore, not necessarilygit checkout)
By comparison, since svn has no index, only a working tree, svn checkout will copy a given revision on a separate directory.
The closer equivalent for git checkout would:
svn update(if you are in the same branch, meaning the same SVN URL)svn switch(if you checkout for instance the same branch, but from another SVN repo URL)
All those three working tree modifications (svn checkout, update, switch) have only one command in git: git checkout.
But since git has also the notion of index (that "staging area" between the repo and the working tree), you also have git reset.
Thinkeye mentions in the comments the article "Reset Demystified ".
For instance, if we have two branches, '
master' and 'develop' pointing at different commits, and we're currently on 'develop' (so HEAD points to it) and we rungit reset master, 'develop' itself will now point to the same commit that 'master' does.On the other hand, if we instead run
git checkout master, 'develop' will not move,HEADitself will.HEADwill now point to 'master'.So, in both cases we're moving
HEADto point to commitA, but how we do so is very different.resetwill move the branchHEADpoints to, checkout movesHEADitself to point to another branch.

On those points, though:
LarsH adds in the comments:
The first paragraph of this answer, though, is misleading: "
git checkout... will update the HEAD only if you checkout a branch (if not, you end up with a detached HEAD)".
Not true:git checkoutwill update the HEAD even if you checkout a commit that's not a branch (and yes, you end up with a detached HEAD, but it still got updated).git checkout a839e8f updates HEAD to point to commit a839e8f.
De Novo concurs in the comments:
@LarsH is correct.
The second bullet has a misconception about what HEAD is in will update the HEAD only if you checkout a branch.
HEAD goes wherever you are, like a shadow.
Checking out some non-branch ref (e.g., a tag), or a commit directly, will move HEAD. Detached head doesn't mean you've detached from the HEAD, it means the head is detached from a branch ref, which you can see from, e.g.,git log --pretty=format:"%d" -1.
- Attached head states will start with
(HEAD ->,- detached will still show
(HEAD, but will not have an arrow to a branch ref.
In their simplest form, reset resets the index without touching the working tree, while checkout changes the working tree without touching the index.
Resets the index to match HEAD, working tree left alone:
git reset
Conceptually, this checks out the index into the working tree. To get it to actually do anything you would have to use -f to force it to overwrite any local changes. This is a safety feature to make sure that the "no argument" form isn't destructive:
git checkout
Once you start adding parameters it is true that there is some overlap.
checkout is usually used with a branch, tag or commit. In this case it will reset HEAD and the index to the given commit as well as performing the checkout of the index into the working tree.
Also, if you supply --hard to reset you can ask reset to overwrite the working tree as well as resetting the index.
If you current have a branch checked out out there is a crucial different between reset and checkout when you supply an alternative branch or commit. reset will change the current branch to point at the selected commit whereas checkout will leave the current branch alone but will checkout the supplied branch or commit instead.
Other forms of reset and commit involve supplying paths.
If you supply paths to reset you cannot supply --hard and reset will only change the index version of the supplied paths to the version in the supplied commit (or HEAD if you don't specify a commit).
If you supply paths to checkout, like reset it will update the index version of the supplied paths to match the supplied commit (or HEAD) but it will always checkout the index version of the supplied paths into the working tree.
Say I have a local branch and I make change A, then push it so both the local and remote has change A. I then realize I don't want change A as there was an error in it. I can use git reset to reset my local branch to the commit before change A, but then if I do that and try to push, it doesn't work. This makes sense because technically the local branch is behind the remote branch. And a solution to this is to just make a "revert change A" commit, but I don't even want my commit history to show these two commits since they cancel each other out
Is there a way to do "git reset" a remote branch?
Let's say I'm on branch A and I do some changes on the branch. For some reasons, I need to checkout to branch B. But the problem is I don't wanna commit them yet. I just wanna keep them without commiting them.
I already tried stashing using git add . and then git stash. But I'm not sure if I do it right because after I run git stash, it automatically removes my changes. And when I switch to another branch & then switch back to that branch, all the changes disappeared.
What should I actually do in this situation? Any advice?