Here are some of the top git commands which I handpicked, that can be useful to increase your productivity, and also save your some time.
1) Updating the last commit message. If you want to update the message of your last commit, because either it didn't look conventional or meaningful, use this command to edit it:
git commit --amend -m "Updated message"
2) Blank Commit.
Sometimes we need a blank commit (no changes added or deleted) maybe we need to initialize our repo or trigger some kind of action, we can use this command:
git commit --allow-empty -m "blank commit message"
3) Checking the total number of commits.
If we want to check the total no of the commit on a particular branch we can use this command.
git rev-list --count
4) Checking files from different a branch.
If we want to view some files from a different branch while working on a different branch, we can use this command
git show :
eg git show main:README.md
5) Staging (adding) and committing the changes in a single command.
Instead of doing git add and git commit separately, we can use this single command to perform this
git commit -am "message"
6) Record of all the commits
git reflog
with this handy command, you can get the record of all commits of all in done, now you can check and change which commit you wanna go
git reset HEAD@{your index name }
for example here below the image, can need to see all the commit details I'll change to head 31 so I'll put it to git reset HEAD@{31}
The default action for git reset is git reset --mixed. It means that the changes in your working directory will be preserved but not staged (since the index was modified to match the chosen commit, the changes are not in the index anymore).
other options are :
git reset --soft
is this command doesn't modified the index, or working tree leaving your changes staged for commit.
git reset --hard
this git reset --hard command will be caution, as it resets both the index and working tree. Uncommitted changes and all commits after will be removed.
7) Let’s face those merge conflicts
you're on hairy merge conflict , but after comparing two conflicting versions you still have no idea which one is correct.
git checkout --conflict=diff3
Resolving merge conflict is not all fun and games, but this command makes a life little bit easier
Often you need more context to decide which branch is correct. By default, Git shows you a version of the markers that contains versions of the two files that have a conflict. By choosing the option above you will be able to see the base version as well, which can hopefully save you some trouble. You can also set it as default with:
git config --global merge.conflictstyle diff3
💡 "Resolving merge conflicts is fun!" - said no one ever.
8)Let autocorrect take care of it
You’re pretty proud of your breakneck typing speed, but at the same time you can’t even remember how many times you typed “git stauts” instead of “git status” and it leaves you mildly annoyed.
git config --global help.autocorrect
Git Autocorrect is a convenient option for all the impatient devs out there. The integer value represents a tenth of a second. Choosing 30 will give you 3 seconds to change your mind and stop the operation - otherwise Git will assume you meant the instruction most similar to the one you wrote. Don’t worry though, if you type something that is not even close to a Git command, Git will give up on guessing and print an error message instead.
9) Git tutorial on the terminal.
Yes , you heard is right, with only single command use can have a whole git tutorial at your fingertips
git help tutorial
Ever wondered what to do when one is asked to change the commit-msg and accidentally types git --amend -m "New commit-msg"? This merges the new changes with the previous commit and makes a whole new commit.
But then we are told to keep the review id the same (i.e keep the previous commit only)
Now how to go back to the previous state without losing our changes? Though there can be many ways to solve it, I discovered to solve it via "git reflog".
git reflog: Reflogs track when Git refs were updated in the local repository. Using it we can reset our head to the point before we made the amend.
$ git reflog ... d0c9f22 HEAD@{0}: commit (amend): [Feature] - ABC Commit Description
Then you can do
git reset --soft c296452