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.

Photo by Yancy Min on Unsplash

Setting a repository

It’s recommended that first of all you assign a username and email.

$ git config --global user.name <name>
$ git config --global user.email <email>
  • From an existing project in your local machine
$ git init
$ git remote add <remote>
$ git clone <remote>

Basic commands

It’s important to know that git provisions 3 different areas that are the core of the repository. The first one is the working directory which is the root of your Git project, here the files go through any change that the user performs. The staging area, also known as index, is where changes are built up, in this area all files are converted to tracked files by git. And the last one called as the commit area safely stores all files already committed. As seen in the image below, there’s another area called Remote Repository, that represents the remote location of the origin repository.

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

When working on projects, specifically with other team members involved, a lot of changes are made through the different files in the repository. In order to display modifications in detail between versions of these files, a diff is needed.

// 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

When reviewing the commit history in our repository, a HEAD label is shown in the list of commits, this reference points out to the last commit in the branch that is currently being used. It serves as a starting point from where the next commit is going to take place.

Commit history.
$ git checkout <commit_id>
Detached HEAD
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>
Fast forward merge
// in master branch
$ git merge --no-ff <branch_name>
No fast forward merge
3-way merge

Amend and rebase

The amend command lets us combine changes in the staging area with the last commit, without generating a new entirely commit. This is very helpful when dealing with premature commits where we can easily forget to staged one or various files. Although, this command aggregates the stage changes to the last commit, a new commit is created with its own new reference (commit id), and the previous commit reference is lost. Also amend command is used to change the most recent commit message.

// 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
Rebase

Tagging and stash

When a project reaches a milestone in its development, a good practice is to mark this specific point in the repository history, these markers are called tags. Tags are usually used to identify stable releases and to keep track of the project’s versions. There are two types of tags: lightweight and annotated. Lightweights are just like pointers to specific commits in the history, while annotateds are objects containing tagger’s name, email and date.

// 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

The commands and their basics options presented in this article are at least the basics you should know when working with Git. Git is a powerful and great tool used and known for almost all developers. So I recommend you to go ahead and explore deeper about the different commands and the advanced options they present.

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