Skip to content

Instantly share code, notes, and snippets.

@ngstigator
Last active November 1, 2023 18:13
Show Gist options
  • Save ngstigator/dc7755cbc81f1a794ee326f2f1952ee6 to your computer and use it in GitHub Desktop.
Save ngstigator/dc7755cbc81f1a794ee326f2f1952ee6 to your computer and use it in GitHub Desktop.

Git

Commands

Trigger without needing empty commits

git add . && git commit --amend --no-edit

Delete latest commit from remote

git reset --hard HEAD~1
git push origin HEAD --force

Search commit messages

git log --all --grep='<STRING>'

Track branch

When publishing:

git push -u <REMOTE> <BRANCH_NAME>

After publishing:

git branch -u <REMOTE>/<BRANCH>

Show merged branches

git branch --merged| egrep -v "(^\*|master|main|dev)"

Configuration

Useful aliases

# GIT
alias ga='git add'
alias gaa='git add .'
alias gau='git add --update'
alias gb='git branch'
alias gbd='git branch --delete '
alias gc='git commit'
alias gcm='git commit --message'
alias gcf='git commit --fixup'
alias gco='git checkout'
alias gcob='git checkout -b'
alias gcom='git checkout master'
alias gcos='git checkout staging'
alias gcod='git checkout develop'
alias gd='git diff'
alias gda='git diff HEAD'
alias glg='git log --graph --oneline --decorate --all'
alias gld='git log --pretty=format:"%h %ad %s" --date=short --all'
alias gm='git merge --no-ff'
alias gma='git merge --abort'
alias gmc='git merge --continue'
alias gnit='git init'
alias gp='git push'
alias gpl='git pull'
alias gpom="git push origin master"
alias gpr='git pull --rebase'
alias gr='git rebase'
alias gs='git status'
alias gss='git status --short'
alias gst='git stash'
alias gsta='git stash apply'
alias gstd='git stash drop'
alias gstl='git stash list'
alias gstp='git stash pop'
alias gsts='git stash save'

.gitconfig

Save SSH key passphrase in Windows

git config --global core.sshCommand C:\\Windows\\System32\\OpenSSH\\ssh.exe

Disable Windows line endings

System-wide .gitconfig

autocrlf=false

Per-user solution

git config --global core.autocrlf false

Per-project solution

git config --local core.autocrlf false

Housekeeping

Delete local branches that are no longer on remote

git branch -vv | grep gone | awk '{print $1}' | xargs git branch -D

Delete local branches that have been merged

git branch -D $(git branch --merged| egrep -v "(^\*|master|main)")

Delete local branches by pattern match

git branch -D `git branch | grep -E '^3\.2\..*'`

Delete remote branches that no longer exist

git fetch --prune

Reduce repo size (.git folder)

git remote prune origin <--dry-run>

Git migrate:

You should do this before you push:

  for remote in `git branch -r | grep -v master `; do git checkout --track $remote ; done

Followed by

git push --all

Remove .DS_Store

find . -name ".DS_Store" -print0 |xargs -0 git rm --ignore-unmatch

REBASE:

git checkout master
git pull origin master
git checkout feature-branch
git rebase master
git push --force-with-lease origin feature-branch

Git ignore local

git update-index --assume-unchanged content/uploads

DIFFs

Git commits since tag:

  1. git config --global tag.sort version:refname
  2. git shortlog $(git tag | tail -n 1)..HEAD

git tag -a v1.4 -m "my version 1.4"

After a shallow clone, get all branches with:

git remote set-branches origin '*'

Git show files changed in commit

git show --name-only c7c82a6c -- ':(exclude)dist/*'

Show change in commit for a file

git show <commit_hash> -- /PATH/TO/FILE

Fetch and update Submodules

git submodule foreach --recursive 'git fetch --tags'

git submodule update --recursive

Git show authors and commits

git shortlog -e -s -n

Show commits by author

git log --author="Jon"

Amend Git commit (before push)

git commit --amend

Delete Specific Commit

Linux:

git rebase -p --onto SHA^ SHA
git push -f <REMOTE> <BRANCH_NAME>

Mac:

git rebase --onto SHA~ SHA
git push -f <REMOTE> <BRANCH_NAME>

Vendors

Bitbucket

Highlight multiple lines of code: https://bitbucket.org/labelengine/createexpert/src/local/.env.development.yaml#lines-15,25

Highlight multiple sections of code: https://bitbucket.org/labelengine/createexpert/src/local/.env.development.yaml#lines-15:25,30:32

Gitlab Recovery Codes

ssh git@gitlab.com 2fa_recovery_codes

Github tokens

{
    "http-basic": {},
    "github-oauth": {
        "github.com": "__TOKEN__"}
}
@ngstigator
Copy link
Author

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