git

Downloading a Remote Repository

git clone <uri>

Initializing Git After Installation

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

Fetch / Push

Basic commands to work with remote repositories directly after cloning.

git pull
git push

Staging

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.

Pushing

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.

Pulling

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

Branches

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

Stash

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

Tags

git tag <text> # marks latest commit with tag

Log

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

Github

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.

Tokens

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

Organizations

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 user.email "accountname@users.noreply.github.com"

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

Config

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

Authentication

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

libsecret

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

SSH

Check ~/.ssh/ for id_rsa and id_rsa.pub. If they don't exist run ssh-keygen -b 4096 -c "name@host.com" 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 github.com
003      IdentityFile ~/.ssh/key-github
004  Host gitlab.com
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 github.com
003      IdentityFile ~/.ssh/key-github
004  Host github-2.com
005      HostName github.com
006      IdentityFile ~/.ssh/key-github-2

And use git clone git@github-2.com:<name>/<repo>.git to set everything up.

Troubleshooting

git config --list --show-origin
ssh -vT git@github.com

https://docs.github.com/en/authentication/troubleshooting-ssh

Backups

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:

https://github.com/isse-augsburg/minibrass/wiki/Setting-up-a-new-git-repo-with-two-remotes