Skip to content

Instantly share code, notes, and snippets.

@Kavignon
Last active January 10, 2025 14:46
Show Gist options
  • Save Kavignon/5fd84927a2427d7e3b07b6249b5da437 to your computer and use it in GitHub Desktop.
Save Kavignon/5fd84927a2427d7e3b07b6249b5da437 to your computer and use it in GitHub Desktop.
Provides utilities for handling your Git operations locally with automations to avoid repeatable work

Git Aliases Documentation

Documentation Management

docs

  • Description: Displays the Markdown documentation for your Git aliases in the terminal, automatically rendering it with glow for a formatted, easy-to-read view. If glow is not installed, the command will prompt you to install it in order to view the documentation.
  • Arguments: None
  • Usage: git docs

help-aliases

  • Description: Lists all configured Git aliases, sorted alphabetically.
  • Arguments: None
  • Usage: git help-aliases

Commit Management

c-amend

  • Description: Amends the last commit with a new message.
  • Arguments: <message>
  • Usage: git amend 'New commit message'

c-undo

  • Description: Resets the last commit and unstages all changes.
  • Arguments: None
  • Usage: git undo

c-drop-latest

Description: Resets to a previous commit, dropping the latest commits. Defaults to 1 commit if no number is specified. It then updates the branch on the remote server with the dropped commit. Arguments: <number> [optional]
Usage: git drop-latest (defaults to 1 commit) or git drop-latest 2

Branch management

b-rebase-root

  • Description: Rebases the current branch interactively from the root
  • Arguments: None
  • Usage: git b-rebase-root

b-rename

  • Description: Renames a branch locally and on the remote repository.
  • Arguments: <old-name> <new-name> [remote]
  • Usage: git b-rename old-branch new-branch origin

b-from-picks

  • Description: Creates a new branch from a base branch and cherry-picks a list or range of commits.
  • Arguments: <new-branch> [base-branch] [commit-id] [cherry-pick-commits]
  • Usage: git create-from-picks feature-branch "commitA^..commitB"

b-new

  • Description: Creates a new branch from the specified base branch (or main/master by default) or from a specific commit ID if provided.
  • Arguments: <branch-name> [base-branch] [commit-id]
  • Usage: git b-new feature-branch main

Syncing and Merging

sync-base

  • Description: Syncs the current branch with the default branch (main or master) or a specified branch.
  • Arguments: [branch-name]
  • Usage: git sync-base develop

Cleanup

view-clean

  • Description: Shows what files would be deleted by a clean operation, without actually deleting them.
  • Arguments: None
  • Usage: git view-clean

full-clean

  • Description: Resets changes and forcefully removes untracked files.
  • Arguments: None
  • Usage: git full-clean

Status and Logging

st

  • Description: Displays the status of the repository in a short format.
  • Arguments: None
  • Usage: git st

lg

  • Description: Displays a graphical representation of the commit history.
  • Arguments: None
  • Usage: git lg

Backup and Recovery

backup-branch

  • Description: Creates a backup branch of the current branch and pushes it to the remote repository.
  • Arguments: None
  • Usage: git backup-branch

Git Aliases

Enhance your Git workflow with these custom aliases. Each alias is designed to simplify common tasks and improve efficiency.

Create a new docs directory

In this directory that will be located at ~/docs, we will store the git-aliases.md markdown which contains all the information that we need on how the aliases work. You can copy the file named git-aliases.md that's in the gist.

Adding the Aliases to Your Git Configuration

To add these aliases to your Git configuration, follow these steps:

  1. Open Your Git Configuration File

    Open your global .gitconfig file, typically located at ~/.gitconfig, in your preferred text editor.

  2. Add the Aliases

    Copy and paste the following [alias] section into your .gitconfig from the git-aliases.md file in the gist.

