Everyday Git by Aditya Tyagi

Everyday GIT

Not another git cheatsheet you say?? There are a ton of git cheatsheets already. It is highly unlikely that i’ll ever refer to a cheat sheet when I can just google that sh** in 5 min and go through 5 Stack Overflow answers. Copy-paste random things until something works!


Why another Git Cheatsheet?

I beg to differ. I say this because most of the git cheat-sheets out there are very generic and have commands that I will rarely use. In my 4 years of dev career, which is pretty small as compared to some of the veterans out there, but still have managed to narrow down to the following git commands.

These are the commands that I use daily, almost. Some of these are the ones that come handy when I f*** up and f*** up bad. Some of these are just because I am too lazy to type.

Why a blog? Why a publicly available blog?

This is majorly a self-note blog post. A reference point for me. No matter where I am, and irrespective of the organisation I am working for, this blog post will be the go-to point.

What’s in it for you?

Nothing special, but might also be everything you want. To be honest, bookmark it, or ignore it if you feel it’s too much. But the following are the commands that I have used and I am positive that I’ll be using it in near future!

GIT commands

I want toCommand
Delete a branch locallygit branch -d localBranchName
Delete a branch remotelygit push origin --delete remoteBranchName
Delete a branch locally and remotelygit branch -d localBranchName && git push origin --delete remoteBranchName
Check the remote of repo with urlgit config --get remote.origin.url
Change author of last commitgit commit --amend --author="Author Name email@address.com"

:w

:q
Delete tag on remotegit push --delete origin tagname
Delete tag locallygit tag --delete tagname
Delete a remotegit remote rm remoteName
Squash (N is the number of commits)git reset HEAD~N

Reference 1
Delete a stash by stash numbergit stash drop stash@{n}
Delete all unstaged filesgit checkout .

git reset --hard HEAD
Unstage all staged filesgit reset
Revert last mergegit reset --hard HEAD~1
Reset and sync local repository with remote branchgit fetch origin && git reset --hard origin/master && git clean -f -d

Reference 1
Show only last N number of commitsgit log --oneline -N
Remove all unstagged changesgit checkout -- .
Remove git from a reporm -rf .git*
Merge without merge commitReference 1
Revert last pushed commitgit revert commitHash
Rename branchgit branch -m newName
Check if a branch contains a particular commitgit branch --contains | grep commitHash | grep branchName

Useful GIT resources

ReadMe and Markdown

  1. ReadMe and Markdown Cheatsheet

Git Alias

Git Alias are nothing but git shortcuts. For example:

cp = cherry-pick
st = status -s
cl = clone
ci = commit
co = checkout
br = branch
diff = diff --word-diff
dc = diff --cached

A progammer uses simple commands like fetch, pull, merge, rebase, push on a daily basis and like a 1000 times. There are commands that are simple, yet verbose to type. Git Alias helps to create shortcuts for these git scripts.

You can add alias one at a time:

$ git config --global alias.co checkout
$ git config --global alias.br branch
$ git config --global alias.ci commit
$ git config --global alias.st status

or you can add them all at once. To setup git alias, we need to edit the .gitconfig file and add the aliases there. The path of this file can vary depending on the operating system you are working on.

Generally,

  1. MacOS: ~/.gitconfig
  2. Linux: ~/.gitconfig
  3. Windows: It can be different based on system settings. A starting point

To quickly open, try running git config --global --edit in terminal and it is highly possible that it will open in your default editor.

There are literally a ton of aliases that you can use and what works for me, might not work for you. Thus, will drop few links here, wherein you can select the aliases that works for you.

  1. Must Have Git Aliases: Advanced Examples by Nicola Paolucci
  2. Sample .gitconfig file: Check here or here

The most powerful feature of git alias is positional parameters. Positional parameters, in simple words are the values to your alias functions. Yes, functions.

my_alias = "!f() { 〈your complex command〉 }; f"

You can write alias functions that take arguments (just like normal functions) and run the script with them. The only thing you have to take care of is the order in which you pass parameters while invoking alias.

  • $1 to mean the first parameter passed to the command.
  • $2 to mean the second parameter passed to the command. (and so on for $3,
    $4, etc.)

For example, if you want to delete branch locally and remotely but don’t want to remember the long git script

git branch -d localBranchName && git push origin --delete remoteBranchName

You can create a git alias function

delAll = "!f() { \
         git branch -D $1 && git push origin --delete $1; \
       }; f"

Now, all you have to do is pass the branch name you want to delete locally and on remote. You can fire this beauty by:

git delall mybranchName

Multiple Git SSH accounts

  1. Specifying the SSH key to use

Special Mentions

  1. Conventional Commits: A specification for adding human and machine readable meaning to commit messages

Leave a Reply

Your email address will not be published. Required fields are marked *