Skip to content

Instantly share code, notes, and snippets.

@NicHub
Last active February 15, 2024 12:20
Show Gist options
  • Save NicHub/073d343103254c317ae2 to your computer and use it in GitHub Desktop.
Save NicHub/073d343103254c317ae2 to your computer and use it in GitHub Desktop.
Git-Cheat-Sheet

GIT CHEAT SHEET

Other Cheat Cheet

GUI

FIRST-TIME GIT SETUP

On Windows, set VS Code as your default editor

git config --global core.editor "code --new-window --wait"

Settings are stored in ~/.gitconfig Direct editing :

git config --global --edit

My config https://gist.github.com/NicHub/df87a397567afa9d9107

GETTING HELP

git help <verb>
git <verb> --help
man git-<verb>
git help config

IGNORING FILES

Create .gitignore file

# exclude everything
/*
# except README.TXT
!README.TXT

INITIALIZING A REPOSITORY IN AN EXISTING DIRECTORY

git init
git add *.c
git add README.TXT
git commit -m 'initial project version'

CHECKING THE STATUS OF YOUR FILES

git status

TRACKING NEW FILES

git add README.TXT

STAGING MODIFIED FILES

git add README.TXT

COMMITTING YOUR CHANGES

git commit -m "comment"
git commit -a -m "comment" # add modified and deleted files

REVERT FILES TO STAGED VERSION

git checkout myfile  # Revert one file
git checkout -f   # Revert all files + recover deleted files

REVERT FILES TO LAST COMMITED VERSION

git reset --hard HEAD

GET OLD VERSION (ALL FILES)

git lg
git checkout master~2  # 2 revisions back
git checkout 1cc8070   # absolute
git checkout master    # To come back to latest

GET OLD VERSION (ONE FILE)

git lg
git checkout master~2 test.md  # 2 revisions back
git checkout 1cc8070 test.md   # absolute
git checkout master test.md    # To come back to latest

SWITCH TO ANOTHER BRANCH

git checkout master
git checkout NewBranch

GET THE LIST OF FILES THAT DIFFER BETWEEN BRANCHES

git diff --name-status master..dev

HISTORY OF GETTING OLD REVISIONS

git reflog

REMOVE FILE FROM INDEX BUT KEEP IT ON DISK

git reset README.TXT

git rm -r --cached DIRECTORY
git commit -m "cleanup"

DIFF CHANGES

git diff # CLI
git difftool # Diff tool defined in ~/.gitconfig
git lg
git difftool ba23d5b f8cdaad README.TXT

DIFF CHANGES ON A FILE IN TWO DIFFERENT BRANCHES

git difftool branch1 master -- myfile.md

CREATE A BRANCH AND SWITCH DIRECTLY TO IT

git checkout -b NewBranch

COMPARE TWO BRANCHES

git diff master NewBranch
git difftool master NewBranch

COMPARE TWO REVISIONS OF A FILE

GITFILE=path/file.ext
git lg $GITFILE
git difftool c350f90:$GITFILE 9231779:$GITFILE

git difftool HEAD~1 HEAD $GITFILE
git difftool HEAD~2 HEAD~5 $GITFILE

DEFINE ALIAS FOR GIT LOG

nano ~/.gitconfig 

[alias]
	lg = log --pretty=format:'%C(yellow)%h %Cred%ad %Cblue%an%Cgreen%d %Creset%s'

SHOW BRANCH HISTORY

git log
git lg # Must be defined as alias in ~/.gitconfig 

SHOW BRANCH HISTORY FOR ONE FILE

git lg README.TXT

RENAME FILE

git mv README.TXT README.BAK

MERGE

http://www.git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging

# Crée une branche
git checkout -b newbranch
git commit -a -m "my comment"
# Merge la branche
git checkout master
git merge newbranch

GIT BOOK

Create a book on GitBook.com and make some changes Then on the local machine

git clone https://git.gitbook.com/nichub/ajeter.git
cd ajeter
git remote add gitbook https://git.gitbook.com/nichub/ajeter.git

Make some change localy and push them to the server

git commit -a -m "update"
git push -u gitbook master

Or make some change on the server and pull them localy

git pull -u gitbook master

GIT HUB

git pull -u origin master
git push -u origin master

LE REMISAGE — STASH

git stash
git stash list

Pour revenir à l’état d’origine :

git stash apply --index # Avec index
git stash apply         # Sans index

AFFICHER L’URL DU DÉPOT SOURCE

git config --get remote.origin.url

AFFICHER LES FICHIERS AFFECTÉS PAR UN COMMIT

git show HEAD --compact-summary
git show HEAD~1 --compact-summary

GLOBAL GITIGNORE

git config --global core.excludesfile ~/.gitignore

git config --global core.excludesfile "%USERPROFILE%\.gitignore"

SQUASH DEV AND MERGE TO MASTER

    branch1=master
    branch2=dev
    msg="A lot of new stuff"

    git checkout $branch2
    git lg -11
    git status
    ID=$(git log ${branch1}..${branch2} --pretty=format:%h | tail -1)
    echo $ID
    git reset ${ID}
    git add -A
    git status
    git commit --amend -m "${msg}"
    git status
    git checkout $branch1
    git lg -11
    git merge $branch2
    git branch --delete $branch2
    git checkout -b $branch2
    git lg -11
    git status

Ignore file changes

git update-index --assume-unchanged "_config.yml"
git update-index --no-assume-unchanged "_config.yml"

## Clone distant branch

git branch -r
git checkout --track origin/branch_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment