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)
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:
I use Visual Studio Code addon to have blame output always on the code until I read it!!!!
Yep, WebStorm also has this handy feature showing an author and date of change of every line.
I use it all the time š¤
Gitkraken also has blame built-in and will show the author and commit message to left of every line. Easier than using the terminal.
This is āļøš¤£
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 usegit stash pop
for get back your file changesI always find myself using
git stash -u
so it stashes new files in addition to changed filesThis is my favorite too!!
git stash apply
To recover a deleted branch
This one save my life
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.
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.
Not just deleted branches, deleted commits also! :)
I like to pull a
git reset --hard
when I need to start fresh.I use:
My alias is:
I also have an alias for
git
, which is justg
.I have aliased it as
nuke
Wow
g
šI m up for this one.
git checkout -b
== make a new branch off of your current branch and switch to itgit branch -m
== rename your current branchBranches 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.
is one of my favorite too! I even made an alias for this command.
it is now
my favorite too is
after resolving conflict with
nvim
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 likegit diff
andgit add
combined.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
Ooo, that's an interesting one! Very fitting alias too, haha š! Will have to add that one to the toolbox šš¾. Thanks, Hamish!
Glad you like it Muna! I love the name too, it always matches how I'm feeling when I run it. š
haha good one :D
Due to my OCD my favorite is
git status
and when I'm super anxious I'll add the untracked-files optiongit status --untracked-files="all"
git status
every single time I'm about to commit.Can't be too careful!
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.