Git, a quick and simple guide.

This guide will give you a solid base to start working with git, or a reference for the experienced developers.

Image for post
Image for post
Photo by Yancy Min on Unsplash

Setting a repository

$ git config --global user.name <name>
$ git config --global user.email <email>
$ git init
$ git remote add <remote>
$ git clone <remote>

Basic commands

Image for post
Image for post
Git areas and principal commands.
// to add only one specific file 
$ git add <file_name>
// to add all files in working directory
$ git add .
// if you want to unstage a file
$ git restore --staged <file_name>
$ git commit -m <commit_message>
$ git commit -am <commit_message>
$ git status 
$ git status -s
$ git log// to view a compact version of the commit history
$ git log --oneline
$ git log --graph
// revert the last commit
$ git revert HEAD
// revert to and specific commit id
$ git revert <commit_id>
// only for staging
$git reset
// both, working directory and staging
$ git reset --hard
$ git fetch origin master
$ git pull origin master
$ git push origin master

Diff

// diff working dir - staging
$ git diff
// diff working dir - commit area
$ git diff HEAD
// diff staging - commit area
$ git diff --staged HEAD
// diff specific commit - commit area
$ git diff <commit_id> HEAD
// diff commit area - remote
$ git diff master origin/master
// diff branch - commit area
$ git diff <branch_name> master

Branches

Image for post
Image for post
Commit history.
$ git checkout <commit_id>
Image for post
Image for post
Detached HEAD
Image for post
Image for post
Representation of a master branch and feature branch
// list all branches
$ git branch -a
// create a branch
$ git branch <branch_name>
// rename branch
$ git branch -m <branch_name> <new_branch_name>
// delete branch
$ git branch -d <branch_name>
// to move between branches
$ git checkout <branch_name>
// in master branch
$ git merge <branch_name>
Image for post
Image for post
Fast forward merge
// in master branch
$ git merge --no-ff <branch_name>
Image for post
Image for post
No fast forward merge
Image for post
Image for post
3-way merge

Amend and rebase

// amend staged changes changing commit message
$ git commit --amend -m <message>
// amend staged changes without changing commit message
$ git commit --amend --no-edit
// in feature branch
$ git rebase master
// if conflict exits
$ git rebase --abort
// when conflicts are solved
$ git rebase --continue
Image for post
Image for post
Rebase

Tagging and stash

// lightweight tag
$ git tag <tag_name> <commit_id>
// annotated tag
$ git tag -a <tag_name> -m <tag_message> <commit_id>
// to compare tags
$ git diff <tag1> <tag2>
//to update tag
$ git tag -a <tag_name> --force <commit_id>
// to delete tag
$ git tag <tag_name> --delete
// to push tags to remote
$ git push origin --tags
// to push only annotated tags to remote
$ git push origin --follow-tags
//to delete tag in remote
$ git push origin :<tag_name>
// to stash
$ git stash
$ git stash save <message>
// to list all stashes
$ git stash list
// to see files stashed (stashes are listed as "stash@{1}")
$ git stash show <stash>
// to retrieve stashed files
$ git stash apply <stash>
// to delete stash
$ git stash drop <stash>
// to retrieve and delete stash
$ git stash pop <stash>
// to delete stashes
$ git stash clear
// to create branch from stash
$ git stash branch <name branch> <stash>

Conclusion

Written by

I write about software development, AI, ML and other stuff that I find interesting. || Software Developer and AI-ML enthusiast

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store