GIT

Mandatory understanding

  • https://qph.ec.quoracdn.net/main-qimg-584210cfd8ba45339fbcdd77b4a07e48?convert_to_webp=true
  • http://rogerdudler.github.io/git-guide/
  • https://git-scm.com/doc
  • https://dzone.com/refcardz/getting-started-git
  • https://www.quora.com/What-is-the-best-Git-cheat-sheet”

Commit hash aliases

HEADThe current committed version
HEAD^
HEAD~1
One commit ago
HEAD^^Two commits ago
:/鈥漅eformatting all鈥Nearest commit whose comment begins with 鈥淩eformatting all鈥
RELEASE-1.0Tag applied to the code when it was certified for release.

Defaults

git config --global color.ui "true"
git config --global format.pretty oneline "true"
git config --global merge.tool meld
git config --global merge.tool.cmd "meld \"$BASE\" \"$LOCAL\" \"$REMOTE\" \"$MERGED\""
git config --global merge.trustExitCode false
git config --global diff.external git-meld

Aliases

git config --global --replace alias.unstage "reset HEAD ---"
git config --global --replace alias.revert-file "checkout --" # filename
git config --global --replace alias.revert-commit "reset --soft HEAD^"
git config --global --replace alias.revert-permissions "git ls-tree -r HEAD | perl -ne '/^10(0\d{3}) blob \S+\t(.+)$/ && { system \"chmod\",\"$1\",\"$2\" || die }'"
git config --global --replace alias.revert-merge "git revert -m 1 <merge-commit>"
git config --global --replace alias.current-branch "rev-parse --abbrev-ref HEAD"
git config --global --replace alias.unmerged "branch -v --no-merged"
git config --global --replace alias.unmerged-all "branch -v --no-merged -all"
gi config --global --replace alias.unmerged-develop "branch -v --no-merged develop"
git config --global --replace alias.unmerged-staging "branch -v --no-merged staging"
git config --global --replace alias.tree "log --graph --oneline --format=\"%h %s %an (%cr, %ci)\"" # --all
git config --global --replace alias.clean-all "git clean -df"
git config --global --replace alias.unstash "stash show -p | git apply -R"
git config --global --replace alias.unstash "stash show -p | git apply -R"
git config --global --replace alias.unstash "stash show -p | git apply -R"
git config --global --replace alias.unstash "stash show -p | git apply -R"
git config --global --replace alias.merge-interactive "merge --no-commit --no-ff"
git config --global --replace alias.merge-abort "merge --abort"
git config --global --replace alias.upstreams "for-each-ref --format='%(refname:short) <=> %(upstream:short)' refs/heads"
git config --global --replace alias.history "log --date=format:'%Y-%m-%d %H:%M:%S %z' --pretty='format:%an %ar (%ad)%n%Cgreen  %s%n%Cblue  %H  <%ae>%n' --graph"
git config --global --replace alias.deleted "log --diff-filter=D --summary"
git config --global --replace alias.upstream "rev-parse --abbrev-ref --symbolic-full-name @{u}"
git config --global alias.track '!f() { ([ $# -eq 2 ] && ( echo "Setting tracking for branch " $1 " -> " $2;git branch --set-upstream $1 $2; ) || ( git for-each-ref --format="local: %(refname:short) <--sync--> remote: %(upstream:short)" refs/heads && echo --Remotes && git remote -v)); }; f']]>

Revert accidently chmod 777 on git checkout

#!/bin/bash
TMP=$( tempfile )
TMP_REVERT=$( tempfile )

set +e

git diff -p -R --no-ext-diff --no-color \
| grep -E -B 1 -A 1 '^(old mode)' > $TMP

git diff -p -R --no-ext-diff --no-color \
| grep -E -B 1 -A 1 '^(old mode)' \
| sed -e 's/^old/NEW/;s/^new/old/;s/^NEW/new/' > $TMP_REVERT

set -e

if [ ! -s "$TMP" ]; then
  echo Nothing to do.
  exit 0
fi

cat $TMP

echo
echo "We only revert file permissions. If you see other diff please cancel!"
read -r -p "Apply this changeset? [y/N] " response
if [ -n "$( echo $response | grep -E "[yY][eE][sS]|[yY]" )" ]; then
  cat $TMP | git apply # --stat
  echo "Reverted file permissions. If you want to undo changes use $ cat $TMP_REVERT | git apply -R!"
fi

Snippets

Fast checkout single branch with history truncated

git clone -b <remote-branch> -depth 1 <repository-path> <local-target-directory>

Interactive merge without fast-forward and wihtout committing

git merge <remote/branch> --no-commit --no-ff   

Abort merge

git merge --abort

Checkout new branch from remote

git checkout -b <local-branch> <remote/tracking-branch>

Delete local branch

git branch -d <local-branch>

Revert single file

git reset HEAD <filename>
git checkout -- <filename> 

Create a new tag

git tag

Publish a new local branch to remote origin

git push -u origin # -u created new remote branch

