A branch is a pointer to a commit. Local branches are branches present in our local repository, and remote branches are present in the remote repository.
HEAD is also a pointer. The HEAD points to the currently checked-out branch. Or, we can say that the HEAD points to the most recent commit of our current branch. For a remote repository, the HEAD points to the tip of the default branch(master, in most cases).
Let's learn how to reset a local Git branch to match a remote branch.
Before we begin, let's back up our changes before resetting our branch. We do this to ensure that our current changes or commits are safe. In case we want these changes back, or if we want to compare some changes, or something else goes wrong, our work will still be safe.
We will create a new branch pointing to the current state of our repository. After we reset the other branch, we will still have this backup branch to keep our commits safe.
git branch <backup-branch-name>
For example, consider we have a master branch with three commits. The above command adds a new branch pointing to the same commit as the master branch.
Before
A -- B -- C <- master <- HEAD
git branch backup-branch
After
A -- B -- C <- master <- HEAD
^
backup-branch
Next, we need to download or fetch the latest changes from the remote repository.
We will use the Git Fetch command to fetch the latest commits from the remote repo and add them to the remote-tracking branches. A remote-tracking branch tracks the changes of its corresponding remote branch.
git fetch <remote-name>
The remote name will be the origin, in most cases.
For example, consider a remote repository with two branches(master and feature). Also, we have a local repository in which the origin/feature remote-tracking branch is lagging.
Remote Repository
A -- B -- C <- master
\
\
D <- feature
Local Repository
A -- B -- C <- master, origin/master
^
origin/feature
When we run the Git Fetch command, the new commit(Commit D) from the remote repo is added to the origin/feature branch.
git fetch origin
Local Repository
A -- B -- C <- master, origin/master
\
\
D <- origin/feature
Now, we have an up-to-date remote-tracking branch. We need to reset our local branch to match the remote-tracking branch. We will use the Git Reset command with the --hard option to reset our local branch. The --hard option makes sure that all the changes in the working directory and staging area are also reset.
git reset --hard <remote-tracking-branch>
The final step is to clean up some changes that may be present after the reset operation. We will use the Git Clean command.
git clean -xdf
The -x flag will remove all untracked files and ignored build directories. The -d flag removes any untracked directories. The -f flag will overwrite any default Git Clean configurations and clean up the untracked local files.
Let's take an example to understand the above steps.
Consider a remote repository with a single master branch and three commits.
Remote Repository
A -- B -- C <- master <- HEAD
Now, consider our local repo contains five commits. Let's assume that we have added two new commits(D and E) after cloning the repository.
Local Repository
A -- B -- C -- D -- E <- master <- HEAD
^
origin/master
origin/HEAD
We want to reset our local master branch to match the remote HEAD(or the tip of the remote master branch). First, let's create a backup branch.
git branch backup
Local Repository
A -- B -- C -- D -- E <- master <- HEAD, backup
^
origin/master
origin/HEAD
Next, we will run the Git Fetch command to download any new changes. In our case, there are no new changes in the remote repo. However, it is an important step as sometimes we may not be aware of the changes in the remote repo.
git fetch origin
Finally, we will run the Git Reset command. Pass the remote-tracking branch name(origin/master) to the command.
git reset --hard origin/master
Local Repository
A -- B -- C -- D -- E <- backup
^
master <- HEAD
origin/master
origin/HEAD
Let's clean up any other changes.
git clean -xdf
Resetting a local branch to match a remote branch can be done in a few simple steps.