Apr 5, 2018

Common Git Commands

Clone a Git repository to your machine

git clone https://github.com/exampleuser/repository-to-clone.git

See a list of branches of the Git repository

To see a list of previously checked-out branches:

git branch

To see a list of previously checked-out and remote branches:

git branch --all

See the current branch

git branch

The current branch has * next to it.

Switch to a branch

git checkout target-branch-name

Mirror a Git Repository in a different Git Location

If you want to mirror a repository in another location, including getting updates from the original, you can clone a mirror and periodically push the changes.

First, we need to create a bare mirrored clone of the repository.

git clone --mirror https://github.com/exampleuser/repository-to-mirror.git

Next, go into the cloned repo, and set a new push location - the mirroed git location.

cd repository-to-mirror.git
git remote set-url --push origin https://github.com/exampleuser/mirrored.git

As with a bare clone, a mirrored clone includes all remote branches and tags, but all local references will be overwritten each time you fetch, so it will always be the same as the original repository. Setting the URL for pushes simplifies pushing to your mirror. To update your mirror, fetch updates and push.

git fetch -p origin
git push --mirror