Start bisect and run your analysis

git bisect start

but before tell bisect how to operate

git bisect bad
git bisect good

or automatically run you analysis

git bisect run

bisect log

git bisect log

Abort bisect

git bisect reset

Merge commit from other branch

git cherry-pick

Add remote (if forgot)

git remote add origin git@gutemine.rfl.lab:gui/gui.git

Revert a specific merge commit

git revert -m 1

Cherry-pick a merge commit

git cherry-pick -m 1

Delete a remote branch (“:” is important!)

git push origin :

Delete a local branch

git branch -D

Sync branch list

git fetch -p

Rename current branch

git branch -m

Rename other branch

git branch -m

Push to a new remote

git push -u origin

Restore deleted branch

$ git fsck –full –no-reflogs –unreachable –lost-found > lost
$ cat lost | cut -d” ” -f3 > commits
$ cat commits | xargs -n 1 git log -n 1 –pretty=oneline > lostcommits

Now look for the git commit id (GIT-SHA) based on the commit comments and use it in the command below. Checkout a new branch called NEW-BRANCH with the previously found GIT-SHA:

$ git checkout -b NEW-BRANCH GIT-SHA

Remove local tag

git tag -d 12345

Remove remote tag

git push origin :refs/tags/12345

Push all tags

git push –tags

Add a new tag with comment

git tag -a v1.4 -m ‘$( date +%Y_%m_%dT%H_%M_%SZ)’

Merge, no FF, no commit – good for manual merging

git merge origin/dev-enhancements –no-commit –no-ff

Apply dev-enhancements to current branch

Merge, without commit, or prepare single commit for changes isntead of branch merge commit

git merge origin/dev-enhancements –no-commit –squash

Shallow clone without history

git log –date=format:’%Y-%m-%d %H:%M:%S %z’ –pretty=’format:%Cgreen%an %ar (%ad)%n %s%n%Cblue %H <%ae>%n’
git clone –depth 1 -b viq_theming git@gutemine.rfl.lab:gui/gui.git viq-theming-cleaned
git remote add clonable git@gutemine.rfl.lab:viq-cloneable-projects/gui.git

Reset all local merges

$ git fetch origin
$ git reset –hard origin/staging
$ git-wtf
Local branch: staging
[x] in sync with remote
Remote branch: origin/staging (git@gutemine.rfl.lab:gui/gui.git)
[x] in sync with local

Partially apply git patch

$ git apply –reject hasconflicts.patch

Show only fioles which were added (A) or mdoifeid (M) last 1 week

git whatchanged –diff-filter=A –name-only –pretty=”” –since=”1 weeks ago” —

Show changes on a file

git blame index.php

Set new upstream for a branch

git push –set-upstream scm IQI-400

Set new remote url

git remote set-url scm git@git@gitlab.com:blurayne/cronan.git

Add new remote scm

git remote add scm git@gitlab.com:blurayne/cronan.git

set default upstream

git branch –set-upstream-to myfork/master

Prune local branches without upstream (delete remote branches first!)

git fetch scm -p && for branch in LC_ALL=C git branch -vv | grep ': gone]' | awk '{print $1}'; do git branch -D $branch; done

Book, cr-at-eol: https://git-scm.com/book/tr/v2/Customizing-Git-Git-Configuration
“cr-at-eol, which tells Git that carriage returns at the end of lines are OK.
Git will detect these issues when you run a git diff command and try to color them so you can possibly fix them before you commit. It will also use these values to help you when you apply patches with git apply. When you鈥檙e applying patches, you can ask Git to warn you if it鈥檚 applying patches with the specified whitespace issues:
$ git apply –whitespace=warn Or you can have Git try to automatically fix the issue before applying the patch:
$ git apply –whitespace=fix

now open merge request @Gitlab

show cherry picks of a branch

git cherry ML3-1092 -v

List commits deleting files

git fsck --lost-found

Restore deleted file

git checkout <deleting_commit>^ -- <file_path>

Resolve binary merge-conflict

git checkout --theirs -- path/to/conflicted-file.txt

Find out when file was deleted

git log --diff-filter=D --summary
git log --all --full-history --summary -- **<file>*
git log --all --full-history -- <file-path>

Revert deleted file

git checkout <commit-where-the-file-was-deleted>~1 -- <deleted-file-or-path>

Import file from another path

git archive markus/develop .viqenv conf | tar -xvf -
git archive markus/develop .viqenv conf | tar -tvf -
git archive origin/IQI-583-docker-dev-config .viqenv .angular-cli.json src/environments/ | tar -tvf -
git archive origin/IQI-583-docker-dev-config .viqenv conf  | tar -tvf -
git archive --format tar -9 --remote ssh://server.org/path/to/git > /tmp/remotearchive.tgz

Working with GIT Patches (git am, git apply, git format-patch etc.)

https://www.devroom.io/2009/10/26/how-to-create-and-apply-a-patch-with-git/

Pitfalls

https://git-scm.com/book/de/v1/Git-Branching-Rebasing