Getting changes from a remote repository
You can use common Git commands to access remote repositories.
In this article
Options for getting changes
These commands are very useful when interacting witha remote repository.clone
andfetch
download remote code from a repository's remote URL to your local computer,merge
is used to merge different people's work together with yours, andpull
is a combination offetch
andmerge
.
Cloning a repository
To grab a complete copy of another user's repository, usegit clone
like this:
$gitclone https://github.com/USERNAME/REPOSITORY.git#Clones a repository to your computer
You can choose fromseveral different URLs when cloning a repository. While logged in to GitHub, these URLs are available on the main page of the repository when you click Code.
When you rungit clone
, the following actions occur:
- A new folder called
repo
is made - It is initialized as a Git repository
- A remote named
origin
is created, pointing to the URL you cloned from - All of the repository's files and commits are downloaded there
- The default branch is checked out
For every branchfoo
in the remote repository, a corresponding remote-tracking branchrefs/remotes/origin/foo
is created in your local repository. You can usually abbreviatesuch remote-tracking branch names toorigin/foo
.
Fetching changes from a remote repository
Usegit fetch
to retrieve new work done by other people. Fetching from a repository grabs all the new remote-tracking branches and tagswithout merging those changes into your own branches.
If you already have a local repository with a remote URL set up for the desired project, you can grab all the new information by usinggit fetch *remotename*
in the terminal:
$git fetch REMOTE-NAME#Fetches updates made to a remote repository
Otherwise, you can always add a new remote and then fetch. For more information, seeManaging remote repositories.
Merging changes into your local branch
Merging combines your local changes with changes made by others.
Typically, you'd merge a remote-tracking branch (i.e., a branch fetched from a remote repository) with your local branch:
$git merge REMOTE-NAME/BRANCH-NAME#Merges updates made online with yourlocal work
Pulling changes from a remote repository
git pull
is a convenient shortcut for completing bothgit fetch
andgit merge
in the same command:
$git pull REMOTE-NAME BRANCH-NAME#Grabs online updates and merges them with yourlocal work
Becausepull
performs a merge on the retrieved changes, you should ensure thatyour local work is committed before running thepull
command. If you run intoa merge conflictyou cannot resolve, or if you decide to quit the merge, you can usegit merge --abort
to take the branch back to where it was in before you pulled.