Git Cheatsheet (Basics)

Glossary

Keywords Description
git Open-source distributed version-control system, used to store code in repositories
GitHub, GitLab and Bitbucket Platform for hosting and collaborating on Git repositories
staging Proposed files/directories that you'd like to commit
commit Saving all staged files/directories to your local repository
branch An independent line of development, so you can develop features isolated from each other. Master branch is the default.
clone Local version of a repository, including all commits and branches
remote Common repository on eg. Github that all team members to keep that changes in sync with
fork Copy of a repository owned by a different user
pull request A method of submitting contributions to a repository
HEAD Represents your current working directory

Configuration

Key/Command Description
git config --global user.name [name] Set author name to be used for all commits
git config --global user.email [email] Set author email to be used for all commits
git config color.ui true Enables helpful colorization of command line output

Core Commands

Key/Command Description
git init [directory] Creates new local repository
git clone [repo] Creates local copy of remote repository
git add [directory] Stages specific [directory]
git add [file] Stages specific [file]
git add -A Stages all changed files
git add . Stages new and changed files, NOT deleted files
git add -u Stages changed and deleted files, NOT new files
git commit -m "[message]" Commit everything that is staged
git status Shows status of changes as untracked, modified or staged

Synchronization of Changes

Key/Command Description
git fetch Downloads all history from the remote branches
git merge Merges remote branch into current local branch
git pull Downloads all history from the remote branch and merges into the current local branch
git push Pushes all the commits from the current local branch to its remote equivalent

Tip: git pull is the combination of git fetch and git merge

Undo Changes

Key/Command Description
git checkout -- [file] Replace file with contents from HEAD
git revert [commit] Create new commit that undoes changes made in [commit], then apply it to the current branch
git reset [file] Remove [file] from staging area
git reset --hard HEAD Removes all local changes in working directory
git reset --hard [commit] Reset your HEAD pointer to previous commit and discard all changes since then

Branches

Key/Command Description
git branch [branch] Create a new branch
git checkout [branch] Switch to that branch
git checkout [branch] -b Create and checkout new branch
git merge [branch] Merge [branch] into current branch
git branch -d [branch] Deletes the [branch]
git push origin [branch] Push [branch] to remote
git branch Lists local branches
git branch -r Lists remote branches
git branch -a Lists local and remote branches

History

Key/Command Description
git log Lists version history for the current branch
git log --author=[name] Lists version history for the current branch from certain author
git log --oneline Lists compressed version history for the current branch
git show [commit] Outputs metadata and content changes of the specified commit
git blame [file] Shows who changed what and when in file

Gitignore

You can list files/directories that you want to explicitely exclude from Git in a .gitignore file. This file should be placed at the root of your repository.

It is noted that people usually exclude dependency caches, such as node_modules, system files, such as .DS_Store, among others.

You can see the .gitignore file of this repository or more examples provided by GitHub.

Platforms

The following platforms can be used to host your repositories.

Platform Price
GitHub Free
GitLab Free
Bitbucket Free

Config stuff

Ajouter votre nom dans la config globale :

  git config --global user.name "John Doe"

Vérifier si c’est correct :

  git config --global user.name

Définir votre courriel :

  git config --global user.email "[email protected]"

Afficher le courriel :

  git config --global user.email

Le résultat de ma config cat ~/.gitconfig :

  # Mettre ce fichier ".gitconfig" dans votre home folder.
  [user]
      name = John Doe
      email = [email protected]
  [core]
      editor = vim
      autocrlf = input
  [color]
      status = auto
      diff = auto
  [push]
      followTags = true
  [http]
      sslverify = false
  [url "https://[email protected]"]
      insteadOf = https://10.1.1.11
  [url "https://[email protected]"]
      insteadOf = https://git.example.com
  [credential]
      helper = cache --timeout=3600

Pour afficher toutes les configs perso :

  git config --list

To view the information that you entered, type :

  git config --global --list

How to find out which Git config file is used and how to override settings

  git config --list --show-origin 

Go to the master branch to pull the latest changes from there

  git checkout master

pull, add, commit, push

Download the latest changes in the project This is for you to work on an up-to-date copy (it is important to do every time you work on a project), while you setup tracking branches.

  git pull REMOTE NAME-OF-BRANCH -u

View the changes you've made It's important to be aware of what's happening and what's the status of your changes.

  git status

Add changes to commit You'll see your changes in red when you type "git status".

  git add CHANGES IN RED
  git commit -m "DESCRIBE THE INTENTION OF THE COMMIT"

Send changes to gitlab

  git push REMOTE NAME-OF-BRANCH

Delete all changes in the Git repository, but leave unstaged things

  git checkout .

Pour modifier un package du repo et le repousser

On va chercher les fichiers sur le serveur git.

  git clone -c http.sslVerify=false https://10.1.1.11/myrepo/projet-a.git
# ou
  git clone [email protected]:myrepo/projet-a.git

Après nos modifications, on les ajoute au git local.

  git add .

Commit des changements dans le git local.

  git commit -m "Commentaire sur les changements."
# ou
  git commit --author="John Doe <[email protected]>" -m "Commentaire sur les changements."

Afficher les commits

  git show

Joindre un tag au commit ()

  git tag -a v0.1-6 9fceb02  -m "Version 0.1-6"

On pousse le tout sur le serveur git.

  git push
# ou
  git push origin v0.1-6

On pousse les tags

  git push --tags
# ou seulement un (1) :
  git push origin v0.1-6

Version courte :

  git add .
  git commit --author="John Doe <[email protected]>" -m "Commentaire sur les changements." 
  git show # Pour afficher le no. du commit, par exemple : "commit 0928f8e2bafd1537ad1281beba3b05541251753c".
  git tag -a v0.1-6 0928f8e2bafd1537ad1281beba3b05541251753c -m "Version 0.1-6"  # Assigne le tag au no. du commit.
  git push
  git push origin v0.1-6

Création d'une nouvelle branche

Commande pour créée une nouvelle branche, par exemple "testing_new_code" :

  git branch testing_new_code

On pointe notre environnement/dossier de travail sur cette nouvelle branche :

  git checkout testing_new_code

On fait la procédure normale pour la suite :

  touch nouveau_fichier.txt
  git add .
  git commit -a -m 'Ajout d'un nouveau fichier'
  git push --set-upstream origin testing_new_code

Retourner sur la branche principale :

  git checkout master

Voir toutes les différences entre le commit et l'état du fichier actuel

  git diff

Voir les différences d'un seul fichier

  git diff mon_fichier.txt

Affiche les différences entre deux branches (des commits bien sûr)

git diff master..18.04-experimental

Pour voir les commits et ultiment choisir celui a revenir :

  git log
# ou
  git log --oneline

# Ex. :
  git revert -e 320934b314a82c8ea0e94eeeca5c84e9497231c2

Création d'un nouveau projet

Les commandes ci-dessous sont pour faire un premier commit d’un projet dans le groupe « myrepo » :

  cd <racine_de_votre_projet>           # Exemple : cd /sources/nom_du_projet>
  git init
  git remote add origin https://10.25.24.71/myrepo/<nom_du_projet>.git  # Il doit déjà existé sur le serveur Git.
  git add .
  git commit -m "Initial commit"
  git -c http.sslVerify=false push -u origin master

Un login vous sera demander, c’est le vôtre qu’il faudra entrer.

Pour enlever manuellement le sync d'un fichier à ignorer avant l'ajout d'un .gitignore

  git rm --cached <FILENAME>