What is your favourite Git command?

twitter logo ・1 min read

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

twitter logo DISCUSS (54)
markdown guide
 
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!!!!!

 

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 🤓
annotations

 
 
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

 
 
 

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

 

I use:

git reset HEAD --hard

My alias is:

grshard

I also have an alias forgit, which is just 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.

 

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 rebate coming up? Just git checkout -b with the same branch name first, but with -pre-merge at the end. If you mess it up bad, just switch to the other branch you made.

Coworker force pushed a branch you were branched off of, and now you can't pull easily? Rename your branch, then checkout their branch from upstream, and fix it locally.

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.

 
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

 

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.

 

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"

 

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

Can't be too careful!

 

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.

 
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"
 
 

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!

 
 

Amend is nice, I do it a lot on a daily basis.

git revert

For some unknown reason running git revert feels good to me 😄 It will basically undo what was done for the commit(s) specified.

I use it for removing buggy features right before a deployment or just broken code that was accidentally added. For this to work you would need to commit often and group features into their own commits.

Another weird use case is resetting a file. For ex. in one community repository I have a file called upcoming. When an event is upcoming that file is updated and when a event is over the update is reverted so the upcoming file reverts back to the placeholder text There are currently no events planned.... I might be abusing this command but it works for me 😄

 

The git bisect command checkouts all versions one by one between two revision until you find the version which is not working.

$ git bisect start
$ git bisect bad # Current version is bad
$ git bisect good v2.6.13-rc2 # v2.6.13-rc2 is known to be good

 
 

git checkout-b <name-of-new-sub-branch>

Basically, my most used git command. It creates a new branch off the current branch, it's amazing for new features and hot fixes or just random tests.

Closely followed by git status

Can't be too careful...

 

For me git reset --soft HEAD 1, I have a git alias for this git undo. This is particularly useful for me when I want to fix small mistakes in committed files, without adding a new commit.

Also, git reflog. Very useful to get info about commits from deleted branches.

 

I am really liking the new commands of git 2.23.

In my opinion, it was an excellent idea to split git checkout in git switch and git restore.

 

Not really a git command per se, but once I use the OhMyZSH shell, that have lots of useful alias to git commands, I love the alias gwip and gunwip.

GWIP

$ alias | grep gwip -
gwip='git add -A; git rm $(git ls-files --deleted) 2> /dev/null; git commit --no-verify --no-gpg-sign -m "--wip-- [skip ci]"'

This alias allows me to quickly commit everything, tracked and not tracked changes.

How to use

The current state of my branch is:

$ git status
On branch shipfast-on-docker_approov2-kotlin-wip
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   README.md

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    test.dev.to

no changes added to commit (use "git add" and/or "git commit -a")

Using gwip:

$ gwip
[shipfast-on-docker_approov2-kotlin-wip c7a96dd] --wip-- [skip ci]
 2 files changed, 1 insertion(+), 1 deletion(-)
 create mode 100644 test.dev.to

That results in this git log:

commit c7a96ddbcca0e4ef6d0f9085cd8d420610eccaa7 (HEAD -> shipfast-on-docker_approov2-kotlin-wip)
Author: Paulo Silva <paulos@criticalblue.com>
Date:   Mon Oct 7 15:25:41 2019 +0100

    --wip-- [skip ci]

 README.md   | 2 +-
 test.dev.to | 0
 2 files changed, 1 insertion(+), 1 deletion(-)

So the message --wip-- [skip ci] tells to the CI pipeline to not run for this commit, and at same time the --wip-- part will be used by the alias gunwip to undo what we have done with the gwip.

GUNWIP

$ alias | grep gunwip -                                                                                                                                                                                   
gunwip='git log -n 1 | grep -q -c "\-\-wip\-\-" && git reset HEAD~1'

The gunwip alias will undo any commit where the message contains --wip--, thus will undo what we have done with gwip.

$ gunwip
Unstaged changes after reset:
M   README.md

Leaving us with:

$ git status
On branch shipfast-on-docker_approov2-kotlin-wip
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   README.md

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    test.dev.to

no changes added to commit (use "git add" and/or "git commit -a")

Summary

During my day I use a lot this 2 commands in order to keep progressing in the code until I am finished with what I am doing, thus be able to proper commit the code.

I also push this wip commits upstream to be protected of an hard disk failure. I do this several times a day, and in the end of the day.

 
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.

 

git add -p for me. I often drift off and make a lot of changes while working on a particular problem, this helps me separate out individual changes to create better and more focused commits.

 

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
 
 

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

 

git log --follow <file path> shows the commit history for a file. It’s helpful if you are investigating changes over time. 🤓

 

git add <path> --patch and git rebase master --interactive

The former is for adding specific lines of code to stage, and the latter is for rebasing current branch on top of the master in interactive mode.

 
 
git rebase —interactive <hash of commit before i started hacking>
git push —force-with-lease

As I clean up my branch after addressing pull request comments and before we merge.

 
git retract --overflow --unprint-errors
git bless --retrograde
git bless --assume-mutable-gnomes
 
 

I wish this wasn't true but it's probably

git --reset hard

For when it truly all goes to the fan.

 

I learned to love:

git rebase --interactive [branch]

in order to re-shuffle, combine, split or re-word commits.
To re-write (re-create) a cleaner commit-history.

 

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.

 
git add .
git commit -m "fix all"

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

 
git status

Didn't really know about

git commit--amend

Sometimes, I am unhappy with my commit messages. Thank you for the knowledge @Muna Mohamed

 
 
 
 
 

Rebase. Resolves conflicts, interactive mode squashes commits together, probably does more I haven't looked into. It solves my most common git issues when working in a team.

 
 

My favorite git commad "git log". Because It's good to see who did what and when. Sometimes I even forget what i am doing. My second favorite is "git status".

Classic DEV Post from Mar 19

Jack of the Stack

Different developer types described by the alphabet.

Muna Mohamed profile image
Hi! My name is Muna and I’m a Frontend Developer/Full Stack Developer in Training based in London. Welcome to my space on the internet!