Skip to content

Instantly share code, notes, and snippets.

@samson-sham
Last active March 24, 2024 01:07
Show Gist options
  • Save samson-sham/a2d129672939915a5ed5084cf69288f4 to your computer and use it in GitHub Desktop.
Save samson-sham/a2d129672939915a5ed5084cf69288f4 to your computer and use it in GitHub Desktop.
Git Operations
# Completely clean up git, including git history
git reflog expire --expire=now --expire-unreachable=now --all
git gc --prune=now
# These commands are mentioned by others as well, but I have no ideas what they are...
git fsck --unreachable
git fsck --unreachable --no-reflogs
git gc --aggressive
# Remove folder from the whole branch (including history)
# https://gitbook.tw/chapters/faq/remove-files-from-git
git checkout -b newBranch
git branch -d removeBranch
git push origin --delete removeBranch
# list commit files
git ls-tree -r HEAD
git ls-files -s
git log --name-status
git filter-branch -f --tree-filter "rm -rf src/docker/private"
# git commit SHA-1 will be changed after filter-branch!!! Should check the new hash
git log
git ls-tree -r 173d4724e8640f9a4442cc22062233b0d4d48346
git commit --amend --author="Author Name <email@address.com>" --no-edit
git archive -o ../archive.zip HEAD
git tag -a "1.0" -m "Message"
git push --tags
git push -u ss official-customMenu:CustomMenu
# Checkout a new remote branch
git fetch <remote> <rbranch>:<lbranch>
git checkout <lbranch>
# Checkout branch files but stay on HEAD
git checkout branchname -- .
git remote -v
git remote add name git@ssh.git
git remote set-url gitlab git@gitlab.com/new-url.git
# Check current branch upstream
git branch -vv
git branch --unset-upstream
git branch -u origin/master
git stash save
git stash pop
git stash list
git stash apply stash@{1}
git stash pop stash@{1} # remove stash once applied
git diff stash@{0}^!
# git reset single file
git checkout HEAD -- file.txt
git diff --cached
git diff --staged
# permanently delete file from git
# https://myopswork.com/how-remove-files-completely-from-git-repository-history-47ed3e0c4c35
git filter-branch --index-filter 'git rm -rf --cached --ignore-unmatch path/to/folder' HEAD
git filter-branch -f --index-filter 'git rm -rf --cached --ignore-unmatch path/to/folder' HEAD
# --force flag need to follow filter-branch
# Show backup branches
git show-ref
# Purge all backups
git update-ref -d refs/original/refs/heads/master
# manually commit specific commit to complete new remote branch
git push origin af3cb234c833286ab1ad4eb25f01b35e11beee01:refs/heads/master
git branch --set-upstream-to=origin/master master
git rebase -i
# Rebase from root
git rebase -i --root
break
git rebase --continue
git rebase --abort
npm version [<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease | from-git]
git diff > current.patch
git format-patch -1 HEAD --stdout > some.patch
# The -1 flag indicates how many commits should be included in the patch:
git apply --stat file.patch # show stats.
git apply --check file.patch # check for error before applying.
git apply file.patch # "apply" will lose commit information, will need to commit manually
git am < file.patch # "am" will commit automatically
git describe <SHA-1> # show tag which is closest to the commit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment