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.
Adds files to the staging area where they are considered for future commits
git stage <file, ...>
git add <file, ...>
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
Getting Environment Info
Show how the current local branch differs from remote and lists files not in the staging area.
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 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
Repositories that can be accessed and viewed by anyone.
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
- Activate setting in web interface account settings
git config user.email "email@example.com"
Setting Up New Projects
- Create repo on git server via web interface
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
--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
id_rsa.pub. If they don't exist run
ssh-keygen -b 4096 -c "firstname.lastname@example.org" to create a pair of keys.
If switching from HTTPS authentication to SSH simply adjust the addresses in
~/.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
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
git clone email@example.com:<name>/<repo>.git to set everything up.
git config --list --show-origin
ssh -vT firstname.lastname@example.org
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: