I have two branches:
local branch (the one which I work with) remote branch (public, only well-tested commits go there)
Recently I seriously messed up my local branch.
How would I replace the local branch entirely with the remote one, so I can continue my work from where the remote branch is now?
I have already searched SO and checking out to the remote branch locally does not have any effect.
Make sure you've checked out the branch you're replacing (from Zoltán's comment). Assuming that master is the local branch you're replacing, and that "origin/master" is the remote branch you want to reset to: git reset --hard origin/master
This updates your local HEAD branch to be the same revision as origin/master, and --hard
will sync this change into the index and workspace as well.
That's as easy as three steps:
Delete your local branch: git branch -d local_branch Fetch the latest remote branch: git fetch origin remote_branch Rebuild the local branch based on the remote one: git checkout -b local_branch origin/remote_branch
git branch -D local_branch
in the first step if your branch is not merged.
I'm kind of surprised no one mentioned this yet; I use it nearly every day:
git reset --hard @{u}
Basically, @{u}
is just shorthand for the upstream branch that your current branch is tracking. For example, this typically equates to origin/[my-current-branch-name]
. It's nice because it's branch agnostic.
Make sure to git fetch
first to get the latest copy of the remote branch.
git reset --hard origin/master
is rejected on my 2.29.2 git executables which print out # fatal: Cannot do hard reset with paths.
@{u}
than it is to figure out how many commits I need to reset back with @~X
.
git branch -D <branch-name>
git fetch <remote> <branch-name>
git checkout -b <branch-name> --track <remote>/<branch-name>
git help branch
says about --track
. When creating a new branch, set up branch.<name>.remote and branch.<name>.merge configuration entries to mark the start-point branch as "upstream" from the new branch. This configuration will tell git to show the relationship between the two branches in git status and git branch -v. Furthermore, it directs git pull without arguments to pull from the upstream when the new branch is checked out.
I fixed this command in the answer. Thanks for raising the point.
git status
, it will report if your local branch is ahead or behind remote branch if you have them associated. Additionally, you can do git pull
(or push
) instead of full git pull <remote> <branch>
if you have already set your branch to track <remote/branch>
.
Replace everything with the remote branch; but, only from the same commit your local branch is on:
git reset --hard origin/some-branch
OR, get the latest from the remote branch and replace everything:
git fetch origin some-branch
git reset --hard FETCH_HEAD
As an aside, if needed, you can wipe out untracked files & directories that you haven't committed yet:
git clean -fd
git clean
command did it for me. git reset hard origin/master
do not wipe out untracked files. Thanks!
The safest and most complete way to replace the current local branch with the remote:
git stash
git merge --abort
git rebase --abort
git branch -M yourBranch replaced_yourBranch
git fetch origin yourBranch:yourBranch
git checkout yourBranch
The stash
line saves the changes that you have not committed. The branch
line moves your branch to a different name, freeing up the original name. The fetch
line retrieves the latest copy of the remote. The checkout
line recreates the original branch as a tracking branch.
Or as a bash function:
replaceWithRemote() {
yourBranch=${1:-`git rev-parse --abbrev-ref HEAD`}
git stash
git merge --abort
git rebase --abort
git branch -M ${yourBranch} replaced_${yourBranch}_`git rev-parse --short HEAD`
git fetch origin ${yourBranch}:${yourBranch}
git checkout ${yourBranch}
}
which renames the current branch to something like replaced_master_98d258f.
git stash pop
in that workflow. If you want to re-apply your stashed files.
It can be done multiple ways, continuing to edit this answer for spreading better knowledge perspective.
1) Reset hard
If you are working from remote develop branch, you can reset HEAD to the last commit on remote branch as below:
git reset --hard origin/develop
2) Delete current branch, and checkout again from the remote repository
Considering, you are working on develop branch in local repo, that syncs with remote/develop branch, you can do as below:
git branch -D develop
git checkout -b develop origin/develop
3) Abort Merge
If you are in-between a bad merge (mistakenly done with wrong branch), and wanted to avoid the merge to go back to the branch latest as below:
git merge --abort
4) Abort Rebase
If you are in-between a bad rebase, you can abort the rebase request as below:
git rebase --abort
You can do as @Hugo of @Laurent said, or you can use git rebase
to delete the commits you want to get rid off, if you know which ones. I tend to use git rebase -i head~N
(where N is a number, allowing you to manipulate the last N commits) for this kind of operations.
The selected answer is absolutely correct, however it did not leave me with the latest commit/pushes ...
So for me:
git reset --hard dev/jobmanager-tools
git pull ( did not work as git was not sure what branch i wanted)
Since I know I want to temporarily set my upstream branch for a few weeks to a specific branch ( same as the one i switched to / checked out earlier and did a hard reset on )
So AFTER reset
git branch --set-upstream-to=origin/dev/jobmanager-tools
git pull
git status ( says--> on branch dev/jobmanager-tools
If you want to update branch that is not currently checked out you can do:
git fetch -f origin rbranch:lbranch
git checkout .
i always use this command to replace my local changes with repository changes. git checkout space dot.
As provided in chosen explanation, git reset is good. But nowadays we often use sub-modules: repositories inside repositories. For example, you if you use ZF3 and jQuery in your project you most probably want them to be cloned from their original repositories. In such case git reset is not enough. We need to update submodules to that exact version that are defined in our repository:
git checkout master
git fetch origin master
git reset --hard origin/master
git pull
git submodule foreach git submodule update
git status
it is the same as you will come (cd) recursively to the working directory of each sub-module and will run:
git submodule update
And it's very different from
git checkout master
git pull
because sub-modules point not to branch but to the commit.
In that cases when you manually checkout some branch for 1 or more submodules you can run
git submodule foreach git pull
git reset --hard
. This adds little value.
git reset --hard
git clean -fd
This worked for me - clean showed all the files it deleted too. If it tells you you'll lose changes, you need to stash.
git fetch origin remote_branch git reset --hard FETCH_HEAD
The ugly but simpler way: delete your local folder, and clone the remote repository again.
Success story sharing
git reset
by default will repoint your current branch and sync the index.--soft
will skip updating the index,--hard
will also sync the workspace. My own experience is using--hard
most of the time, except when I want to undo the last commit (which is justgit reset HEAD^
)git fetch origin remote_branch
master
, so make sure you've checked out the branch you're replacing first.