Git Aliases

 [alias]
   # ----------------------------
   # Documentation Management
   # ----------------------------
   docs = "!command -v glow >/dev/null 2>&1 || { echo 'glow is not installed. Please install glow to view this file.'; exit 1; } && glow $HOME/docs/alias-docs.md"
   help-aliases = "!f() { \
     echo 'Available Aliases:'; \
     git config --get-regexp '^alias\\.' | sed 's/^alias\\.//g' | awk '{print $1}' | sort; \
     }; f"

     # ----------------------------
     # Commit Management
     # ----------------------------
     c-amend = "!git commit --amend -m"
     c-undo = "!git reset HEAD~1 && git checkout -n ."
 	c-drop-latest = "!sh -c 'git reset --hard HEAD~${1:-1} && git push --force' -"

     # ----------------------------
     # Branch Management
     # ----------------------------
     b-rebase-root = "!f() {
 	    # Ensure the current branch is not 'main'
 	    if [ \"$(git symbolic-ref --short HEAD)\" = \"main\" ]; then
 		echo 'Error: You are on the main branch. Please switch to your feature branch.';
 		exit 1;
 	    fi;

 	    # Find the first commit of the current branch
 	    root_commit=$(git rev-list --boundary main..HEAD | grep '^-' | cut -c2-);

 	    # Check if the root commit was found
 	    if [ -z \"$root_commit\" ]; then
 		echo 'Error: Unable to determine the root commit of the feature branch.';
 		exit 1;
 	    fi;

 	    # Start interactive rebase from the root commit
 	    git rebase -i \"$root_commit\"^;
 	}; f"
     b-rename = "!f() {
     if [ -z \"$1\" ] || [ -z \"$2\" ]; then
         echo 'Usage: git b-rename <old-branch-name> <new-branch-name> [remote]';
         exit 1;
     fi;
     old_branch=\"$1\";
     new_branch=\"$2\";
     remote=\"${3:-origin}\";
     if ! git show-ref --verify --quiet refs/heads/$old_branch; then
         echo \"Error: Local branch '$old_branch' does not exist.\";
         exit 1;
     fi;
     if git show-ref --verify --quiet refs/heads/$new_branch; then
         echo \"Error: Local branch '$new_branch' already exists.\";
         exit 1;
     fi;
     if git ls-remote --exit-code --heads $remote \"$new_branch\" > /dev/null 2>&1; then
         echo \"Error: Remote branch '$new_branch' already exists.\";
         exit 1;
     fi;
     current_branch=$(git symbolic-ref --short HEAD);
     if [ \"$current_branch\" = \"$old_branch\" ]; then
         git branch -m \"$new_branch\";
         git push $remote \"$new_branch\";
         git push $remote --set-upstream \"$new_branch\";
     else
         git branch -m \"$old_branch\" \"$new_branch\";
         git push $remote \"$new_branch\";
     fi;
     if git ls-remote --exit-code --heads $remote \"$old_branch\" > /dev/null 2>&1; then
         git push $remote --delete \"$old_branch\";
     else
         echo \"Note: Remote branch '$old_branch' not found. Skipping delete.\";
     fi;
 }; f"
     b-new = "!f() { \
 	newBranch=\"$1\"; \
 	baseBranch=\"${2:-$(git remote show origin | grep 'HEAD branch' | cut -d' ' -f5)}\"; \
 	commitId=\"$3\"; \
 	if [ -z \"$commitId\" ]; then \
 	    git checkout \"$baseBranch\" && git pull && git checkout -b \"$newBranch\"; \
 	else \
 	    git checkout \"$baseBranch\" && git checkout -b \"$newBranch\" \"$commitId\"; \
 	fi; \
     }; f"
     b-from-picks = "!f() { \
 	newBranch=\"$1\"; \
 	baseBranch=\"${2:-$(git remote show origin | grep 'HEAD branch' | cut -d' ' -f5)}\"; \
 	cherryPickCommits=\"$3\"; \
 	git checkout $baseBranch && git pull --rebase; \
 	git checkout -b \"$newBranch\" $(git rev-parse $baseBranch); \
 	if [ -n \"$cherryPickCommits\" ]; then \
 	    if [[ \"$cherryPickCommits\" == *'..'* ]]; then \
 		git cherry-pick $cherryPickCommits || { echo 'Cherry-pick range failed. Resolve conflicts and commit manually.'; exit 1; }; \
 	    else \
 		for commit in $cherryPickCommits; do \
 		    git cherry-pick $commit || { echo 'Cherry-pick failed. Resolve conflicts and commit manually.'; exit 1; }; \
 		done; \
 	    fi; \
 	fi; \
     }; f"

     # ----------------------------
     # Syncing and Merging
     # ----------------------------
     sync-base = "!f() { \
 	branch=\"${1:-$(git remote show origin | grep 'HEAD branch' | cut -d' ' -f5)}\"; \
 	current_branch=$(git symbolic-ref --short HEAD); \
 	git checkout $branch && git fetch origin && git pull origin $branch && \
 	git checkout $current_branch && git rebase origin/$branch && \
 	if [ $? -eq 0 ]; then \
 	    git push --force-with-lease; \
 	else \
 	    echo 'Rebase conflicts occurred. Resolve them and then run rebase --continue.'; \
 	fi; \
     }; f"

     # ----------------------------
     # Cleanup
     # ----------------------------
     clean-view = "!git clean -nd"
     clean-force = "!git checkout . && git reset --hard && git clean -fd"

     # ----------------------------
     # Status and Logging
     # ----------------------------
     st = "!git status -sb"
     lg = "!git log --graph --oneline --decorate --all"

     # ----------------------------
     # Backup and Recovery
     # ----------------------------
     backup-branch = "!f() { \
 	CURRENT_BRANCH=$(git symbolic-ref --short HEAD); \
 	BACKUP_BRANCH=\"${CURRENT_BRANCH}-backup-$(date +%Y%m%d%H%M%S)\"; \
 	git branch \"$BACKUP_BRANCH\" && git push origin \"$BACKUP_BRANCH\"; \
     }; f"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment