Skip to content

Instantly share code, notes, and snippets.

@nntrn
Last active April 24, 2024 05:27
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 nntrn/122df46a065081d039d2bfd849aeeb43 to your computer and use it in GitHub Desktop.
Save nntrn/122df46a065081d039d2bfd849aeeb43 to your computer and use it in GitHub Desktop.
Handy git commands
########################################################################
# _ _ __
# (_) | / _|
# __ _ _| |_ _ __ ___| |_
# / _` | | __| | '__/ _ \ _|
# | (_| | | |_ | | | __/ |
# \__, |_|\__| |_| \___|_|
# __/ |
# |___/ by @nntrn
#
########################################################################
#
# https://gist.github.com/nntrn/122df46a065081d039d2bfd849aeeb43
#
########################################################################
#----------------------------------------------------------------------#
# SPECIFYING REVISIONS
#----------------------------------------------------------------------#
#
# HEAD Current branch latest commit git show HEAD
# HEAD^ Parent of the latest commit git diff HEAD^
# HEAD~ Ancestor of the latest commit git log HEAD~3
# HEAD@{n} Specific ref in reflog git checkout HEAD@{1}
# git show HEAD@{"1 week ago"}
#
# @ shortcut for HEAD
# @~1 previous commit
# @~2 second previous commit
# @~1..@ range between HEAD and previous commit
# @~2..@ range between HEAD and second previous commit
# @~2..@~1 range between previous commit and second previous commit
# @^ parent of the latest commit
#
#
# <sha1> dae86e1950b1277e545cee180551750029cfe735,
# dae86e
#
# <describeOutput> v1.7.4.2-679-g3bee7fb
#
# <refname> master
# heads/master
# refs/heads/master
#
# [<branchname>]@{upstream} master@{upstream}
# @{u}
#
# [<branchname>]@{push} master@{push}
# @{push}
#
# <rev>^[<n>] HEAD^
# v1.5.1^0
#
# <rev>~[<n>] HEAD~
# master~3
# <rev>^{<type>} v0.99.8^{commit}
#
# <rev>^{} v0.99.8^{}
#
# <rev>^{/<text>} HEAD^{/fix nasty bug}
#
# :/<text> :/fix nasty bug
#
# <rev>:<path> HEAD:README
# master:./README
#
# :[<n>:]<path> :0:README
# :README
#----------------------------------------------------------------------#
# FORMATS
#----------------------------------------------------------------------#
#
# "(%ad) %s"
# (2024-03-23) Add postlist.html
# (2024-03-23) Initial commit
#
#----------------------------------------------------------------------#
# EVERYDAY GIT
#----------------------------------------------------------------------#
git reset --soft HEAD~3
git commit --edit -m"$(git log --format=%B --reverse HEAD..HEAD@{1})"
# undo changes and remove untracked files
git reset --hard && git clean -df
# garbage collect loose objects.
git gc --prune=now --aggressive
# get path where .git is
git rev-parse --show-toplevel
# shallow clone specific commit
git init
git remote add origin '<url>'
git fetch --depth 1 origin '<sha1>'
git checkout FETCH_HEAD
# compare the version before the last commit and the last commit
git diff HEAD^ HEAD
# show files with changes between remote and local
git diff --name-only origin/staging staging
# show commit
git log --oneline --no-merges origin/staging..staging
# $ git log --oneline --no-merges origin/staging..staging
# e76a30a (HEAD -> staging, origin/staging-sort, staging-sort) Remove artwork
# 055e7f2 Update pages
# e68e802 Update liquid html
# 0674b4d Update assets
# view "undo" history
git reset --hard 3050fc0de # Go back to the commit with the given hash
# format commits along with their diffs
git rev-list HEAD | git diff-tree --stdin --format=medium -p
# rebase
# ------
# Reset the current branch to the commit just before the last 12:
git reset --hard HEAD~12
# HEAD@{1} is where the branch was just before the previous command.
# This command sets the state of the index to be as it would just
# after a merge from that commit:
git merge --squash HEAD@{1}
# Commit those squashed changes. The commit message will be helpfully
# prepopulated with the commit messages of all the squashed commits:
git commit
git reset --soft HEAD~2 &&
git commit --edit -m"$(git log --format=%B --reverse HEAD@{1}..HEAD)"
#----------------------------------------------------------------------#
# STRATEGY OPTIONS
#----------------------------------------------------------------------#
# There are three ways to resolve a merge conflict in Git:
#
# To accept all changes on a file from the local version
# git checkout --ours <file name>
#
# Accept the local version for all conflicting files
# git merge --strategy-option ours
#
# Update the changes on a file from the remote branch
# git checkout --theirs <file name>
#
# Accept the remote version for all conflicting files with:
# git merge --strategy-option theirs
#
# If you have conflict on your local branch you can simply run these commands:
#
# $ git checkout --conflict=merge .
# Recreated 9 merge conflicts
#
# $ git checkout --ours .
# Updated 9 paths from the index
git checkout --conflict=merge .
# accept local
git merge --strategy-option ours
# accept remote
git checkout --ours .
#----------------------------------------------------------------------#
# OVERWRITING
#----------------------------------------------------------------------#
# merge devel into the current branch:
git merge --no-ff --no-commit devel
# overwrite file.txt in current branch
git checkout devel file.txt
# do not merge changes for file.txt - keep version in current branch
git checkout HEAD file.txt
# Removes `30-seconds.txt` from the last commit
git rm --cached "30-seconds.txt"
git commit --amend
#----------------------------------------------------------------------#
# REPOSITORY MAINTENANCE
#----------------------------------------------------------------------#
# Check for corruption:
git fsck
# Recompress, remove unused cruft:
git gc
#----------------------------------------------------------------------#
# Rebase from local branch
#----------------------------------------------------------------------#
git add .
git commit -m "blah"
git checkout master
git pull
git checkout $BRANCH_NAME
git rebase -i master
#----------------------------------------------------------------------#
# gh-pages
#----------------------------------------------------------------------#
# delete ref to remove branch history
git update-ref -d refs/heads/gh-pages
# remove all unversioned files.
git rm --ignore-unmatch -r -f '<FILES>'
#----------------------------------------------------------------------#
# COMPARE COMMITS
#----------------------------------------------------------------------#
# $ git diff --name-status @~2..@
# M .github/workflows/jekyll-build.yml
# M docs/assets/style.css
# get compare diff between last commit and previous commit - only list of affected files
git diff --name-status @~1..@
git diff @~1..@~2
# Get list of affected files for previous commit with highlighted changes
git show @~1
# get list of affected files for previous commit with highlighted changes
git show --name-only @~1
# Get list of file names affected for previous commit
git diff-tree -r --name-only @~1
# get list of file names with status for previous commit
git diff-tree -r --name-status @~1
# pull latest remote changes
git stash
git pull
git stash pop
########################################################################
# ARCHIVE
########################################################################
# create archive with contents of the latest commit on the current branch.
git archive -o latest.zip HEAD
# list files to zip
git archive --format=zip HEAD $(git ls-files ./*/*.md) >git-archive-9244.zip
# $ unzip -d out git-archive-9244.zip
# Archive: git-archive-9244.zip
# c5ec9716e1a5388db2577014ccc3b12b9e057df9
# creating: out/functions/
# inflating: out/functions/barcharts.md
# inflating: out/functions/describe.md
# creating: out/general/
# inflating: out/general/codepoints.md
# inflating: out/general/data-cleaning.md
# Archive files in assets/ with prefix
git archive --format=zip --prefix=git-assets/ HEAD:assets >git-assets.zip
# $ unzip git-assets.zip
# Archive: git-assets.zip
# creating: git-assets/
# creating: git-assets/custom/
# inflating: git-assets/custom/style.css
# inflating: git-assets/favicon.svg
########################################################################
# BISECT
########################################################################
git bisect start
# Current version is bad
git bisect bad
# v2.6.13-rc2 is known to be good
git bisect good v2.6.13-rc2
########################################################################
# BRANCH
########################################################################
# delete remote branch
git push -d origin '<branch-name>'
# delete local branch
git branch -d '<branch-name>'
# force delete
git branch -D '<branch-name>'
# View branches sorted by date
git branch --sort=-committerdate
# find branches containing a specific Git commit
git branch --contains 3050fc0
# find branches not containing a commit
git branch --no-contains 3050fc0
#----------------------------------------------------------------------#
# ORPHAN BRANCH
#----------------------------------------------------------------------#
# method 1
git switch --orphan '<new-branch>'
git commit --allow-empty -m "Initial commit on orphan branch"
# method 2 - i like this method
git checkout --orphan '<new-branch>'
git add -A
git commit -m "Initial commit"
# method 2b - empty orphan branch
# checkout orphan branch and remove files
git checkout --orphan '<new-branch>'
git rm -rf .
git reset .
########################################################################
# CAT
########################################################################
git ls-files --stage
# 100644 6675decc339281d846054b46557422e86b2a2cf9 0 .github/workflows/jekyll-build.yml
# 100644 e8b7597805f304332871c67deb51781abce8dcc1 0 annotations.json
# 100644 0ec3a28b4c7f824c793215eafb2b8d198a4f2d31 0 covers/1001812902.jpg
git cat-file blob 6675decc339281d846054b46557422e86b2a2cf9
########################################################################
# CHECKOUT
########################################################################
git checkout master
# reverts Makefile to two revisions back
git checkout master~2 Makefile
# recover deleted file from the index.
rm -f hello.c
git checkout hello.c
# RESOLVE MERGE: accept all changes from the local version
git checkout --ours '<file>'
# RESOLVE MERGE: update changes from the remote branch
git checkout --theirs '<file>'
# If you have conflict on your local branch you can simply run these commands:
#
# $ git checkout --conflict=merge .
# Recreated 9 merge conflicts
#
# $ git checkout --ours .
# Updated 9 paths from the index
# "hard reset" for a path
git checkout HEAD '<path>'
# overwrite file.txt in current branch
git checkout devel file.txt
# do not merge changes for file.txt - keep version in current branch
git checkout HEAD file.txt
# apply changes to file
git checkout stash@{0} -- '<file>'
########################################################################
# CHERRYPICK
########################################################################
# Create a local branch with the changes you want to bring in
git branch '<new_branch>' '<base_branch>'
#Switch into it.
git checkout mybranch
# Pull down the changes you want from the other person's account
git remote add repos-w-changes '<git_url>'
# Pull down everything from their branch.
git pull repos-w-changes branch-i-want
# Switch back to the branch you want to pull the changes into.
git checkout '<original-branch>'
# Cherry pick your commits, one by one, with the hashes.
git cherry-pick -x hash-of-commit
########################################################################
# CLEAN
########################################################################
# dry run
git clean -d -n
# Would remove .gitattributes
# Would remove .gitmodules
# remove untracked files
git clean -d -f
# Removing .gitattributes
# Removing .gitmodules
########################################################################
# CLONE
########################################################################
git clone -b '<branch>' '<remote-repo>'
git clone -b 17.1.0 https://github.com/ansible/awx.git
git clone --depth 1 https://github.com/nntrn/bookstand.git
########################################################################
# COMMIT
########################################################################
# The purpose of a fixup commit is to modify an earlier commit,
# it allows adding more changes in a new commit, but "marking" them as
# belonging to an earlier commit.
# Create a fixup commit to fix up the last commit on the branch:
git commit --fixup HEAD ...
# Create a fixup commit to fix up commit with SHA <COMMIT_SHA>:
git commit --fixup '<sha>'
# Update only the commit message of last commit
git commit --amend
########################################################################
# CONFIG
########################################################################
# github actions
git config --global user.email "${GITHUB_ACTOR}"
git config --global user.name "${GITHUB_ACTOR}@users.noreply.github.com"
########################################################################
# DIFF
########################################################################
#
# Three-dot and two-dot Git diff comparisons:
#
# * two-dot (git diff A..B)
# Example: https://github.com/npm/cli/compare/v8.19.2..v10.5.0
#
# * three-dot (git diff A...B)
# https://github.com/npm/cli/compare/v8.19.2...v10.5.0
#
git diff --name-status main...devel
# M _includes/postlist.html
# M _includes/script.js
# M _includes/style.css
# M _layouts/book.html
# M _layouts/default.html
# M _layouts/genre.html
# A index.html
# D index.md
# Changes in the working tree since your last commit;
# what you would be committing if you run git commit -a
git diff HEAD
# compare the version before the last commit and the last commit
git diff HEAD^ HEAD
# show files with changes between remote and local
git diff --name-only origin/staging staging
# $ git diff --name-only origin/staging staging
# bookstand.jq
# docs/_includes/books-css.html
# docs/_includes/postlist.html
# docs/_layouts/default.html
# docs/assets/books.css
# docs/assets/sort.js
# docs/assets/style.css
# docs/index.html
# docs/tags.html
# get files with more added lines than deleted
git diff --numstat HEAD..origin/main | awk '$1 > $2 {print $NF}'
# $ git diff --numstat HEAD..origin/main | awk '$1 > $2 {print $NF}'
# docs/_includes/filter.js
# docs/_includes/select.html
# docs/_includes/sort.js
# docs/_includes/urlscript.html
# docs/assets/style.css
# scripts/build.sh
git diff --numstat HEAD..origin/master | tr '\t' ' ' | awk '$1 > $2 {print $NF}' | xargs -I % git checkout origin/master %
# show the number of lines inserted and removed
git diff --numstat "@{1 day ago}"
# [added] [removed]
# ------- --------
# 0 10 184609.txt
# 0 3 55516.txt
# 1 1 65977.txt
# 0 0 66519.txt
# 1 1 69885.txt
# 0 12 70450.txt
# 0 3 70452.txt
# 1 1 71614.txt
########################################################################
# FETCH
########################################################################
# Peek at a remote's branch, without configuring the remote in your local repository:
# fetch maint branch from remote repo
# $ git fetch git://git.kernel.org/pub/scm/git/git.git maint
# $ git log FETCH_HEAD
########################################################################
# FILTER BRANCH
########################################################################
# rewrite the branch's history, passing it the previous command
git filter-branch --force --index-filter --tag-name-filter cat -- --all '<command>' --prune-empty
git filter-branch --force --index-filter "git rm --cached --ignore-unmatch FILE_TO_DELETE" --prune-empty --all
# Since I want to force push and force everyone to clone a new version of the history,
# I need to filter the branch using this:
git filter-branch --tag-name-filter cat -- --all
# ---------------------------------------------------------------------#
# Rewriting History
# ---------------------------------------------------------------------#
# Change commit date to author date for all commits [1]
git filter-branch --env-filter 'export GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"'
GIT_COMMITTER_DATE="$(date)" git commit --amend --no-edit --date "$(date)"
# Remove file(s) from history
git filter-branch --index-filter 'git rm --cached --ignore-unmatch FILENAME.txt' HEAD
# Remove empty commits
git filter-branch --commit-filter 'git_commit_non_empty_tree "$@"' -f HEAD
# Update name and email in commits [3]
git filter-branch --env-filter '
WRONG_EMAIL="nntrn@wrong.com"
CORRECT_EMAIL="nntrn@correct.com"
if test "$GIT_AUTHOR_EMAIL" = "$WRONG_EMAIL" ||
test "$GIT_COMMITTER_EMAIL" = "$WRONG_EMAIL"; then
export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi' && rm -fr "$(git rev-parse --git-dir)/refs/original/"
# Remove commits by author
git filter-branch --commit-filter '
COMMIT_USER=nntrn
if [ "$GIT_AUTHOR_NAME" != "$COMMIT_USER" ]; then
skip_commit "$@"
else
git commit-tree "$@"
fi' -f HEAD
git filter-branch --force --prune-empty --subdirectory-filter docs HEAD
# Update email for all commits
git filter-branch --env-filter 'export GIT_AUTHOR_EMAIL="nntrn@correct.com"' -f HEAD
# Remove a secret from a configuration file
git filter-branch --tree-filter "sed -i 's/12hgjg32s/AWS_TOKEN/g' config.yml" --all
########################################################################
# FOR-EACH-REF
########################################################################
# get last commit date for each branch [5]
git for-each-ref --sort='-committerdate:iso8601' --format=' %(committerdate:iso8601)%09%(refname)'
# 2023-06-07 09:44:09 +0000 refs/remotes/origin/apache
# 2022-08-23 08:53:29 +0000 refs/remotes/origin/postgres_playbook
# 2022-08-23 04:12:41 +0000 refs/remotes/origin/postgresql_playbook_git_uninstall
# 2022-07-27 23:04:10 +0000 refs/remotes/origin/unified_patching
# 2022-07-27 23:03:55 +0000 refs/remotes/origin/kernel_patch
########################################################################
# LOG
########################################################################
# trunc message
git log --oneline --format="%h %<(15,trunc)%s"
git log --after="2020-15-05" --before="2020-25-05"
git log --after="1 week ago"
# Show how a function in recipes.jq evolved over time
git log --oneline -L '/def describe:/',/^/:recipes.jq
git log --all --grep='Add'
# $ git log --all --grep='Add' --oneline
# d0402ba Add data
# fa244dc Add accent
# 304e637 Add 404
# get logs for rebase
git log --format=%B --reverse HEAD~10..HEAD
# $ git log --format=%B --reverse HEAD~10..HEAD
# Update recipes.jq
# Add conversion
# Improve brevity
# Add argument for precision
# Add describe-objects.md
# Edit subtitle
# Disable smart quotes
# Add unroll
# Add slurpfile example
# Update examples
git log --all --date=format:%F --pretty="%S|%h|%ad|%s" | column -s '|' -t
# refs/heads/devel 438e57e 2023-12-14 Add grep feature
# refs/remotes/origin/main 3ce9a35 2023-11-14 v1.1.0
# refs/remotes/origin/main dc7a3f5 2023-11-14 Initial commit
# refs/heads/devel 23b3af6 2023-11-14 Fix typo
# refs/heads/devel bfa1484 2023-11-14 Add ascii box
# refs/heads/devel 8fa7162 2023-11-14 Make executable
# refs/heads/main 3745b03 2023-09-16 Stdout color log
# refs/heads/alphav1 a377e66 2023-09-26 Update stdout colors
# refs/heads/alphav1 9229698 2023-09-26 Update README.md
# refs/heads/main 34c27af 2023-09-15 Update README.md
# refs/heads/alpha 1a1813f 2023-09-15 gh-search[alpha]
# refs/stash b625d1c 2023-09-13 WIP on wip: 3fbeea5 Add example for searching username
# refs/stash 0bc89a0 2023-09-13 index on wip: 3fbeea5 Add example for searching username
# refs/heads/wip 3fbeea5 2023-09-13 Add example for searching username
git log --all --branches --remotes=heads
git log --branches --not --remotes=origin
git log --branches --remotes=heads
# search commits that changes related to jq
git log -S '<search-term>'
# $ git log -S "jq"
# View changes with logs
# git log -S "jq" --follow -p -- <FILENAME>
git log --shortstat
# commit: [REDACTED]
# Author: [REDACTED]
# Date: [REDACTED]
# Bug fixes and WordPress 4.0.1 update
# 1377 files changed,
# 175405 insertions(+),
# 248 deletions(-)
# list only commits present on current branch but not main
git log --oneline main..
# list only commits preent on main but not current branch
git log --oneline ..main
# summary
git log --use-mailmap --graph --abbrev-commit --decorate --all --summary \
--format=format:'--+ Commit: %h %n | Branch: %S %n'' | Date: %aD (%ar) %n'' | Message: %f %d %n'' + Author: %aN %n'
# * --+ Commit: 5db162a
# | | Branch: refs/heads/staging
# | | Date: Tue, 24 Oct 2023 10:39:37 -0500 (34 hours ago)
# | | Message: Update-README (HEAD -> staging, origin/staging)
# | + Author: nntrn
# |
# |
# * --+ Commit: ba09841
# | | Branch: refs/heads/staging
# | | Date: Tue, 24 Oct 2023 10:39:24 -0500 (34 hours ago)
# | | Message: Minor-changes
# | + Author: nntrn
# |
# |
# Save every ref for README.md
git log --oneline --date=format:%F-%H-%M --format='git show %h:README.md > %ad-%h.md' README.md
# $ git log --oneline --date=format:%F-%H-%M --format='git show %h:README.md > %ad-%h.md' README.md
# git show d5d124e:README.md > 2023-09-13-15-06-d5d124e.md
# git show 6d7e020:README.md > 2023-09-13-07-57-6d7e020.md
# view commiter email
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an, %ae>%Creset' --abbrev-commit
# * 3d9136a - (HEAD -> recipes) Update jekyll (2 hours ago) <nntrn, nntrn@noreply.github.com>
# * 575d3d3 - jq build script (2 hours ago) <nntrn, nntrn@noreply.github.com>
# * 776af76 - Update markdown files (2 hours ago) <nntrn, nntrn@noreply.github.com>
# view deleted commits and files
git log --diff-filter=D --name-only --oneline
# 9c8e7f3 (origin/devel, devel) Reduce margin
# functions/ini-format.md
# 61e75b4 Update collections
# _recipes/barcharts.md
# Note: to prevent any chance of hash collisions [2]
git log --all --date-order --pretty="%H|%P|%d"
# 41640d4331b3f6eb3f7b94b1558dad382323f90e|caa37bcefe5ef50a8224ccc114099b766edf7057| (HEAD -> staging)
# caa37bcefe5ef50a8224ccc114099b766edf7057|889b8acacbba2163345da1865a10785d36f83960| (origin/staging)
git log --all --date-order --pretty="%h|%p|%d"
# 41640d4|caa37bc| (HEAD -> staging)
# caa37bc|889b8ac| (origin/staging)
# 889b8ac|3066cc6|
# 3066cc6|db8214a|
# changelog
echo -e "$(
git log --first-parent --pretty=format:'\n%as: (%t) %s' |
awk -F: '{a[$1]=a[$1]?a[$1]"\\n *"$2:"\\n *"$2;}END{for (i in a)print i":"a[i];}'
)"
# 2021-03-22:
# * (bb319ba079) Fix wrong backup directory var name in apt module (#73840)
#
# 2021-03-23:
# * (e1f4600639) apt_key - update key ID and URL used in test
# * (4b62b82ad7) Add integration tests for add_collection_to_versions_and_dates(), and extend ansible-doc tests (#73601)
# * (6446b9a1d3) setup virtualization
git log --pretty="%h - %cr - %s (%an)" "HEAD~5..HEAD"
# c5ec971 - 3 days ago - v1.2.1 (nntrn)
# 2b7eb31 - 3 days ago - Override and define new vars (nntrn)
# 294449a - 4 days ago - v1.2 (nntrn)
# d5d6a2d - 5 days ago - Remove circular collections (nntrn)
# 0e5bb75 - 5 days ago - Create jekyll-gh-pages.yml (nntrn)
# List all authors and commit counts
git log --format='%aN <%aE>' | awk '{arr[$0]++} END{for (i in arr){print arr[i], i;}}'
# 1 Joe Stone <jstone@email.com>
# 6 ughblah <blah@email.com>
# 20 nntrn <17685332+nntrn@users.noreply.github.com>
# count number of commits per file
git log --name-only --pretty=format: | tr -s '\n' | sort | uniq -c | sort -r -k 1
# 50 docs/assets/style.css
# 28 docs/_includes/books.css
# 26 docs/index.html
# 15 docs/_includes/header.html
# 12 docs/_layouts/default.html
########################################################################
# LS-FILES
########################################################################
# $ git ls-files --format=
# %(objectname) %(objectmode) %(path) %(stage) %(eolinfo:index)
# | | | | |-- %(eolinfo:worktree)
# 880f858c12e04f2a7ce5c3aa8f95a53989551d3a 100644 _config.yml 0 lf
# 6600666d2e9c19fd8a6a18b7971a64ad5e8ab413 100644 _includes/nav_footer_custom.html 0 lf
# 9a212cd1d3a3a21672ecbf641382367a92b8f142 100644 _sass/color_schemes/custom.scss 0 lf
# 8ac8e0b235a099622cf458c71c471086c97a5eeb 100644 _sass/custom/custom.scss 0 lf
#
git ls-files --format='%(objectname) %(objectmode) %(path)'
# 84d52184295efdb1540ed748a7d35d245432fb2d 100644 .gitignore
# b6c3123e3a0fa8f1fda64e3a0407ae0e38ef1b58 100644 LICENSE
# 3398137df2c9ad9c9908479f5ef0f9101603e818 100644 README.md
git ls-files -z | GIT_PAGER= xargs -0 -L1 -I'{}' git log -n 1 --format="* {}: %s" -- '{}'
# * README.md: Update README.md
# * assets/dayone.png: Update README
# * ibooks2dayone: Enable reading from stdin
# * queryibooks: Remove unused arguments
########################################################################
# MERGE
########################################################################
#
# IF THE MERGE "means something": use --no-ff otherwise use -ff
#
# $ git merge --squash staging
#
# $ git log --oneline --format='%s' main..staging
# Update task for bookcover
# Update github actions email
# Update README.md
#
# HEAD@{1} is where the branch was just before the previous command.
# This command sets the state of the index to be as it would just
# after a merge from that commit:
git merge --squash HEAD@{1}
# True merge (fast forward):
git merge '<branch-name>'
# Merge to master (only if fast forward):
git merge --ff-only '<branch-name>'
# Merge to master (force a new commit):
git merge --no-ff '<branch-name>'
# Stop merge (in case of conflicts):
git merge --abort
# Stop merge (in case of conflicts):
git reset --merge
# Undo local merge that hasn't been pushed yet:
git reset --hard origin/master
# Merge only one specific commit:
git cherry-pick 073791e7
git checkout main
git merge -s ort -X theirs -m "v2.0" --log staging
########################################################################
# PATCH
########################################################################
# create patch with diff of branch
git diff branch_b >/tmp/branch_b.patch
# apply patch to markdown files
git apply -p1 --include=*.md /tmp/branch_b.patch
########################################################################
# PULL
########################################################################
git pull
# Update your local working branch with commits from the remote, and
# update all remote tracking branches.
git pull --rebase
# Update your local working branch with commits from the remote, but
# rewrite history so any local commits occur after all new commits
# coming from the remote, avoiding a merge commit.
########################################################################
# REBASE
########################################################################
# REPLACE ALL PICK WITH SQUASH
:g/^pick/s/pick/squash/g
#-----------------------------------------------------------------------
git checkout branchname
git rebase master
#or:
# The rebase moves all of the commits in master onto the tip of branchname.
git merge master '<branch-name>'
# Cancel rebase:
git rebase --abort
# Squash multiple commits into one:
git rebase -i HEAD~3
# Squash-merge a feature branch (as one commit):
git merge --squash '<branch-name>'
GIT_SEQUENCE_EDITOR="sed -i -ze 's/^pick/edit/'" git rebase -i HEAD~3
git config --add rebase.instructionFormat "[%an @ %ar] %s"
git -c "rebase.instructionFormat=(%an <%ae>) %s" rebase -i HEAD~3
########################################################################
# RESET
########################################################################
#
# EXAMPLE
# Assume the following commit history:
#
# D Your latest patch
# C Your second patch
# B Your first patch
# A Someone else's work
#
# You have no staged changes and git show tell you are currently at commit D
#
# $ git reset --soft B
# $ git commit --amend
#
# Adapted from https://stackoverflow.com/a/58100945
git reset --soft HEAD~5
git ammend
# Delete the most recent commit, without destroying the work you've done:
git reset --soft HEAD~1
# Delete the most recent commit and remove changes:
git reset --hard HEAD~1
# Rewinds back to `3050fc0` but keeps changes in the working directory
git reset 3050fc0
# Rewinds back to `c0d30f3` and deletes changes
git reset --hard c0d30f3
# Rewinds back 5 commits but keeps changes in the working directory
git reset HEAD~5
# Rewinds back 3 commits and deletes changes
git reset --hard HEAD~3
# Reset the current branch to the commit just before the last 12:
git reset --hard HEAD~12
########################################################################
# REPLACE
# https://stackoverflow.com/a/58029311
########################################################################
pointA=beb7ea3351f50dd29899baa878ea2fa29c437ecc
pointC=d8cf79f2103b7d25e6c4dbb96bbd3f672d30bae8
merge_commit=ed3fef629f8d7268fe29c37029977443eea46494
# this says to make points B and C & the merge commit be parents of commit A.
git replace -f --graft "$pointA" "$pointC" "$merge_commit"
# BEFORE
# * commit beb7ea3351f50dd29899baa878ea2fa29c437ecc (A)
# |
# * commit ed3fef629f8d7268fe29c37029977443eea46494
# |\ Merge: d8cf79f ed3fef6
# | |
# AFTER
# $ git log --graph
# * commit beb7ea3351f50dd29899baa878ea2fa29c437ecc (HEAD -> master, replaced)
# |\ Merge: d8cf79f ed3fef6
# | |
# | * commit ed3fef629f8d7268fe29c37029977443eea46494
# | |\ Merge: d8cf79f 820bea7
# |/ /
########################################################################
# REV-LIST
########################################################################
# REACHABLE OBJECTS
git rev-list --disk-usage --objects --all
# plus reflogs
git rev-list --disk-usage --objects --all --reflog
# count commits
git rev-list --all --count
git rev-list --count '<branch-name>'
# FORMATS: oneline, short, medium, full, fuller, reference, email, raw,
git rev-list --format=medium HEAD
# view commits in this branch but not upstream
git rev-list @{upstream}..HEAD
# same as
git rev-list origin/ <branch_name >..HEAD
# Lists commit objects in reverse chronological order
#
# --left-right
# Commits from the left side are prefixed with < and those from
# the right with >. If combined with --boundary, those commits are
# prefixed with -.
#
git rev-list HEAD -- '<path>'
# example:
# $ git rev-list HEAD -- _config.yml
# 920e920dc363a12a4b225d250dd44f3ed47921d0
# 5d5a3341b0cd239011461ef648875740e57f0e2e
git rev-list --left-right --boundary --pretty=oneline main..staging
# >5dc16e86c82b21ef2644c6e165a170aa2859ecbb Remove url field for store data
# >e92018d7af6c713aa6450f7dfa242dee63cacc08 Update task for bookcover
# >abefe88c2116d4d35be5b591ae316e553055fcbd Update github actions email
# >eb34e85123edc0b32d33738bfdc4e3b28fa0767c Update README.md
# -061d13e202820966eb639162f427c453bc159390 Add gitignore
# report the disk size of each branch,
while read branch; do
size=$(git rev-list --disk-usage --objects HEAD..$branch)
echo "$size $branch"
done | sort -n
# 0 refs/heads/main
# 0 refs/heads/staging
# 0 refs/remotes/origin/staging
# 8331 refs/stash
# 11448 refs/remotes/origin/HEAD
# 11448 refs/remotes/origin/main
# 39634 refs/tags/v1.0.0
# 127289 refs/heads/devel/panel
# 15320918 refs/remotes/origin/assets
########################################################################
# SHOW
########################################################################
# PRINT REMOTE README.md in devel branch
git show origin/$REMOTE-BRANCH:$FILEPATH
# show diff for latest stash
git show stash@{0}
# show latest stash entry for file
git show stash@{0}:$FILEPATH
########################################################################
# STASH
########################################################################
# Pull latest remote changes
# $ git stash
# $ git pull
# $ git stash pop
# $ git stash list
# stash@{0}: WIP on staging: c0ed5f4 Remove header color
# stash@{1}: WIP on master: 2b7eb31 Override and define new vars
# git stash show -p 0
# git stash show -p 1
# $ git stash list
# stash@{0}: On staging: v3
# stash@{1}: On staging: sans-serif
# stash@{2}: On staging: sidebar
# stash@{3}: On staging: book top border
#
# $ git stash show -p stash@{0} --name-only
# docs/_includes/books.css
# docs/_includes/filtersort.html
# docs/_includes/header.html
# docs/_includes/postlist.html
# docs/_includes/social.html
#
# $ git show stash@{0}:docs/assets/style.css
# get affected files of stash
git stash show -p stash@{0} --name-only
# apply changes to file
git checkout stash@{0} -- '<filename>'
# get contents of stash
git show stash@{0}:'<filepath>'
# Show the files in the most recent stash:
git stash show
# Show the diff of the most recent stash:
git stash show -p
# Show the changes of the named stash:
git stash show -p stash@{1}
git stash show
# data/athletes.json | 149138 ------------------------------------------
# data/cities.json | 6 +-
# data/football-players.json | 16 +-
# functions/summary.md | 4 +-
# 4 files changed, 13 insertions(+), 149151 deletions(-)
git stash show -p
# diff --git a/data/athletes.json b/data/athletes.json
# deleted file mode 100644
# index 66c3ede..0000000
# --- a/data/athletes.json
# +++ /dev/null
# @@ -1,149138 +0,0 @@
# -[
# - {
# - "id": 736041664,
# - "name": "A Jesus Garcia",
# - "nationality": "ESP",
git stash show -p stash@{0} --name-only
# data/athletes.json
# data/cities.json
# data/football-players.json
# functions/summary.md
git diff stash@{1} -- functions/summary.md
# diff --git a/functions/summary.md b/functions/summary.md
# index 6e477a2..ca73947 100644
# --- a/functions/summary.md
# +++ b/functions/summary.md
# @@ -2,7 +2,7 @@
# Creates a new stash
git stash save
# Creates a new stash, including untracked files
git stash save -u
# Creates a new stash with the message "Bugfix WIP"
git stash save "Bugfix WIP"
git stash list --oneline
# 10c500d (refs/stash) refs/stash@{0}: WIP on staging: c0ed5f4 Remove header color
# e839de3 refs/stash@{1}: WIP on master: 2b7eb31 Override and define new vars
########################################################################
# TAG
########################################################################
# HTTPS://stackoverflow.com/a/35979751
# https://initialcommit.com/blog/git-tag
git tag --list
# 1.0.0
# 1.0.1
# 1.0.2
# 1.0.3
# To set the date used in future tag objects, set the environment variable GIT_COMMITTER_DATE
#
# SHELL | EXAMPLE
# --------------------|---------------------------------
# date --rfc-email | Tue, 25 Jul 2023 08:20:59 -0500
# date '+%s %z' | 1690290222 -0500
# date '+%FT%X' | 2023-07-25T08:22:42
# date '+%F %H:%M' | 2023-07-25 08:26
#
GIT_COMMITTER_DATE="2006-10-02 10:31" git tag -s v1.0.1
git config --global push.followTags true
# creates the tag
git tag v1.0.0
# push tag to server
git push tags
# checkout a git tag
# git checkout tags/tag-name -b branch-name
git checkout tags/v1.0 -b v1.0-branch
# To find the remote tags:
git ls-remote --tags origin
# Create a tag with the given tag message
git tag -m "tag message" '<tag_name>' -a
# To Push a single tag to remote
git push origin '<tag_name>'
# Push all tags to remote
git push origin --tags
########################################################################
# WORKTREE
########################################################################
# EXAMPLE
mkdir gh-search-test
cd gh-search-test
git clone -b devel https://github.com/nntrn/gh-search devel
cd devel
git worktree add ../main main
# .
# ├── devel
# │ ├── README.md
# │ └── gh-search
# └── main
# ├── README.md
# └── gh-search
########################################################################
# MISC
########################################################################
# output trace as json to file
GIT_TRACE2_EVENT=3 git log --oneline 3>/tmp/gitlog.txt
# alternative to du: add up "size" and "size-pack" fields
git count-objects -v
# $ git count-objects -v
# count: 56
# size: 224
# in-pack: 945
# packs: 1
# size-pack: 10725
# prune-packable: 0
# garbage: 0
# size-garbage: 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment