DEV Community

Muna Mohamed
Muna Mohamed

Posted on

What is your favourite Git command?

I'm curious to know, what is your favourite Git command?

Mine is "git commit --amend", which let's you change your last commit message. I manage to mess up a commit message at least once a day 😅

For the sake of clarity, please include what the git command does so we can all learn from one another! 😄

Top comments (90)

Collapse
 
alvaromontoro profile image
Alvaro Montoro
git blame

The git blame command shows what revision and author last modified each line of a file.

It can also be used to destroy friendships and create awkward moments at work when the application stops working and you want to check who made the last change to the line of code that breaks everything. Example of a possible conversation:

Steve: Production is down, and I don't know what's happening.
Muna: Did you change anything?
Steve: No! It just started happening all of a sudden.
Muna: There's an error on this line, let me check who made the change with git blame.
Steve: (gulp)
Muna: STEEEEEEEEEEEVE!!!!!

Collapse
 
opencode profile image
Apruzzese Francesco

I use Visual Studio Code addon to have blame output always on the code until I read it!!!!

Collapse
 
karataev profile image
Eugene Karataev

Yep, WebStorm also has this handy feature showing an author and date of change of every line.
I use it all the time 🤓
annotations

Thread Thread
 
zenulabidin profile image
Ali Sherief • Edited

Gitkraken also has blame built-in and will show the author and commit message to left of every line. Easier than using the terminal.

Collapse
 
bhupesh profile image
Bhupesh Varshney 👾

This is ✔️🤣

Collapse
 
elecweb profile image
Elecweb
git stash
git stash pop

for keeping file changes without commit. for example, you're doing your new feature and then bug found on production so you need to switch to new branch for hot fix. you can use git stash to keep your changes on feature branch and when you finish fixing bug you can use git stash pop for get back your file changes

Collapse
 
jdforsythe profile image
Jeremy Forsythe

I always find myself using git stash -u so it stashes new files in addition to changed files

Collapse
 
enzoftware profile image
Enzo Lizama Paredes

This is my favorite too!!

Collapse
 
bjornbreck profile image
Bjorn Breckenridge

git stash apply

Collapse
 
rafaelliendo profile image
Rafael Liendo
git reflog

To recover a deleted branch

Collapse
 
elecweb profile image
Elecweb

This one save my life

Collapse
 
ianwijma profile image
Ian Wijma

I might be wrong. But are branches not just archived when they are "deleted"? At least that's how I learner it. 🤔 Or can jou permanently delete a branch.

Collapse
 
guha profile image
Arghya Guha

Git runs a garbage collector automatically from time to time.
If you delete a branch and the commits on it have never been merged with another branch, they will, eventually, disappear.

Collapse
 
hyftar profile image
Simon Landry

Not just deleted branches, deleted commits also! :)

Collapse
 
ben profile image
Ben Halpern

I like to pull a git reset --hard when I need to start fresh.

Collapse
 
sethbergman profile image
Seth Bergman

I use:

git reset HEAD --hard

My alias is:

grshard

I also have an alias forgit, which is just g.

Collapse
 
manan30 profile image
Manan Joshi

I have aliased it as nuke

Collapse
 
mzaini30 profile image
Zen

Wow g 😂

Collapse
 
msamgan profile image
Mohammed Samgan Khan

I m up for this one.

Collapse
 
codeluggage profile image
Matias • Edited

git checkout -b == make a new branch off of your current branch and switch to it
git branch -m == rename your current branch

Branches are "free" in git. When I have mentored other engineers, the most empowering and liberating moment for them is almost always when they see git as a safety net. That tends to go hand in hand with the realization of how powerful branches are.

Got a nasty rebase coming up? Just git checkout -b with the same branch name first, but with some descriptive naming like -before-rebasing at the end. If things get messed up during the rebase, you can just undo everything and go back to before you started the rebase.

Coworker force pushed a branch you were branched off of, and now you can't pull easily due to conflicts? Rename your branch, then checkout their branch from upstream, and you can take your time and compare and fix it locally between 2 branches instead of in the middle of a pull.

Done some git acrobatics and aren't 100% sure then changes are right, but still want to push them up to remote? Make a new branch off of the remote branch so you have a local copy if anything goes wrong.

Made some large changes that you want to keep, but may be going in the wrong direction? Make a new branch, commit the changes, then go back to the original branch and continue working.

I highly recommend naming branches the same, but appending dashes with more explanatory comments, like -refactor or -test-stubs.

Git is a safety net that let's you relax and not worry about the state of your local folder, and liberally creating tons of branches is the key.

Collapse
 
ybbond profile image
Yohanes Bandung Bondowoso • Edited
git commit --amend --no-edit

is one of my favorite too! I even made an alias for this command.
it is now

git cane

my favorite too is

git rebase --continue

after resolving conflict with nvim

Collapse
 
briwa profile image
briwa • Edited

One of my favorites is git add -p. It does an interactive mode to review and stage changes as chunks as opposed to the whole file. Sometimes I would want to commit all of them, other times I'd like to keep some for the next commit or stash them instead. It's like git diff and git add combined.

Collapse
 
hamstu profile image
Hamish Macpherson
git nevermind

It's not a real git command, but it's an alias to wipe out any uncommitted changes, new files, etc. Basically gets you back to a clean state (i.e., the the last commit.) I do it all the time! (Warning: there's no going back once you run it though!)

This is how to set it up in your ~/.gitconfig:

[alias]
nevermind = !git reset --hard HEAD && git clean -d -f

Collapse
 
munamohamed94 profile image
Muna Mohamed

Ooo, that's an interesting one! Very fitting alias too, haha 😄! Will have to add that one to the toolbox 👍🏾. Thanks, Hamish!

Collapse
 
hamstu profile image
Hamish Macpherson

Glad you like it Muna! I love the name too, it always matches how I'm feeling when I run it. 😅

Collapse
 
hayderimran7 profile image
Imran Hayder

haha good one :D

Collapse
 
mazentouati profile image
Mazen Touati • Edited

Due to my OCD my favorite is git status and when I'm super anxious I'll add the untracked-files option git status --untracked-files="all"

Collapse
 
jckuhl profile image
Jonathan Kuhl

git status every single time I'm about to commit.

Can't be too careful!

Collapse
 
rohitshetty profile image
Rohit Shetty
git bisect

Git bisect saved my ass a few times last few weeks. Git bisect helps you find out when a previously unnoticed anomaly was introduced. You mark your current version as bad and some previous commit as good (where you know that anomaly didn't exist) and then Git bisect continues "bisecting" your commit history, you continue to mark the current version as either good or bad, ultimately identifying the commit where this unnoticed then bug was introduced.

This can also be used to identify commit when a certain feature was added too. In short, I find it useful especially working with a codebase that does not have much test coverage.