Ammend
Ever committed a change and instantly realized you forgot something? Maybe a typo in the commit message or an extra file you meant to include? Instead of making a new commit, you can fix the last one with the amend flag. Let's say you just made a commit:
git commit -m "Fix navbar bug"
But oops! You forgot to add a file. Instead of creating a new commit, stage the missing file and run:
git commit --amend
This updates your last commit, adding any new changes. If you only want to fix the message, use:
git commit --amend -m "Fix navbar responsiveness issue"
If you've already pushed the commit, be careful! Amending rewrites history, so you’ll need to force-push (git push --force) to update the remote branch.
Rebase
Rebase is a powerful tool that helps keep your Git history clean and linear. Instead of merging branches (which creates extra merge commits), rebasing moves your commits on top of another branch.
git checkout feature
git rebase main
Before rebasing:
main: A --- B --- C
\
feature: D --- E
After rebasing:
main: A --- B --- C --- D --- E
Bisect
Got a bug and don't know which commit caused it? Let Git do the detective work with bisect. It uses binary search to find the commit that introduced a bug. Start by running:
git bisect start
git bisect bad # Mark the current commit as bad
git bisect good # Mark an older known good commit
Git will check out a commit in between. Keep marking until you find the culprit. Eventually there will be no more revisions left to inspect, and the command will print out a description of the first bad commit.When you’re done, run:
git bisect reset
Archive
Want to send your project's source code but without the Git history? Instead of manually deleting .git, just run:
git archive --format=zip HEAD -o project.zip
Shortlog
If you want a quick summary of the contributions in your repository, git shortlog can help. It groups commits by author and summarizes the commit history.
git shortlog
git shortlog -s #Shows a summary with only the number of commits for each author.