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:
Initialize a Repository:
git init
Clone a Repository:
git clone <repository_url>
Add Changes to Staging:
git add <file_name>
Commit Changes:
git commit -m "Commit message"
Check Repository Status:
git status
View Changes between Working Directory and Last Commit:
git diff
Branching and Merging:
Create a New Branch:
git branch <branch_name>
Switch to a Branch:
git checkout <branch_name>
Create and Switch to a New Branch:
git checkout -b <branch_name>
List Branches:
git branch
Merge Branch into Current Branch:
git merge <branch_name>
Remote Repositories:
Add a Remote Repository:
git remote add <remote_name> <repository_url>
List Remote Repositories:
git remote -v
Fetch Changes from Remote Repository:
git fetch
Pull Changes from Remote Repository:
git pull
Push Changes to Remote Repository:
git push
Viewing History:
View Commit History:
git log
View Brief Commit History:
git log --oneline
View Branch History Graph:
git log --graph --oneline --all
Show Changes in a Specific Commit:
git show <commit_hash>
Undoing Changes:
Discard Changes in Working Directory:
git checkout -- <file_name>
Discard Changes in Staging:
git reset HEAD <file_name>
Undo the Last Commit:
git reset --soft HEAD^
Discard Changes in Working Directory and Staging:
git reset --hard HEAD
Tagging:
Create a Tag:
git tag <tag_name>
Create an Annotated Tag:
git tag -a <tag_name> -m "Tag message"
List Tags:
git tag
Collaboration:
Create a Pull Request:
# This command is repository-specific, and the syntax may vary based on the platform (GitHub, GitLab, etc.)
Fetch a Pull Request:
git fetch origin pull/<pull_request_id>/head:<branch_name>
Cherry-pick a Commit from Another Branch:
git cherry-pick <commit_hash>
Rebase onto Another Branch:
git rebase <base_branch>
Configuration:
Configure User Information:
git config --global user.name "Your Name" git config --global user.email "your.email@example.com"
Configure Default Editor:
git config --global core.editor "editor_name"
Configure Merge Tool:
git config --global merge.tool "merge_tool_name"
Stashing:
Stash Changes:
git stash
List Stashes:
git stash list
Apply Latest Stash:
git stash apply
Apply a Specific Stash:
git stash apply stash@{2}
Working with Remotes:
Remove a Remote:
git remote rm <remote_name>
Rename a Remote:
git remote rename <old_name> <new_name>
Submodules:
Add a Submodule:
git submodule add <repository_url> <path>
Update Submodules:
git submodule update --init --recursive
Remove a Submodule:
git submodule deinit -f -- <submodule_path>
Advanced History Rewriting:
Interactive Rebase:
git rebase -i <base_branch>
Squash Commits during Rebase:
# Replace "pick" with "squash" or "s" in the interactive rebase file
Amend the Last Commit:
git commit --amend
Rewrite Commit Author Information:
git commit --amend --author="Author Name <email@example.com>"
Gitignore:
Create a Gitignore File:
touch .gitignore
Ignore Files or Directories:
# Add file or directory names to .gitignore
Searching:
Search in Commit Messages:
git log --grep="search_term"
Search in Changes:
git log -S"search_term"
Bisect:
Start Bisecting:
git bisect start
Mark a Commit as Bad:
git bisect bad
Mark a Commit as Good:
git bisect good
Finish Bisecting:
git bisect reset
Reflog:
View Reflog:
git reflog
Restore to a Previous State using Reflog:
git reset --hard HEAD@{2}
Git Subcommands:
- Git Clean – Remove Untracked Files:
git clean -fd
Git Submodule – Initialize and Update:
git submodule update --init --recursive
Git Archive – Create a Tarball:
git archive --format=tar.gz --output=output.tar.gz <branch_name>
Advanced Diff:
View Word Diff:
git diff --word-diff
Ignore Whitespace in Diff:
git diff -w
Show Only Names of Changed Files:
git diff --name-only
Worktrees:
Create a New Worktree:
git worktree add <path> <branch_name>
List Worktrees:
git worktree list
Remove a Worktree:
git worktree remove <path>
Bisect (Advanced):
- Automate Bisect with a Script:
git bisect start <bad_commit> <good_commit> git bisect run <test_script>
Signing Commits:
Sign a Commit:
git commit -S -m "Signed commit message"
Verify a Signed Commit:
git log --show-signature
File Blame:
- View File Blame:
git blame <file_name>
Work with Shallow Clones:
- Create a Shallow Clone:
git clone --depth 1 <repository_url>
Git LFS:
Track Large Files with Git LFS:
git lfs track "*.pdf"
View Git LFS Objects:
git lfs ls-files
Hooks:
Create a Pre-Commit Hook:
touch .git/hooks/pre-commit
Create a Post-Commit Hook:
touch .git/hooks/post-commit
Subversion Integration:
- Clone a Subversion Repository:
git svn clone <svn_repository_url>
GPG:
Generate a GPG Key:
gpg --gen-key
List GPG Keys:
gpg --list-secret-keys --keyid-format LONG
Log Options:
Log with Author Names:
git log --pretty=format:"%h %an %s" --graph
Log for a Specific Author:
git log --author="Author Name"
Log for a Specific Period:
git log --since="2 weeks ago" --until="1 day ago"
Revert:
Revert a Commit:
git revert <commit_hash>
Revert a Range of Commits:
git revert <start_commit_hash>^..<end_commit_hash>
Archive:
- Create an Archive of the Repository:
git archive --format=tar.gz --output=output.tar.gz HEAD
Fetch:
Fetch a Single Branch:
git fetch origin <branch_name>
Fetch Tags:
git fetch --tags
Grep:
Search for a String in Files:
git grep "search_term"
Search for a String in Commits:
git log -S"search_term"
Amend:
Amend the Last Commit Message:
git commit --amend -m "New commit message"
Amend the Author of the Last Commit:
git commit --amend --author="Author Name <email@example.com>"
Config:
List Config Settings:
git config --list
Edit Config Settings:
git config --global --edit
External Diffs:
Use External Diff Tool:
git difftool
Use External Merge Tool:
git mergetool
Work with Remotes:
Mirror a Repository:
git clone --mirror <repository_url>
Push to All Remote Branches:
git push --all
Pull Requests:
List Pull Requests:
git pull-request list
Open a Pull Request:
git pull-request create
Index:
- Manipulate the Index:
git ls-files git update-index --assume-unchanged <file_name>
Misc:
- 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.
“Fantastic!”