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 (98)

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 on

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

This is my favorite too!!

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
 
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
 
mattdevio profile image
Matt G

My favorite command is...

git log --all --graph --decorate --oneline

My alias for it is gitpretty. Git + Ascii art, what more could you want.

Collapse
 
kev_barbe profile image
Kevin Barbe

This is also one of my favorites, my ridiculous alias for this one is gloga, standing for graphical log all (branches).

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 on

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 on
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
 
mazentouati profile image
Mazen Touati • Edited on

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
 
briwa profile image
briwa • Edited on

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
 
drhyde profile image
David Cantrell • Edited on

commit --amend is useful, but I think I have to vote for cherry-pick. It makes trivial what can be a right pain in some other version control systems. And if I can cast two votes, let the second one be for bisect.

My least favourite is commit -a -m "...". It makes it too easy to commit many unrelated changes with an unhelpful message. And of course I use it all the damned time.

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.

Collapse
 
olegthelilfix profile image
Oleg Aleksandrov

git push --force origin master

Collapse
 
ianwijma profile image
Ian Wijma

For when you just want to go home right?

Collapse
 
olegthelilfix profile image
Oleg Aleksandrov

Nope, in case, if I would like to be fired as fast as possible.
Of course before that command I should run a few another command, rm - R . & git add . & git commit -m"init of project".
And probably never be hired again ;))

Collapse
 
moopet profile image
Ben Sinclair

Honestly?

git merge --abort
Collapse
 
pcdevil profile image
Attila Gonda

I am a Terminal user, and I even stage my files from the command line.

The most used and my favourite command is:

git add --patch

With this I can choose what hunk I want to add to the staged status. This is very powerful tool which can be a bit confusing, but just a little bit of practice I really liked when I discovered.

It also helped me to practice TDD because if the dirty file list is huge, it takes too much time to figure out what to add. If you want to do small iterations between commits, this kind of enforces you too!

Timeless DEV post...

Git Concepts I Wish I Knew Years Ago

The most used technology by developers is not Javascript.

It's not Python or HTML.

It hardly even gets mentioned in interviews or listed as a pre-requisite for jobs.

I'm talking about Git and version control of course.