Git cheat sheet

Several cheat-sheets of different topics in .md format. Checkout the Github pages version.

Git cheat sheet

Passwords and authentication

Tell git to remember the password in future pushes during 3600 seconds:

git config --global credential.helper cache
git config --global credential.helper 'cache --timeout=3600'

Modify user and email globally:

git config --system user.name "username"
git config --system user.email "mailexample@mail"

Modify user and email for linux user:

git config --global user.name "globalname"
git config --global user.email "mailexampl@mail"

Modify user and email on current git repository:

git config --local user.name "nameproject"
git config --local user.email "mailexample@mail"

Check the user name:

git config --get user.name
git config --get user.email

Private repositories

An ssh key should locally exists, protected with a password (not sure if the same as the GH account), which public key should be added to the list of known keys on the GitHub account. To create a new ssh key pair locally:

ssh-keygen -t ed25519 -C "anemail@email.com"

Everytime we want to interact with the private repo, there should be an ssh session running and using this key, this can be done with:

eval "$(ssh-agent -s)"

ssh-add ~/.ssh/id_rsa_gh_key

It might be useful to setup an auxiliary script such as:

#!/bin/bash

eval "$(ssh-agent -s)"

ssh-add ~/.ssh/id_rsa_github_1

in some directory (e.g. `~/.utilities/setup_github_ssh_key_1.sh, permissions 744).

Further, we could add an alias to invoke the script

cat ~/.bash_aliases

alias setup_gihub_key_1='source ~/.utilities/setup_github_ssh_key_1.sh'

Branches

Check in which branch you are working now:

git status
git branch

Create a new branch and switch to it

git checkout -b new_branch_name

tags

Checkout to a specific tagged version of the code.

git checkout tags/v2.1.3

List all branches

git branch -a

Pull all remote branches

git pull --all

Create a new branch called “develop”

git branch develop

Switch to the branch “develop”

git checkout develop

Create a new branch and switch to it

git checkout -b newbranch

Delete a branch locally

git branch -d branchname

Save workflow

Once you are in the desired branch, change the code and commit the changes

git add filechanged.py
git commit -m "description of change"

If the changes are correct, then merge with master

git checkout master
git merge newbranch

Delete a branch if not needed

git branch -d newbranch

Remove commit

Remove the last commit:

git reset --hard HEAD^

Remove the last 2 commits:

git reset --hard HEAD~2

Without --hard flag the commits are removed but not the changes in the code.

git fetch

Download all branches and commits from origin:

git fetch origin 

Download commits and files from specific branch:

git fetch origin branch_name

Download all registered repositories and branches:

git fetch --all

To check the result of the command without actually applying changes:

git fetch ... --dry-run

Git pull

Checkout changes from master to current branch

git pull origin master(main)

Checkout changes from specific branch to current branch

git pull origin branch_name

Git add

Add changes to specific files:

git add pathToFile

Add changes to current folder and subfolders

git add .

Add changes to all repository

git add -A

Git rm

Remove files added with git add:

git rm --cache pathtofile

or remove a complete folder

git rm -rf --cache pathtofolder

Merge a branch to incorporate changes to master

# Update progress in destination branch master
git checkout master
git pull
# Merge feature branch 
git merge FEATURE_BRANCH_NAME

Then, on github create a pull request of the two branches.

.gitignore

Ignore certain files systematically when adding changes creating a .gitignore file and adding the files

file1
file2
*.log
folder1
folder2/*
!folder2/subfolder_or_file_included

Return to main page