How to Update a Git Clone
This article outlines the steps we can take to update our cloned repository in Git.
Suppose we have a remote repository we forked on GitHub and cloned on our local machine. How can we update our cloned repository with the original remote repository?
Update a Git Clone
We previously forked from a GitHub public repository in the below example. After copying the repository to our personal GitHub account, we cloned the repository to our local machine.
Now, we would like to update our clone with the remote. How do we go about this?
We will start with setting up the upstream repository. In simpler terms, the upstream is the original repository from which we forked.
We must go back to the original repository and copy the repo’s link.
To set up the upstream, we will run the following:
$ git remote add upstream <URL>
Once that is done, you can check if the upstream is present with the git remote
command with a double verbose flag, as shown below:
If there are any changes in the original/central repository, we can bring them to our local repository with the git pull upstream
command.
You will have to include a branch to pull from. In our case, we only have one branch, master
.
We could specify which branch to pull from in the command if we had other branches.
To update our master
, we will run the below command:
$ git pull upstream master
This will fetch and merge the changes to our master
branch.
Git will notify you if there are changes or not, as seen above.
Lastly, we can stage and commit the changes. If merge conflicts are present, you must manually resolve them and commit the changes.
The cycle does not end here. It is only logical to update the fork in GitHub.
This is done by pushing the committed changes. We will run the command below:
$ git push origin master
This will keep your fork and clone up to date with the original repository.
Conclusion
In a nutshell, you can update your Git clone with the central repository by setting up the upstream to your clone, after which you can pull any changes to your local repo.
It is always advisable to update your clone every time you are about to work on a project, especially if you are working on a joint project. This will ensure that you are in sync with other developers.
John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.
LinkedInRelated Article - Git Pull
- Git Checkout VS Pull
- Git Pull VS Merge
- How to Pull Origin Branch Overwrites Master Branch in Git
- Difference Between Git Merge Origin/Master and Git Pull
- Difference Between Git Pull and Git Pull Origin Master
- How to Undo a Git Pull