Boost Your Git Productivity with These 5 Tips

In this article, we’ll explore five practical tips to enhance your productivity when working with Git. Whether you’re a seasoned developer or just getting started, these tips cover useful Git aliases, time-saving workflows, and smart configurations.

Handy Git Aliases:

When using Git from the command line, aliases can significantly streamline your workflow. Consider creating aliases like “git nah” to undo changes and reset your project quickly. This alias goes beyond a simple reset and includes cleaning up directories and files, as well as aborting a rebase if one is in progress.

bash

[alias]
    nah = "!f(){ git reset --hard; git clean -df; if [ -d ".git/rebase-apply" ] || [ -d ".git/rebase-merge" ]; then git rebase --abort; fi; }; f"

Another useful alias is “git forget,” which helps clean up local branches after merging. It fetches the remote, lists branches, filters out merged branches, and deletes the local versions.

bash

[alias]
    forget="!git fetch -p && git branch -vv | awk '/: gone]/{print $1}' | xargs git branch -D"
    forgetlist="!git fetch -p && git branch -vv | awk '/: gone]/{print $1}'"

Be cautious with the “forget” alias, as it deletes branches locally, and use “forgetlist” for a read-only view of branches that will be deleted.

Git Uncommit:

The “git uncommit” alias is handy for situations where you need to remove the latest commit before pushing changes to a remote repository.

bash

[alias]
    uncommit = reset --soft HEAD~1

This command effectively rewinds the branch by one commit while keeping the changes from that commit in the staging area.

Configure a Git Commit Template:

Improve the consistency and clarity of your commit messages by configuring a commit template. This can be done globally or per project.

bash

# Configure a commit.template globally
git config --global commit.template ~/.gitmessage
 
# Configure a commit.template in a project
git config commit.template .gitmessage

Craft your commit messages with a template that includes a one-line summary, context, reasons for the change, and a breakdown of what the commit does.

Use a Password Manager for SSH Keys:

Enhance security and convenience by managing SSH keys with a password manager like 1Password. By using 1Password as your SSH agent, you can authorize SSH operations with biometric scanning, and your private key never has to reside in your ~/.ssh folder.

Smart Branch Cleanup Workflow:

Streamline your branch cleanup process after merging by fetching the remote, listing branches, and deleting local versions.

bash

[alias]
    forget="!git fetch -p && git branch -vv | awk '/: gone]/{print $1}' | xargs git branch -D"
    forgetlist="!git fetch -p && git branch -vv | awk '/: gone]/{print $1}'"

This workflow is useful for maintaining a tidy local repository after merging branches.

Conclusion:

By incorporating these tips into your Git workflow, you can save time, maintain a cleaner repository, and enhance the overall efficiency of your development process. Experiment with these suggestions and tailor them to fit your specific needs and preferences. Happy coding!

Here's a list of 100 commonly used Git commands for developers:

Basic Commands:

  1. Initialize a Repository:

    git init
    
  2. Clone a Repository:

    git clone <repository_url>
    
  3. Add Changes to Staging:

    git add <file_name>
    
  4. Commit Changes:

    git commit -m "Commit message"
    
  5. Check Repository Status:

    git status
    
  6. View Changes between Working Directory and Last Commit:

    git diff
    

Branching and Merging:

  1. Create a New Branch:

    git branch <branch_name>
    
  2. Switch to a Branch:

    git checkout <branch_name>
    
  3. Create and Switch to a New Branch:

    git checkout -b <branch_name>
    
  4. List Branches:

    git branch
    
  5. Merge Branch into Current Branch:

    git merge <branch_name>
    

Remote Repositories:

  1. Add a Remote Repository:

    git remote add <remote_name> <repository_url>
    
  2. List Remote Repositories:

    git remote -v
    
  3. Fetch Changes from Remote Repository:

    git fetch
    
  4. Pull Changes from Remote Repository:

    git pull
    
  5. Push Changes to Remote Repository:

    git push
    

Viewing History:

  1. View Commit History:

    git log
    
  2. View Brief Commit History:

    git log --oneline
    
  3. View Branch History Graph:

    git log --graph --oneline --all
    
  4. Show Changes in a Specific Commit:

    git show <commit_hash>
    

Undoing Changes:

  1. Discard Changes in Working Directory:

    git checkout -- <file_name>
    
  2. Discard Changes in Staging:

    git reset HEAD <file_name>
    
  3. Undo the Last Commit:

    git reset --soft HEAD^
    
  4. Discard Changes in Working Directory and Staging:

    git reset --hard HEAD
    

Tagging:

  1. Create a Tag:

    git tag <tag_name>
    
  2. Create an Annotated Tag:

    git tag -a <tag_name> -m "Tag message"
    
  3. List Tags:

    git tag
    

Collaboration:

  1. Create a Pull Request:

    # This command is repository-specific, and the syntax may vary based on the platform (GitHub, GitLab, etc.)
    
  2. Fetch a Pull Request:

    git fetch origin pull/<pull_request_id>/head:<branch_name>
    
  3. Cherry-pick a Commit from Another Branch:

    git cherry-pick <commit_hash>
    
  4. Rebase onto Another Branch:

    git rebase <base_branch>
    

Configuration:

  1. Configure User Information:

    git config --global user.name "Your Name"
    git config --global user.email "your.email@example.com"
    
  2. Configure Default Editor:

    git config --global core.editor "editor_name"
    
  3. Configure Merge Tool:

    git config --global merge.tool "merge_tool_name"
    

