Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Lathanao
Last active November 28, 2023 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lathanao/20315f1667cc6ca74b94ad48f566f0ae to your computer and use it in GitHub Desktop.
Save Lathanao/20315f1667cc6ca74b94ad48f566f0ae to your computer and use it in GitHub Desktop.
Git sheet cheat / examples / tips / common setups.

Git sheet cheat

Initialisation

$ git init

Clone without the full history

$ git clone --depth 1

Status

$ git status

Help

$ git help log

Common config variables

$ git config --global user.email "me@mail.com"
$ git config --global user.name "me"
$ git config --global color.ui true
$ git config --global core.fileMode false  // File permission change ignored
$ git config --global diff.renames 0
$ git config --global merge.renameLimit 999999 
$ git config --global --replace-all core.pager "less -F -X"
$ git config --global --add core.pager "less -F -X"

Check changes

$ git config --list --global
$ git config --list --show-origin --show-scope

Edit the configuration

$ git config --edit --global
$ git config --edit

Add to staging (green to red)

$ git add <file>
$ git add .       //stage not staged files
$ git add --all   //stage not staged and deleted files
$ git add *       //stage not staged and untracked files

Add Since Git 2.0

$ git add -u :/	//To stage your whole working tree
$ git add -u .		//To stage just the current path

Add to staging with patern

$ git add modules/\*.jpg
$ git add \*.html

Remove staging (green to red) is the exact oposit to $ git add *

$ git reset * 

Diff

$ git diff HEAD
$ git diff --staged
$ git diff --name-only <SHA1> <SHA2>
$ git diff --name-only <branch-A> <branch-B>

Commit in staging

$ git commit -a -m "First commit"

Check logs

$ git log -n 2
$ git log -2
$ git log -p <file>
$ git log origin/master..HEAD
$ git log origin/master..

Count logs

$ git log --oneline | wc -l

Show branch graph

$ git log --graph --decorate --pretty=oneline --abbrev-commit master origin/master
$ git log --graph --oneline --decorate=full -20 --date=short --format="%C(yellow)%h%C(reset) %C(magenta)[%ad]%C(reset)%C(auto)%d%C(reset) %s %C(cyan)@%an%C(reset)"

Deindex files

$ git rm --cached -r *
$ git rm --cached app/\**/*.xml

Checkout

Restore from staging statging

$ git checkout master
$ git checkout master <file>
$ git checkout master <file>
$ git checkout .

Reset (moving the HEAD pointer)

Delete changes of a file

$ git reset HEAD <file>
$ git reset -- <file>

Delete all changes and return to the last commit

$ git reset --hard

Return to commit

$ git reset <commit>
$ git reset <commit> --soft (leave changes in staging)
$ git reset <commit> --hard (delete changes from staging)

Checkout vs Reset

Feature Git checkout Checkout Reset
Switches branches Yes No
Restores files Yes No
Checks out files from the staging area Yes No
Moves the HEAD pointer No Yes
Moves the staging area No Yes

Revert

Undo a commit

$ git revert e2eb29

Undo - again - a commit

$ git revert revert e2eb29

Solve the detached HEAD problem

https://stackoverflow.com/questions/5772192/how-can-i-reconcile-detached-head-with-master-origin

$ git branch temp
$ git checkout temp
$ git diff master temp
$ git branch -f master temp
$ git branch -d temp
$ git push origin master

Tutorial github

$ git remote add origin http://github.com/try-git/try_git.git
$ git push -u origin master
$ git pull origin master
$ git diff HEAD
$ git add octofamily/octodog.txt
$ git diff --staged
$ git reset octofamily/octodog.txt

Check branch

Check branch

$ git branch

Check commit before push

$ git log origin/master..HEAD

Check commit before push

$ git push -u origin dev

Reset files from repo

Downloads the latest from remote without trying to merge or rebase anything.

$ git fetch --all
$ git fetch https://${GITHUB_USER}:${GITHUB_PASSWORD}@github.com/{GITHUB_USER}/{GITHUB_REPO}.git ${GIT_LOCAL_BRANCH}

Then the '$git reset' resets the master branch to what you just fetched. The --hard option changes all the files in your working tree to match the files in origin/master

$ git reset --hard origin/master
$ git reset --hard FETCH_HEAD

Just for fun :

Push or Pull commits one by one : Push a commit, reset it & push new head to repo

$ git log
$ git push origin <commit>:master
$ git reset --hard <commit>
$ git push -f origin master

Fetch + checkout | merge

$ git fetch <remote> <branch>
$ git log
$ git checkout .

Archives Git

$ git archive --format=zip HEAD -o `date +%Y%m%d`_${PWD##*/}.zip
$ zip -r `date +%Y%m%d`_${PWD##*/}.zip /directory

Update Remote

$ git remote -v
$ git remote set-url origin git@github.com:ACCOUNT/REPO.git

Clean a branch by override

$ git checkout <branch-to-override>
$ git fetch --all 
$ git reset --hard <remote>/<branch-to-copy>

Sync a file between branch, modified in every branch (e.g. css file) Or make a pull-like for only one file

 $ git checkout <remote>/<branch> <file name>
 $ git diff --name-only <branch-A> <branch-B>

The file will be updated, then merge should be without any conflit

Good to know

.gitkeep

.gitkeep isn’t documented, because it’s not a feature of Git.

Git cannot add a completely empty directory. People who want to track empty directories in Git have created the convention of putting files called .gitkeep in these directories. The file could be called anything; Git assigns no special significance to this name.

There is a competing convention of adding a .gitignore file to the empty directories to get them tracked, but some people see this as confusing since the goal is to keep the empty directories, not ignore them; .gitignore is also used to list files that should be ignored by Git when looking for untracked files.

Opposite: Good to know

Staging : "reset" (soft) do the exact opposite to add

$ git reset *

vs

$ git add *

Merge : "theirs" do the exact opposite to ours

$ git merge -X theirs
or
$ git checkout --theirs PATH/FILE

vs

$ git merge -s ours
or
$ git checkout --ours PATH/FILE

Mastering Advanced Git Commands

Firt-parent

$ Git merge --firt-parent

Xargs git

$ find . -name '*.jpg' | xargs git rm

Rebase vs merge vs pull --rebase

pull --rebase is a Git command that fetches the latest changes from the remote repository and then rebases your local branch on top of the remote branch. This effectively combines your local changes with the remote changes without creating a merge commit.
$ git rebase origin/master
vs
$ git merge origin/maste
vs
$ git pull --rebase origin/master

Repack

https://git-scm.com/docs/git-count-objects

$ git count-objects 

https://git-scm.com/docs/git-repack

$ git-repack 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment