Everyday GIT
Tags: git, github, webdev, tutorial
Published: 2022-06-30 10:18:22

## Might not be for you, but might be exactly what you need!

Not another git cheatsheet you say?? There are a ton of git cheatsheets already there are 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 the go-to point.

What’s in it for you?

Nothing special but 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 more!

![Not another GIT Cheatsheet](https://miro.medium.com/max/450/1*PHRg1zjcUYALnD41hySVFg.jpeg)

---

## Useful GIT resources

![Find the complete table here](https://miro.medium.com/max/630/1*2wvwCbuH-P3VlYGu_X9N_w.png)

[Find the complete table here](https://adityatyagi.com/index.php/2022/01/08/everyday-git/)

---

## ReadMe and Markdown

1.  [ReadMe and Markdown Cheatsheet](https://github.com/adam-p/markdown-here/wiki/Markdown-Here-Cheatsheet)

---

## Git Alias

![Git Alias](https://miro.medium.com/max/469/1*oy3SI_AFTke3Hb5A_srNWg.jpeg)

Git Alias are nothing but `git` shortcuts. For example:

```bash
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:

```bash
$ 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 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](https://stackoverflow.com/questions/2114111/where-is-the-global-git-config-data-stored)

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 where in you can select the aliases that works for you.

1.  [Must Have Git Aliases: Advanced Examples by Nicola Paolucci](https://www.durdn.com/blog/2012/11/22/must-have-git-aliases-advanced-examples/)
2.  Sample `.gitconfig` file: Check [here](https://github.com/durdn/cfg/blob/master/.gitconfig) or [here](https://gist.github.com/johnpolacek/69604a1f6861129ef088)

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

```bash
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

```bash
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](https://devconnected.com/how-to-clone-a-git-repository/#Specifying_the_SSH_key_to_use)

## Special Mentions

1.  [Conventional Commits: A specification for adding human and machine readable meaning to commit messages](https://www.conventionalcommits.org/en/v1.0.0/)

---  

Thanks for reading ❤

If this blog was able to bring value, please follow me [here](https://dev.to/adityatyagi)! Your support keeps me driven!

Originally published on [adityatyagi.com](https://adityatyagi.com/index.php/2022/01/08/everyday-git/)

Want to connect?
Follow me on [Twitter](https://twitter.com/secondbestcoder) and [LinkedIn](https://www.linkedin.com/in/aditya-tyagi-03b4189a/) or reach out in the comments below!