Stashing:

  1. Stash Changes:

    git stash
    
  2. List Stashes:

    git stash list
    
  3. Apply Latest Stash:

    git stash apply
    
  4. Apply a Specific Stash:

    git stash apply stash@{2}
    

Working with Remotes:

  1. Remove a Remote:

    git remote rm <remote_name>
    
  2. Rename a Remote:

    git remote rename <old_name> <new_name>
    

Submodules:

  1. Add a Submodule:

    git submodule add <repository_url> <path>
    
  2. Update Submodules:

    git submodule update --init --recursive
    
  3. Remove a Submodule:

    git submodule deinit -f -- <submodule_path>
    

Advanced History Rewriting:

  1. Interactive Rebase:

    git rebase -i <base_branch>
    
  2. Squash Commits during Rebase:

    # Replace "pick" with "squash" or "s" in the interactive rebase file
    
  3. Amend the Last Commit:

    git commit --amend
    
  4. Rewrite Commit Author Information:

    git commit --amend --author="Author Name <email@example.com>"
    

Gitignore:

  1. Create a Gitignore File:

    touch .gitignore
    
  2. Ignore Files or Directories:

    # Add file or directory names to .gitignore
    

Searching:

  1. Search in Commit Messages:

    git log --grep="search_term"
    
  2. Search in Changes:

    git log -S"search_term"
    

Bisect:

  1. Start Bisecting:

    git bisect start
    
  2. Mark a Commit as Bad:

    git bisect bad
    
  3. Mark a Commit as Good:

    git bisect good
    
  4. Finish Bisecting:

    git bisect reset
    

Reflog:

  1. View Reflog:

    git reflog
    
  2. Restore to a Previous State using Reflog:

    git reset --hard HEAD@{2}
    

Git Subcommands:

  1. Git Clean – Remove Untracked Files:
    git clean -fd
  1. Git Submodule – Initialize and Update:

    git submodule update --init --recursive
    
  2. Git Archive – Create a Tarball:

    git archive --format=tar.gz --output=output.tar.gz <branch_name>
    

Advanced Diff:

  1. View Word Diff:

    git diff --word-diff
    
  2. Ignore Whitespace in Diff:

    git diff -w
    
  3. Show Only Names of Changed Files:

    git diff --name-only
    

Worktrees:

  1. Create a New Worktree:

    git worktree add <path> <branch_name>
    
  2. List Worktrees:

    git worktree list
    
  3. Remove a Worktree:

    git worktree remove <path>
    

Bisect (Advanced):

  1. Automate Bisect with a Script:
    git bisect start <bad_commit> <good_commit>
    git bisect run <test_script>
    

Signing Commits:

  1. Sign a Commit:

    git commit -S -m "Signed commit message"
    
  2. Verify a Signed Commit:

    git log --show-signature
    

File Blame:

  1. View File Blame:
    git blame <file_name>
    

Work with Shallow Clones:

  1. Create a Shallow Clone:
    git clone --depth 1 <repository_url>
    

Git LFS:

  1. Track Large Files with Git LFS:

    git lfs track "*.pdf"
    
  2. View Git LFS Objects:

    git lfs ls-files
    

Hooks:

  1. Create a Pre-Commit Hook:

    touch .git/hooks/pre-commit
    
  2. Create a Post-Commit Hook:

    touch .git/hooks/post-commit
    

Subversion Integration:

  1. Clone a Subversion Repository:
    git svn clone <svn_repository_url>
    

GPG:

  1. Generate a GPG Key:

    gpg --gen-key
    
  2. List GPG Keys:

    gpg --list-secret-keys --keyid-format LONG
    

Log Options:

  1. Log with Author Names:

    git log --pretty=format:"%h %an %s" --graph
    
  2. Log for a Specific Author:

    git log --author="Author Name"
    
  3. Log for a Specific Period:

    git log --since="2 weeks ago" --until="1 day ago"
    

Revert:

  1. Revert a Commit:

    git revert <commit_hash>
    
  2. Revert a Range of Commits:

    git revert <start_commit_hash>^..<end_commit_hash>
    

Archive:

  1. Create an Archive of the Repository:
    git archive --format=tar.gz --output=output.tar.gz HEAD
    

Fetch:

  1. Fetch a Single Branch:

    git fetch origin <branch_name>
    
  2. Fetch Tags:

    git fetch --tags
    

Grep:

  1. Search for a String in Files:

    git grep "search_term"
    
  2. Search for a String in Commits:

    git log -S"search_term"
    

Amend:

  1. Amend the Last Commit Message:

    git commit --amend -m "New commit message"
    
  2. Amend the Author of the Last Commit:

    git commit --amend --author="Author Name <email@example.com>"
    

Config:

  1. List Config Settings:

    git config --list
    
  2. Edit Config Settings:

    git config --global --edit
    

External Diffs:

  1. Use External Diff Tool:

    git difftool
    
  2. Use External Merge Tool:

    git mergetool
    

Work with Remotes:

  1. Mirror a Repository:

    git clone --mirror <repository_url>
    
  2. Push to All Remote Branches:

    git push --all
    

Pull Requests:

  1. List Pull Requests:

    git pull-request list
    
  2. Open a Pull Request:

    git pull-request create
    

Index:

  1. Manipulate the Index:
    git ls-files
    git update-index --assume-unchanged <file_name>
    

Misc:

  1. Exit Merge Conflict State:
    git merge --abort
    

These commands cover a wide range of Git functionalities, from basic to advanced. Depending on your specific use cases, you might find some commands more useful than others. Feel free to explore and incorporate them into your workflow as needed.

1 thought on “Boost Your Git Productivity with These 5 Tips”

Leave a Comment