Git commands and tips, I use frequently
Part- 1
I have been using Git for almost four years and kept my day to day Git commands and tips in a document to just copy and use as and when required.
I just want to share my compilation of git commands and tips, I use in my day to day workflow. Hope it will help.
Git Aliases
This is not a command but if you are tired of writing a full git command, Git Aliases can make your life very easy. You can easily setup an alias for any Git Command. Treat it as a git shortcut.
For example:
Instead of “git checkout”, I use “git co”. So how you can set it up ?
Here we go, just write below line in your teminal
$ git config --global alias.co checkout
Here
$ git config --global
telling git to update global configuration file
alias.co
defining your alias name
checkout
is your main git command
You can set as many as aliases based on your requirements. I generally use for check last commit, complex logging, checking differences visually etc.
If you want to read more about aliases, please click here.
Advance Git Commands
Other than aliases here are some git commands, I use in my day to day activities. Today I will focus on viewing the commit history.
Have you used “git log”. What a pretty bored B/W output.
Instead of a black and white log of your commits, use below for a colourful, more visible log
$ git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
Above command seems very complex. Isn’t it?
Why don’t you use git alias for that. Copy and paste below line in your teminal
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
Woo hoo…now, what you all need to do is, use “git lg” to see your colourful logs
git lg
If you want to see, the log of lines changed, jus use
git lg -p
You can use other log options along with above alias.
For example:
-p
Show the patch introduced with each commit.
--stat
Show statistics for files modified in each commit.
--shortstat
Display only the changed/insertions/deletions line from the — stat command.
--name-only
Show the list of files modified after the commit information.
--name-status
Show the list of files affected with added/modified/deleted information as well.
--abbrev-commit
Show only the first few characters of the SHA-1 checksum instead of all 40.
--relative-date
Display the date in a relative format (for example, “2 weeks ago”) instead of using the full date format.
--graph
Display an ASCII graph of the branch and merge history beside the log output.
--pretty
Show commits in an alternate format. Options include oneline, short, full, fuller, and format (where you specify your own format).
--oneline
Shorthand for --pretty=oneline --abbrev-commit
used together.
You can read more about git logging by clicking at below reference.
Reference: https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History
Thats it for today. I will share some more useful git commands in this series.
Till then, have a great weekend.