1. Navigating and Searching
1.1 View the Last Commit Log
git log -1
Sometimes, you just need to quickly check what the last commit was without digging through a long history. This command lets you view the most recent commit in one step.
1.2 Search Commit Messages for a Keyword
git log --grep="fix bug"
When working on a large project, finding specific commits can be like finding a needle in a haystack. This command allows you to search through commit messages to locate the exact change you need.
1.3 Search for a Specific Change in History
git log -S "function_name"
Ever wondered when a particular function or line of code was introduced? This command lets you track down which commit first introduced a specific piece of code.
1.4 Show a Graphical Representation of the Branch History
git log --oneline --graph --decorate --all
If you’re working with multiple branches and frequent merges, the commit history can get messy. This command provides a clear, visual representation of the branch history, making it easier to understand.
2. Staging and Committing Changes
2.1 Stage All Changed Files Quickly
git add -u
If you’ve modified or deleted files but don’t want to stage new untracked files, this command is perfect. It stages only updated and removed files, keeping your commit clean.
2.2 Commit Changes with a One-Liner
git commit -am "Updated feature X"
Want to save time by staging and committing in one step? This command lets you do that, ensuring you keep your workflow smooth and efficient.
2.3 Change the Last Commit Message
git commit --amend -m "New commit message"
Made a typo in your last commit message? No worries! This command allows you to update it without creating an additional commit.
2.4 Split Large Commits into Smaller Ones
git reset HEAD~
git add -p
Sometimes, you realize that your last commit contained too many changes. By resetting and using git add -p
, you can interactively choose which changes to stage and create cleaner, more meaningful commits.
3. Working with Branches
3.1 Create a New Branch and Switch to It
git checkout -b feature-branch
No need to run two commands separately. This one-liner creates a new branch and switches to it immediately, saving you time and keeping your workflow seamless.
3.2 Delete a Local Branch
git branch -d feature-branch
Finished working on a feature? This command safely removes a local branch, ensuring your workspace stays tidy.
3.3 Delete a Remote Branch
git push origin --delete feature-branch
Once a remote branch is no longer needed, this command helps you remove it from the remote repository to avoid clutter.
3.4 List All Merged Branches
git branch --merged
Before deleting branches, it’s a good idea to check which ones have already been merged. This command lists all merged branches, helping you clean up safely.
3.5 Switch to the Previous Branch
git checkout -
If you find yourself switching back and forth between two branches frequently, this simple command can save you a lot of time.
4. Undoing Changes
4.1 Discard All Local Changes
git reset --hard HEAD
Sometimes, you just want a fresh start without committing anything. This command resets your working directory to the last committed state, discarding any uncommitted changes.
4.2 Unstage a File Without Losing Changes
git reset HEAD filename
Accidentally staged a file? Use this command to remove it from staging while keeping your modifications intact.
4.3 Revert a Commit Without Losing History
git revert HEAD
Unlike git reset
, this command keeps history intact by creating a new commit that undoes the changes from a previous commit.
4.4 Restore a Deleted File
git checkout -- filename
Accidentally deleted a file? No worries, this command can bring it back from the latest commit.
4.5 Reset to a Previous Commit and Keep Changes
git reset --soft HEAD~1
If you want to go back to an earlier commit but keep your changes unstaged, this command is the way to go.
5. Enhancing Logs and Diffs
5.1 Show a Compact Commit History
git log --oneline --decorate --graph
Displays a concise, readable log of commits.
5.2 View Changes in a File Since Last Commit
git diff HEAD filename
See exactly what has changed in a specific file.
5.3 Compare Two Branches
git diff branch1..branch2
Highlights the differences between two branches.
5.4 View Changes Between Two Commits
git diff commit1 commit2
Compare the changes introduced between two commit hashes.
Conclusion
By mastering these 50 secret Git commands, you can significantly improve your workflow and save hours each week. Whether you’re navigating commit histories, staging files efficiently, managing branches, or undoing changes, Git has powerful tools to make your life easier.
Start incorporating these commands into your workflow today, and you’ll notice an immediate boost in productivity! Have a favorite Git command that’s not on this list? Let me know in the comments!