Unlocking the Power of Git: 20 Tips & Tricks for Beginners to Advanced Users
Git is one of the most powerful tools in a developer’s toolbox, but mastering it can take time. Whether you're just getting started with Git or looking to sharpen your skills, this guide has got you covered. From basic to advanced tips, these commands and techniques will help you take control of your version control.
1. Undo the Last Commit Without Losing Changes
Sometimes you commit prematurely, but you don’t want to lose your changes. Use this command to undo your last commit without removing your work:
1. Undo the Last Commit Without Losing Changes
Sometimes you commit prematurely, but you don’t want to lose your changes. Use this command to undo your last commit without removing your work:
git reset --soft HEAD~
This keeps your changes staged but undoes the commit. To remove both the commit and changes:
git reset --hard HEAD~
Use this carefully, as it completely removes uncommitted changes.
2. Stash Uncommitted Changes
When you need to switch branches but don’t want to commit your current changes, stash
is your friend:
git stash
When you're ready to resume:
git stash apply
Or, remove the stash after applying:
git stash pop
This lets you cleanly switch branches while keeping your work intact.
3. View a File’s History Over Time
Want to know how a file has evolved? Use this command to trace the history, even if it has been renamed:
git log --follow <filename>
This helps in debugging or understanding why a file changed.
4. Amend the Last Commit
Made a mistake in your last commit or forgot to include a file? No need to create a new commit—just amend it:
git commit --amend
This opens the commit message editor, and you can add changes or modify the message without creating a new commit.
5. Compare Staged vs. Unstaged Changes
You may have changes in your working directory and changes staged for the next commit. To see the difference between these two states:
git diff
To compare the staged changes with your last commit:
git diff --staged
This helps you keep track of what’s in your staging area versus what you’re still working on.
6. Checkout a File from a Previous Commit
Need to revert a file to its state in a previous commit without switching branches? No problem:
git checkout <commit-hash> – <filename>
This retrieves the specific file version but keeps you on the current branch.
7. Quickly View Commit History
For a concise and easy-to-read commit log, use this one-liner:
git log --oneline
For an even better visual overview of branches and merges, use:
git log --oneline --graph --all
8. Squashing Multiple Commits
If you want to clean up your commit history before merging, squash multiple commits into one:
git rebase -i HEAD~3
This opens an interactive editor where you can choose to squash commits, keeping your history clean and easy to follow.
9. See Who Made Changes to Each Line
Ever wondered who to blame for a bug? This command shows you who last modified each line of a file:
git blame <filename>
It’s not just for finding mistakes; it helps you understand the context of changes as well.
10. Ignore Changes to a Tracked File
Sometimes you don’t want to commit changes to certain files, like configuration files. Use this to tell Git to ignore modifications:
git update-index --assume-unchanged <file>
Later, if you want Git to start tracking changes to the file again:
git update-index --no-assume-unchanged <file>
This is useful for avoiding accidental commits of environment-specific files.
11. Efficient Branch Switching
Switch between the last two branches quickly:
git checkout -
This is a huge time saver when you need to toggle between two branches frequently.
12. Delete a Local Branch
After merging or abandoning a feature branch, you can delete it with:
git branch -d <branch-name>
If the branch isn’t fully merged, force-delete it:
git branch -D <branch-name>
13. Pull with Rebase (Avoid Merge Commits)
When pulling from a remote branch, you might end up with unnecessary merge commits. Instead, pull with rebase to create a linear history:
git pull --rebase
This keeps your commit history clean and avoids merge clutter.
14. List All Branches (Local and Remote)
To see all local and remote branches, use:
git branch -a
This is handy for checking what branches exist both on your machine and in the remote repository.
15. Check What Files Are Being Tracked
Want to see which files Git is tracking in your current repository?
git ls-files
This lists all tracked files.
16. Reverting a File to the Last Commit
Need to discard local changes to a file and reset it to the latest commit version?
git checkout HEAD – <filename>
This discards uncommitted changes, resetting the file to its last committed state.
17. Clean Up Untracked Files
If you have untracked files or directories cluttering your workspace, remove them with:
git clean -f
To remove untracked directories as well:
git clean -fd
This helps you maintain a tidy workspace.
18. View a File’s Commit History
To view every file that has ever been committed to the repository:
git log --pretty=format: --name-only --diff-filter=A | sort | uniq
This gives you a complete overview of all committed files.
19. Creating Git Aliases
Speed up your workflow by creating Git command shortcuts. For example:
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
Now you can use git co
instead of git checkout
, git ci
instead of git commit
, and so on.
20. Rewriting Commit History with Rebase
If you need to modify or squash past commits:
git rebase -i <commit-hash>
This opens an interactive editor where you can edit, squash, or reword past commits to make your history cleaner and more meaningful.
Bonus Section: More Handy Git Commands
If you're still hungry for more Git goodness, here are a few additional commands that can further streamline your workflow.
Ignoring Tracked Files After They Have Been Committed
Sometimes, files that are already tracked in your Git repository (like configuration or environment files) need to be ignored temporarily. You can use the following command to tell Git to ignore changes to any tracked file:
git update-index --assume-unchanged <file>
This command is helpful when you don't want environment-specific or local configuration files to be accidentally committed, but still want them in your working directory.
If you later want Git to start tracking changes to the file again, simply use:
git update-index --no-assume-unchanged <file>
This will restore Git’s ability to detect and track changes for the file.
Conclusion:
These Git commands and techniques will not only save you time but also enhance your version control practices. Whether you’re a beginner or an advanced Git user, there’s always something new to learn. As you continue to grow as a developer, mastering Git will give you confidence in managing your codebase with ease.
May the code be with you! Thanks for joining us on this coding journey.