When working with other people's repositories, there are four basic Git commands you will need:

  • git clone
  • git fetch
  • git merge
  • git pull

These commands all act on a repository's remote URL.

Clone

To grab a complete copy of another user's repository, you will use git clone, like this:

git clone https://github.com/user/repo.git
# Clones a repository to your computer

When you run git clone, the following actions occur:

  • A new folder called repo is made
  • It is initialized as a Git repository
  • All of the repository's files are downloaded there
  • git clone checks out the default branch (usually called master)
  • git clone creates a remote named origin, pointing to the URL you cloned from

You can choose from several different URLs when cloning a repository. While logged in to GitHub, these URLs are available in the sidebar:

Remote url list

Fetch

Fetching from a repository grabs all the new branches and tags without copying those changes into your repository. You'd use git fetch to look for updates made by other people.

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 using git fetch <em>remotename</em> in the terminal:

git fetch remotename
# Fetches updates made to an online repository

Otherwise, you can always add a new remote.

Merge

Merging combines your local changes with changes made by others.

Typically, you'd merge a branch on your online repository with your local branch:

git merge remotename/branchname
# Merges updates made online with your local work

Pull

git pull is a convenient shortcut for completing both git fetch and git mergein the same command:

git pull remotename/branchname
# Grabs online updates and merges them with your local work

Because pull performs a merge on the retrieved changes, you should ensure that your local work is committed before running the pull command. If you run into a merge conflict you cannot resolve, or if you decide to quit the merge, you can use git merge --abort to take the branch back to where it was in before you pulled.

Further reading