DevWonders

Logo
Github Logo

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.
                            
                        

Worktree

Imagine you are deep into coding a new feature on your branch, and suddenly you are asked to fix an urgent bug on the main branch. Instead of stashing your messy changes or creating temporary commits just to switch branches, git worktree allows you to check out another branch in a completely separate physical folder on your computer. You can have both branches open in your editor at the same time!

                            
                                # Creates a new folder called '../hotfix' and checks out 'main' there
                                git worktree add ../hotfix main
                            
                        

Rerere (Reuse Recorded Resolution)

If you do long-running branches or frequent rebases, you probably hate resolving the exact same merge conflict multiple times. rerere stands for "Reuse Recorded Resolution". When enabled, Git watches how you solve a conflict. If it ever sees that exact same conflict again in the future, it will automatically apply your previous solution without asking you.

                            
                                # Enable it globally once and let Git do the work
                                git config --global rerere.enabled true