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)
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.
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 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.
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:
This is ✔️🤣
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.
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
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! :)
when I forgot to commit some fixes and to many thing to recall ⚡️
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 changesThis is my favorite too!!
git stash apply
I always find myself using
git stash -u
so it stashes new files in addition to changed filesgit merge branch-name lol
I like "git checkout - " which let you checkout to previous branch.
git push origin master --force :)
Satan ?
I love this git command
It lets me save uncommitted changes so I can do some
rebasing
, solve a couple of conflicts then get back to my workgit 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.
git rebase -i