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! 😄

Oldest comments (90)

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
 
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
 
eperedo profile image
Eduardo P. Rivero
git stash save "i am totally not to forget this changes"
git commit --amend -m "Previous name was too awful I need to change it"
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
 
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
 
bhupesh profile image
Bhupesh Varshney 👾

This is ✔️🤣

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
 
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
 
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
 
arrlancore profile image
arrlancores
git add .
git commit -m "fix all"

when I forgot to commit some fixes and to many thing to recall ⚡️

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
 
enzoftware profile image
Enzo Lizama Paredes

This is my favorite too!!

Collapse
 
bjornbreck profile image
Bjorn Breckenridge

git stash apply

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
 
ispirett profile image
Isaac Browne

git merge branch-name lol

Collapse
 
raisaugat profile image
Saugat Rai

I like "git checkout - " which let you checkout to previous branch.

Collapse
 
kevinhch profile image
Kevin

git push origin master --force :)

Collapse
 
flexyko profile image
xFlex

Satan ?

Collapse
 
wchr profile image
Wachira

I love this git command

$ git stash

It lets me save uncommitted changes so I can do some rebasing, solve a couple of conflicts then get back to my work

$ git stash pop
Collapse
 
stephanie profile image
Stephanie Handsteiner • Edited

git stash to save uncommitted changes.
git stash pop to continue working on them, after you had to take a break to work on something more important.

Collapse
 
ecoupal profile image
ecoupal

git rebase -i