Git's 17th anniversary is on April 7th, 2022. Since its creation, Git has become a standard tool for software development. It helps programmers track changes they make to their code, and as a code hosting platform, it allows users to find useful applications. If you interact with source code, open source or otherwise, there's a strong likelihood you interact with Git. With so many people using Git daily, I wondered what the open source community's favorite Git commands are. What better way to find out than to ask?
git diff
One of the hidden gems of Git is its ability to do word-level diffs.
$ git diff --word-diff file1 file2
It's not just for Git repos, either. It can also diff arbitrary files:
$ git diff --word-diff file1 file2
git status
I can't live without the git status
command. This one is great if I've recently adjusted my .gitignore
, and I'm unsure if I got the syntax correct. It's also nice if I want to quickly see which PHP, SCSS, or JavaScript files are being committed before I do one last PHPCS run-through.
git cherry-pick
I use git cherry-pick
if I have been working on a branch and want to only keep one or two commits. I can pull out the changes from a single commit and apply them to the branch I'm working on now. It's useful when there are merge conflicts, but there is an important change(s) you need for some reason.
git blame
I use git blame
because invariably when I stare at code and ask, "who did this?!" the answer ends up being me. It's usually something I did long ago, and I've forgotten why it had to be that way. If it wasn't me, then it gives me a pointer to go and ask someone why they did it and why it had to be that way.
git bisect
My favorite has to be git bisect
. It helps identify the commit that introduced a bug by doing a binary search on commits. Simple to use but incredibly effective.
Also, if I create a script that tells me whether the current source is good or bad, then git bisect run
figures everything out on its own!
git stash
At work, I have to deal with customer projects in order to reproduce specific kinds of behavior. The git stash
command allows me to get back to the initial state very quickly after I've applied changes to the projects I want to revert.
Easily git stash
! I get derailed a lot into urgent matters (bug fixes) while coding a longer piece. I love how verbose it is. It matches how my brain works. But it's not only about being distracted into more urgent matters. Sometimes, stash
serves a higher purpose: The need to chase a divergent solution to something already solved. Maybe it's more elegant or chaotic. Maybe it's just a clean slate, a hiatus that might lead to nothing or something great. Either way, I can always pop back from it and return to my work in progress.
I love using git stash
and having the ability to stash changes that I'm not sure about and being able to recall them later. It is a great way to be able to noodle around with an idea.
git push
The first reason is I love git push
is that it simply allows you to share your work—your code—with others! The second reason is that push --force
is considered harmful and destructive, but I like to see it as a great power that comes with great responsibility. 😊
My practical answer: git add -p
because it allows me to have that extra review step before I commit my code. Also, I can quickly add just the relevant changes and then do away with any pseudocode or debugging nonsense that's leftover.
My real answer: git push
because it reminds me of the song "Kick, Push" by Lupe Fiasco and also means, "I did the thing and all that remains is opening a PR."
[ Keep this command on hand and more with our updated Git cheat sheet. ]
git merge
My favorite Git command is git merge
. Specifically, I love merging development branches into the master branch with the following:
$ git merge --strategy recursive -X theirs 3.17.x
The theirs
option within the recursive
merge strategy incorporates changes from their side, as long as the changes don't conflict with what they are getting merged into.
If you know you never want to override local values when doing a merge, the -Xours
flag is a big-time saver, especially when used to sync code between mirrored repos. Should it find a conflict, it forces Git to preserve the local value and ignore any incoming changes.
git reset
As I find myself constantly having to abandon changes or needing to make sure my local branch is synced with the remote, I use git reset
a lot. I also find it a helpful command as it allows me to reset my branch and go back in my repository history.
git reflog or rebase?
The command git reflog
is my Lord and savior, but git rebase -i
is my fave because I like to rewrite history.
Your git command
There are plenty of Git commands to choose from, so what's your favorite? Is there a dream command that Git's lacking? Tell us in the comments!
Comments are closed.