Downloading a Remote Repository

git clone <uri>

Initializing Git After Installation

git config --global <name>
git config --global <email>

Fetch / Push

Basic commands to work with remote repositories directly after cloning.

git pull
git push


Adds files to the staging area where they are considered for future commits

git stage <file, ...>
git add <file, ...>

Commiting Changes

git commit -m <message>

To commit all changes to files that have been added to the staging area at some point in the past:
git commit -a -m <message>

This only saves state of the local repository. git push saves the commit to remote.


With no arguments, pushes current branch to the standard remote repository set when the local repository was cloned from a remote repo (assuming branches match).

git push <remote repo> <branch>

For repositories cloned from Github git push is usually equal to git push origin main.


Fetch changes from remote

git pull

Getting Environment Info

Show how the current local branch differs from remote and lists files not in the staging area.

git status


git checkout <branch> # switch into <branch>, changing state of local files
git checkout -b <branch> # creates new branch before switching
git push -u origin <branch> # push to new branch and set its upstream repo

Merging new branch into main:

git checkout main
git merge <branch>
git branch -d <branch> # deletes branch that is no longer needed


A way to save changes without commiting them, so they don't get lost when switching branches.

git stash
git stash pop # retrieve stashed state on the current branch


git tag <text> # marks latest commit with tag


git log -p -<n> #p=verbose, n=number of commits to print


Public Repositories

Repositories that can be accessed and viewed by anyone.

Private Repositories

Repositories only accessible by the maintainer and users the maintainer has given the rights to.


Can be assigned rights when using git commands. Are created in the web interface and used by users when authenticating.

git config --global credential.helper manager-core


Repositories may be assigned to an organization. They are only accessible to users that belong to the organization.

Hide E-Mail From Commits

  1. Activate setting in web interface account settings
  2. git config ""

Setting Up New Projects

  1. Create repo on git server via web interface
  2. git clone <uri>

If the project already exists on the local machine:

001  cd <project-dir>
002  git init
003  vim .gitignore
004  git add .
005  git commit -m "initial commit"

Then create empty repo on remote (without a README!) and:

001  git remote add origin <uri>
002  git remote -v # to check that previous command worked
003  git branch -M main # sets up main branch
004  git push -u origin main # remote origin is set to default upstream for current branch


Omitting --global writes configuration to the current repository's .git/config file. Has precedence over global configuration.


Two ways to prevent having to repeatedly give credentials on each command: libsecret for HTTPS and using SSH instead.


001  sudo apt install libsecret-1-0 libsecret-1-dev
002  cd /usr/share/doc/git/contrib/credential/libsecret
003  sudo make
004  git config --global credential.helper /usr/share/doc/git/contrib/credential/libsecret/git-credential-libsecret


Check ~/.ssh/ for id_rsa and If they don't exist run ssh-keygen -b 4096 -c "" to create a pair of keys.

If switching from HTTPS authentication to SSH simply adjust the addresses in <repo>/.git/config:

https://<host> togit@host`

And adjust ~/.ssh/config if multiple keys are required (e.g. when using a range of hosts):

001  # .ssh/config
002  Host
003      IdentityFile ~/.ssh/key-github
004  Host
005      IdentityFile ~/.ssh/key-gitlab

Remember key files require chmod 600.

When using more than one account on the same git server:

001  # .ssh/config
002  Host
003      IdentityFile ~/.ssh/key-github
004  Host
005      HostName
006      IdentityFile ~/.ssh/key-github-2

And use git clone<name>/<repo>.git to set everything up.


git config --list --show-origin
ssh -vT


git clone --mirror <uri> <repo-backup>

Multiple Remote Repositories

001  git remote add <name>
002  git push <name> --all # push all branches to the new remote repo

To push to many remote repos at once: