2 min read

Streamlining Git Workflows: Essential Aliases and CLI Commands

Streamlining Git Workflows: Essential Aliases and CLI Commands

In the world of software development, efficiency is key. Git, the ubiquitous version control system, offers powerful tools to streamline workflows, but it can be cumbersome without the right configurations. This blog post explores essential Git aliases and GitHub CLI commands to enhance your productivity, from creating branches to merging pull requests.

Essential Git Aliases

1. tree Alias

The tree alias provides a visually appealing log of your commit history, displaying branches and merges in a graphical format.

[alias]
tree = log --graph --decorate --pretty=format:'%C(bold)%h%Creset %C(cyan)%s%Creset %C(dim white)(%cr) %C(bold cyan)<%an>%Creset' --all

Example Usage:

git tree

This command generates a log with a graphical representation of your commit history, showing commit hashes, messages, relative dates, and author names. It's perfect for quickly visualizing the project's history.

2. tree-all Alias

The tree-all alias builds upon the tree alias, simplifying the history view by focusing on commits referenced by branches or tags.

[alias]
tree-all = log --graph --decorate --pretty=format:'%C(bold)%h%Creset %C(cyan)%s%Creset %C(dim white)(%cr) %C(bold cyan)<%an>%Creset' --all --decorate --oneline --simplify-by-decoration

Example Usage:

git tree-all

This command provides a cleaner view of your commit history, highlighting only the commits that are referenced by branches or tags, making it easier to track significant changes.

3. newbranch Alias

The newbranch alias automates the creation of new branches with a consistent naming convention, including the current date, a random number, and a specific feature name.

[alias]
newbranch = "!f() { \
feature=$1; \
date=$(date +%Y%m%d); \
random=$RANDOM; \
branch_name=\"${date}-${random}-${feature}\"; \

git checkout -b \"${branch_name}\"; \
}; f"

Example Usage:

git newbranch login-page

This command creates a new branch named something like 20240211-12345-login-page, incorporating the current date, a random number, and the feature name login-page.

Creating and Managing Branches with GitHub CLI

Step-by-Step Workflow

  1. Create a New Branch:Use the newbranch alias to create a new branch with a descriptive name:git newbranch feature-name
  2. Add and Commit Changes:Make your changes, then add and commit them:git add .
    git commit -m "Implement feature-name"
  3. Push the Branch to Origin:Push your new branch to the remote repository:git push origin feature-branch-name
  4. Create a Pull Request:Use the GitHub CLI to create a pull request:gh pr create --title "Implement feature-name" --body "This PR implements the feature-name." --base main --head feature-branch-name
  5. Approve the Pull Request:Approve the pull request using the GitHub CLI:gh pr review --approve <PR_NUMBER>Replace <PR_NUMBER> with the actual pull request number.
  6. Merge the Pull Request with Rebase:Merge the pull request using the rebase strategy:gh pr merge --rebase <PR_NUMBER>Again, replace <PR_NUMBER> with the actual pull request number.