10 Git Commands You’ll Wish Yo... Note

10 Git Commands You’ll Wish You Knew Earlier

This article presents a curated list of intermediate-level Git commands essential for daily development, beyond basic add, commit, pull, and push. It starts by contrasting git merge and git rebase, explaining how merge preserves historical divergence while rebase creates a cleaner, linear history by rewriting commits. The author advises rebasing individual work but exercising caution with shared history due to its history-rewriting nature. Conflict resolution strategies for both merge and rebase are discussed, along with the utility of aborting these operations.The git commit --amend command is introduced as a way to append small, forgotten changes to the previous commit, improving commit history aesthetics. It's noted that amend creates a new commit, changing its hash, which necessitates careful handling when pushing to a remote branch. This leads into git push --force-with-lease, a safer alternative to --force for pushing rewritten history, as it prevents accidental overwriting of others' work.The "king of Git," git rebase -i (interactive rebase), is then detailed, highlighting its power to transform messy commit histories into clean, logical sequences. Various subcommands like pick, reword, edit, squash, fixup, and drop are explained, demonstrating how developers can manipulate, combine, and remove commits to craft an elegant history. The article acknowledges different opinions on commit history cleanliness but emphasizes the value of this command.Next, git stash is presented as a crucial command for temporarily saving uncommitted changes when needing to switch contexts, such as fixing a production bug. It covers stashing untracked files with the -u flag and the importance of naming stashes for better organization. Commands for listing, showing, applying, and popping stashes are provided, outlining the workflow for managing temporary changes.Finally, git cherry-pick is introduced as a highly useful command for selectively applying a specific commit from one branch to another without merging or rebasing entire branches. This is particularly helpful for incorporating individual fixes or features from other development lines. Overall, the article serves as a practical guide to elevate Git skills beyond the basics, offering commands that address common real-world development scenarios.