Everyday GIT
Cheat sheet
To speed up the code integration process most developers use SmartGit, SourceTree or other Git versioning client tool.
Things go touchy when you need to repeat same process in a command line. Hopefully there are several solutions for that.
This article will focus on Git commands & techniques you most likely use or need on daily basis.
Table of contents:1. Git aliases
2. Git log & graphs
3. Git “must know” commands
1. Git aliases
First element that can speed-up the code integration are git ‘aliases’. Using this technique, you can transform long and complex git commands into simple ones and share them easily with your team. This is a real time saver that avoids copy/paste bad habits.
First things first, let’s verify what is under the hood of our global/local git configuration:
--global
: stands for ‘global config file’ ~/.gitconfig--local
: stands for ‘local config file’ /.git/config-l
: stands for ‘list all’
$ git config --global -luser.name=Kamil Kubicki
user.email=dev@email.com
pull.rebase=true
My configuration is alias free - an excellent practice🎮ground! Now we will discuss two ways to add the git alias.
First method relies on Git config file content modification:
Globally: ~/.gitconfig
Locally in project: /.git/config
New alias should be included below [alias]
section:
[alias]
...
Second method consists on using the command line:
$ git config --global alias.<your_alias> <referenced_git_command>
To explore the power of alias, I propose you to start with a simple example for three following commands:
- git checkout
- git status
- git log
In order to add an alias for each command, we need to amend the Git config file:
[alias]
co = checkout
st = status
l = log
Easy as is, isn’t it? We can immediately test the changes:
$ git st
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: src/market/src/Item/index.tsxChanges not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: package.json
modified: src/App.tsx
modified: yarn.lockUntracked files:
(use "git add <file>..." to include in what will be committed)
src/market/node_modules/
src/market/package.json
src/market/src/index.tsx
src/market/yarn.lock
$ git co
M package.json
M src/App.tsx
A src/-market/src/Item/index.tsx
M yarn.lock
$ git l
commit c6cc91d1ac411fee13f32f9f636b29fbe8581e43 (HEAD -> master)
Author: Kamil Kubicki <dev@email.com>
Date: Sat May 28 18:46:09 2022 +0200Initialize project using Create React App
The same result achieved using a command line is shown below (remember to use --global
or --local
parameter depends on your configuration requirements):
$ git config --global alias.co checkout
$ git config --global alias.st status
$ git config --global alias.l log$ git config --global -l
user.name=Kamil Kubicki
user.email=dev@mail.com
pull.rebase=true
core.autocrlf=input
alias.co=checkout
alias.st=status
alias.l=log
Great job! We are ready to jump into more complex definitions discussed under next section.
2. Git log & graphs
(!) Before going any further, please read carefully below section.
Troubleshooting: A problem was noticed on Windows OS. The screen freezes when user tries to resize the terminal with git log longer than one terminal screen.
To handle that issue we can get rid of git pager (not recommended method for significant repositories) or reduce the number of commits that will be prompted on the screen.
$ git config --global core.pager cat
$ git config --global core.pager less -FX
cat
: tells to not use pager and write output directly to the command lineless --FX
: paginate only when the output is longer than the terminal screen — type Q to exit the pager
Alternatively, instead of cat
configuration you can use --no-pager
parameter directly inside the command.
$ git --no-pager log
The solution, that suits best our needs is to reduce the lines of command’s result using -n
parameter followed by the number of commits you wish to print in terminal (you will notice that the output is verbose, this is something we will focus on in a minute).
$ git --no-pager log -n 10
Well done! Now we are ready to move forward and usegit log
command.
As you probably guessed, the command displays commit logs. However, the power of log depends on other parameters that help to take full advantage of the git feature and take the output to the next leverl— a bit closer to the notorious SmartGit or SourceTree display tree results.
--no-pager
: tells Git to not use pager--oneline
: shorthand for ‘ --pretty=oneline --abbrev-commit’. Retrieves partial prefix for commit object name and ensures output’s squashing to one line per commit-n 10
: defines max number of commits in output--decorate
/--no-decorate
: prints out the ref names
(example: HEAD -> 6.2, origin/HEAD, origin/6.2)--graph
: an excellet way to display the state of our repository in a human readable way. Draws a text-based graphical representation of the commit history
$ git log --oneline --decorate -n 10
$ git log --oneline --no-decorate -n 10
$ git log --graph --oneline --decorate -n 10
The goal of this article is to speed-up daily routine - we will add now previous command to our alias list — as we learned in previous section:
$ git config --global alias.cl "log --graph --oneline --decorate -n 10"$ git cl
Voila! Our alisas showing git branches in terminal is fully operational 🎊.
Bonus:
Git is awesome — I bet, you already know that 😄It won’t be then surprising for you that we can go a step further and define the decoration of log output command.
--pretty=format:"..."
helps to define the structure of each commit line displayed on the screen. Here are main format elements used in this article’s examples:
%h
: (abbrev) commit hash%d
: ref names%s
: commit subject%cn
: committer name%cr
: committer date
Every part of pulled information can be also highlighted with %C(your_chosen_color)
parameter — this one needs to be placed straight before the element to decorate.
Github official documentation about colours:
The basic colors accepted are `normal`, `black`, `red`, `green`, `yellow`,`blue`, `magenta`, `cyan` and `white`. The first color given is theforeground; the second is the background. All the basic colors except`normal` have a bright variant that can be speficied by prefixing thecolor with `bright`, like `brightred`
Hex Code #RRGGBB colours format can be also used to decorate your prompted lines.
$ git log --graph --pretty=format:"%C(yellow)%h %C(green)%d %C(white)%s" -n 10
$ git log — graph — pretty=format:"%C(yellow)%h %C(blue)<%an> %C(red)(%cr) %C(green)%d %C(white)%s" -n 10
Thank you for attention and hope you have learned something new.
More information about you can find here: https://git-scm.com/doc
3. Git “must know” commands
Git init
Creates a brand new git repository in the current directory.
$ git init
Git status
Shows project status (displays current branch name and impacted files).
$ git statusOn branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: src/market/src/Item/index.tsxChanges not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: package.json
modified: src/App.tsx
modified: yarn.lockUntracked files:
(use "git add <file>..." to include in what will be committed)
src/market/node_modules/
src/market/package.json
src/market/src/index.tsx
src/market/yarn.lock
Below is a short version of the command that might be handy if you require minimalist display of changes (s stands for --short
, b for --branch
):
$ git status -sb## master
M .idea/vcs.xml
M package.json
M src/App.tsx
AM src/coin-market-ticker/src/Item/index.tsx
M yarn.lock
?? src/market/node_modules/
?? src/market/package.json
?? src/market/src/index.tsx
?? src/market/yarn.lock
M = modified
A = added
D = deleted
R = renamed
U = updated
?? = not tracked
Git add
Adds a file to the index (once added, the file can be comitted/validated and sent to a distant repository). If you don’t want to specify the files one by one and add them all at once, you can use a dot .
instead the filename.
$ git add <your_local_file_path>
or
$ git add .
Git commit
Validates all files previously added to index — each commit action should integrate a message.
$ git commit –m “Your brilliant commit message”
Git diff
Shows the difference (modifications in files) between local content and remote repository.
$ git diff
Git fetch
Gets the content from remote repository — it helps to visualise the distant changes. The command is different from git pull
cause does not merge the code and all local files and changes stay as are.
$ git fetch
Git pull
Gets the remote state of content and sync local files with the distant reference repository.
$ git pull
Git push
Sends local changes to the remote repository.
$ git push
Git branch
There are three main reason of using the command — to list/create or delete an existing branch.
List branches available locally.
$ git branch
Add new branch.
$ git branch <your_branch_name_to_create>
Delete existing branch.
$ git branch -d <your_branch_name_to_delete>
Git checkout
Updates local files to match the remote version.
$ git checkout <your_local_file_path>
Creates a new branch and switches to it by updating the index and the files in the working tree.
$ git checkout -b <your_new_branch_name>
If the branch you want switch to already exists, you can use the command below:
$ git checkout <your_existing_branch_name>
Git merge
Mergeyour_existing_branch_name
content into the current branch.
$ git merge <your_existing_branch_name>
Git log
In its simplest version it displays commit’s informations in desc order (commit id, author, date & message etc.).
$ git logcommit c6cc91d1ac411fee13f32f9f636b29fbe8581e43 (HEAD -> master)
Author: Kamil Kubicki <dev@email.com>
Date: Sat May 28 18:46:09 2022 +0200Initialize project using Create React App