Skip to content

Instantly share code, notes, and snippets.

@seb-martin
Last active February 10, 2018 16:21
Show Gist options
  • Save seb-martin/a3865168bdcd9ae2c4c6 to your computer and use it in GitHub Desktop.
Save seb-martin/a3865168bdcd9ae2c4c6 to your computer and use it in GitHub Desktop.
Cheat Sheets

Git Cheat Sheet

Basics

Configure tooling

Configure a user information for all local repositories.

Action Command
Lists configuration git config --list
Displays a configuration information git config user.name
Sets the name you want attached to your commit transactions git config --global user.name "[name]"
Sets the email you want attached to your commit transactions git config --global user.email "[email address]"
Enables helpful colorization of command line output git config --global user.ui auto

Les paramètres globaux peuvent aussi être consultés/modifiés dans le fichier ~/.gitconfig.

Create repositories

Start a new repository or obtain one from an existing URL

Action Command
Creates a new local repository with the specified name git init [project-name]
Downloads a project and its entire version history git clone [url]

Make changes

Review edits and craft a commit transaction

Action Command
Lists all new or modified files to be commited git status
Shows file differences not yet staged git diff
Snapshots the file in preparation for versioning git add [file]
Snapshots all the files in preparation for versioning git add *
Shows file differences between staging and the last file version git diff --staged
Unstages the file, but preserve its content git reset [file]
Records file snapshots permanently in version history git commit -m "[descriptive message]"

Group changes

Name a series of commits and combine completed efforts.

Action Command
Lists all local branches in the current repository git branch
Creates a new branch git branch [branch-name]
Switches to the specified branch and updates the working directory git checkout [branch-name]
Creates and switches the specified branch git checkout -b [branch-name]
Combines the specified branch's history into the current branch git merge [branch-name]
Deletes the specified branch git branch -d [branch-name]

Refactor filenames

Relocate and remove versioned files.

Action Command
Delete the file from the working directory and stages the deletion git rm [file]
Removes the file from version control but preseves the file locally git rm --cached [file]
Changes the file name and prepares it for commit git mv [file-orgininal] [file-renamed]

Supress tracking

Exclude temporary files and path.

A texte file named .gitignore suppresses accidental versioning of files and paths matching the specified patterns.

*.log
build/
temp-*
Action Command
Lists all ignored files in this project git ls-files --other --ignored --exclude-standard

Save fragments

Action Command
Temporarily stores all modified tracked files git stash
List all stashed changesets git stash list
Restore the most recently stashed changeset git stash apply
Restore the specified stashed changeset git stash apply stash@[num]
Restore and removes the most recently stashed changeset git stash pop
Discards the most recently stashed changelist git stash drop
Discards the specified stashed changelist git stash drop stash@[num]

Review history

Browse and inspect the evolution of project files.

Action Command
Lists version history for the current branch git log
Lists version history for a file, including renames git log --follow [file]
Shows content differences between two branches git diff [first-branch]...[second-branch]
Outputs metadata and content changes of the specified commit git show [commit]

Redo commit

Erase mistakes and craft replacement history

Action Command
Undoes all commits after [commit], preserving changes locally git reset [commit]
Discards all history and changes back to the specified commit git reset --hard [commit]
Replaces working copy with latest from HEAD git checkout -- <filename>

Synchronize changes

Register a repository bookmark and exhange version history.

Action Command
Downloads all history from the repository bookmark git fetch [bookmark]
Combines bookmark's branch into current local branch git merge [bookmark]/[branch]
Uploads all local branch commits to remote repository git push [alias] [branch]
connect local repository to remote repository git remote add origin <server>
update local repository with remote changes git pull

A successful Git branching model

Détail sur le modèle : A successful Git branching model

A successful Git branching model

Feature branches

Creating a feature branch

git checkout -b myfeature develop

Incorporating a finished feature on develop

git checkout develop
git merge --no-ff myfeature
git branch -d myfeature
git push origin develop

Release branches

Creating a release branch

git checkout -b release-1.2 develop
./bump-version.sh 1.2
git commit -a -m "Bumped version number to 1.2"

Finishing a release branch

git checkout master
git merge --no-ff release-1.2
git tag -a 1.2

git checkout develop
git merge --no-ff release-1.2

git branch -d release-1.2

Hotfix branches

Creating the hotfix branch

git checkout -b hotfix-1.2.1 master
./bump-version.sh 1.2.1
git commit -a -m "Bumped version number to 1.2.1"

git commit -m "Fixed severe production problem"

Finishing a hotfix branch

git checkout master
git merge --no-ff hotfix-1.2.1
git tag -a 1.2.1

git checkout develop
git merge --no-ff hotfix-1.2.1

git branch -d hotfix-1.2.1

Commit

Amender le dernier commit

Il est possible d'amender le dernier commit pour en modifier le message.

git commit --amend

ou

git commit --amend -m "Nouveau message"

Voir Edit an incorrect commit message in Git

Tagging

Action Command
create tag git tag <tag> <commit ID>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment