niedziela, 25 lutego 2018

Git: from n00b to (almost) a master

A comprehensive guide to understanding Git

In this article I gathered all the advice I give to people who learn Git.
It will help you understand Git's "philosophy" and work with Git effectively.
Keep in mind that these will be Git's essentials for everyday use - don't expect for Git's "magic" (i.e. creating Git hooks) to be described here.

Commandline vs GUI

I found it the most convenient to work with Git using its commandline.
If you got used to graphical tools, then you might feel reluctant to use the commandline.
But Git was invented as a console tool, and many people - when they give its commandline a chance - find it as the natural way of using Git.

The very beginning: interactive git tutorial

The best interactive tutorial I managed to find on internet is: https://learngitbranching.js.org.
It will guide you from the total novice level up to quite advanced topics of Git.

learngitbranching: graph of the current state of a tutorial repository

Its uniqueness lies in the way it helps you understand of what is going on: the state of Git repository, after every command you type, is being represented as a graph - displayed in the right part of the screen.
After some time, you won't even need to see the graph - you will build it your head :)
And this is very useful ability for working with Git: git's commits and branches make a graph - and this graph is the current state of your interaction with a git repository. So, the ability to imagine the current graph, how the graph transformed after your previous command, is quite crucial. After all, you want to know what's going on before typing the next command...

But ok, you might notice that I recommend commandline for Git, and I emphasize on imagining the repository state as a graph.
So probably, you would need from time to time to see the current graph. It's available in every Git GUI tool, but how about the console?
I use two git commands to draw the graph in console, as ascii art: "git lol" and "git lola".
They are not built into git command, you have to define them as aliases - and I describe how to do it in the next paragraph.

Useful git aliases

Git allows you call very long, complicated command in a shorter way - by defining aliases for them.

Printing repository graph: git lol and git lola

These two commands will display a pretty graph of commits of your project to console:
log --graph --decorate --pretty=oneline --abbrev-commit --all --date=short
log --graph --decorate --pretty=oneline --abbrev-commit
The difference between them is that the first one shows all branches in the project and the second one - only the branch that is currently checked out.
But calling these commands in this verbose form wouldn't be very handy. To solve this problem, let's define aliases for them.

Git aliases are defined in ".gitconfig" file, placed in your home directory (that will be "/home/your_username/.gitconfig" for Linux/macOS or "c:\Users\your_username\.gitconfig" for Windows), in "alias" section of this file.
So, aliases for the above graph-printing commands are defined like this:

[alias]
 lol = log --graph --decorate --pretty=oneline --abbrev-commit
 lola = log --graph --decorate --pretty=oneline --abbrev-commit --all --date=short
After placing it in ".gitconfig" file, you can run two new commands: "git lol" and "git lola".

Even shorter: shell aliases

For two Git commands I run most often ("git status" and "git diff"), I use even a shorter way to call them: just "gs" and "gd", respecitvly.
But you cannot define these as Git aliases (as Git aliases always consists of typing the word "git" and the command name) - you have to define them in your shell.

So, for bash that means placing:

alias gs='git status'
alias gd='git diff'
in "~/.bashrc" file.
Calling most often used commands by typing just two characters in console is extremely useful for me.

Other aliases

The above aliases are all that I found useful, but you can take a look at some other.
Many people use set of aliases called "gitkurwa": https://github.com/jakubnabrdalik/gitkurwa

Git bisect

Work in Progress....

(...)It will stop the bisection process, but stay in your current branch.(...)

Brak komentarzy:

Prześlij komentarz