Skip to content

Instantly share code, notes, and snippets.

@gaurav-nelson
Last active May 23, 2023 07:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gaurav-nelson/6e7f2e670d1f61a8b941911e32d7b98d to your computer and use it in GitHub Desktop.
Save gaurav-nelson/6e7f2e670d1f61a8b941911e32d7b98d to your computer and use it in GitHub Desktop.
Git aliases
[alias]
lg = log --pretty=format:'%Cred%h%Creset %Cblue%an%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)' --abbrev-commit --date=relative
unstage = reset HEAD --
alias = "!f() { \
if [ \"$#\" -ne 2 ]; then { echo >&2 \"Usage: git alias <name> <command>\"; exit 1; } fi; \
git config --global alias.\"$1\" \"$2\"; \
}; f"
syncoriginmaster = "!f() { \
git fetch origin master && git rebase origin/master; \
}; f"
syncoriginmain = "!f() { \
git fetch origin main && git rebase origin/main; \
}; f"
syncupstreammaster = "!f() { \
git fetch upstream master && git rebase upstream/master; \
}; f"
syncupstreammain = "!f() { \
git fetch upstream main && git rebase upstream/main; \
}; f"
syncoriginbranch = "!f() { \
git fetch origin $(git rev-parse --abbrev-ref HEAD) && git rebase origin/$(git rev-parse --abbrev-ref HEAD); \
}; f"
syncupstreambranch = "!f() { \
git fetch upstream $(git rev-parse --abbrev-ref HEAD) && git rebase upstream/$(git rev-parse --abbrev-ref HEAD); \
}; f"
newbranch = checkout -b
deletebranch = "!f() { git branch -D \"$1\"; }; f"
renamebranch = "!f() { git branch -m \"$1\"; }; f"
findbranch = !git branch | grep -i
previousbranch = checkout @{-1}
addtolastcommit = "!git add -A && git commit --amend --no-edit"
undocommit = "!f() { git reset --soft \"$1\" && git status; };f"
undolastcommit = !git reset --soft HEAD^
wip = "!f() { \
git add -A; \
if [ \"$(git log -1 --pretty=%B)\" = \"WIP:Do not merge\" ]; then git commit --amend --no-edit; else git commit -m \"WIP:Do not merge\"; fi; \
}; f"
unwip = "!f() { \
if [ \"$(git log -1 --pretty=%B)\" = \"WIP:Do not merge\" ]; then git reset HEAD^; else echo \"No work in progress commit\"; fi; \
}; f"
getremotebranchorigin = "!f() { \
read -p \"Enter remote branch name: \" branchname; \
echo \"==== Syncing with origin ====\"; \
git fetch origin $branchname; \
echo \"==== Getting the remote branch ====\"; \
git checkout -t origin/$branchname; \
}; f"
getremotebranchupstream = "!f() { \
read -p \"Enter remote branch name: \" branchname; \
echo \"==== Syncing with upstream ====\"; \
git fetch upstream $branchname; \
echo \"==== Getting the remote branch ====\"; \
git checkout -t upstream/$branchname; \
}; f"
showmodifiedfiles = "!f() { \
echo \"==== Modified files in last commit ====\"; \
git diff-tree --no-commit-id --name-status -r $(git rev-parse HEAD); \
}; f"
resetfile = "!f() { git checkout $(git rev-parse @) -- \"${1}\"; }; f"
amend = "!git add -A && git commit --amend --no-edit"
pushbranch = "!git push origin $(git rev-parse --abbrev-ref HEAD)"
forcepushbranch = "!git push -f origin $(git rev-parse --abbrev-ref HEAD)"
switchbranch = "!f() { git checkout "$(echo $1 | cut -d ":" -f 2)"; }; f"
conflicts = "!f() { \
echo \"Files with conflicts:\"; \
git status -s | grep ^UU | cut -d ' ' -f 2; \
}; f"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment