Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

There are many sources that you can use to learn about Git, such as Git documentation, Github document, Git Handbook, and etc. I will try to gather the most common commands and their descriptions here.

Install Git

First of all let's to learn how to install git on Ubuntu:

sudo apt install git-all

You can find the guide to installing git on other OSs here.

Configuration Git

The first thing you should do when you install Git is to set your user name and email address. This is important because every Git commit uses this information, and it’s immutably baked into the commits you start creating:

git config --global user.name "Your Username"
git config --global user.email "Your Email Address"

Start with Git

clone, add, commit, push and pull

clone

The Clone command Clones (make a copy) a repository into a newly created directory, creates remote-tracking branches for each branch in the cloned repository, and creates and checks out an initial branch that is forked from the cloned repository’s currently active branch.

git clone {url-of-project}
# example: git clone https://gitlab.com/{username}/{project-name}.git

add

This command updates the index using the current content found in the working tree, to prepare the content staged for the next commit. 

The git add command adds a change in the working directory to the staging area. It tells Git that you want to include updates to a particular file in the next commit. However, git add doesn't really affect the repository in any significant way—changes are not actually recorded until you run git commit.

# add an spacial file
git add {file-name}

# add all files
git add .

commit

The git commit command captures a snapshot of the project's currently staged changes. 

git commit -m "commit message"
  • In Git, commit is the term used for saving changes.
  • Git does not add changes to a commit automatically. You need to indicate which file and changes need to be saved before running the Git commit command.
  • The commit command does not save changes in remote servers, only in the local repository of Git.

push

The git push command is used to upload local repository content to a remote repository. Pushing is how you transfer commits from your local repository to a remote repository.

git push <remote> <branch>
# example: git push origin master

pull

The git pull command is used to fetch and download content from a remote repository and immediately update the local repository to match that content.

git pull <remote> <branch>
# example: git pull origin master

Repository

A Git repository is the . git/ folder inside a project. This repository tracks all changes made to files in your project, building history over time. Meaning, if you delete the . git/ folder, then you delete your project's history.

Create a new repository

git clone https://gitlab.com/{username}/{project-name}.git
cd {project-name}
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master

Push an existing folder

cd existing_folder
git init
git remote add origin https://gitlab.com/{username}/{prooject-name}.git
git add .
git commit -m "Initial commit"
git push -u origin master

Push an existing Git repository

cd existing_repo
git remote rename origin old-origin
git remote add origin https://gitlab.com/{username}/{project-name}.git
git push -u origin --all
git push -u origin --tags

Branches

It is common for a Git repo to have different branches. They are a great way to work on different features and fixes while isolating the new code from the main codebase.

Repos often have a master branch for the main codebase and developers create other branches to work on different features.

Create new branch:

git branch {new-branch-name}

Switch between branches:

git checkout {branch-name}

You can combine these commands:

git checkout -b {new-branch-name}

Once work is completed on a feature, it is often recommended to delete the branch.

// delete branch locally
git branch -d localBranchName

// delete branch remotely
git push origin --delete remoteBranchName

Merge Branches

Consider this situation: you create a branch, suppose named it version2, from the master project and make changes on it, then switch to the master branch and make other changes on it. Now you find that you need to these changes (on the master branch) on the version2 branch. You should transport these changes to the version2 branch. How would you go about doing that?

Merge

The simplest approach is to merge the changes:

$ git checkout version2
$ git merge master

If there are differences, then merge will apply the commits to the top of version2 and create a new merge commit. Otherwise, the merge will be resolved by a fast-forward.

Rebase

Alternatively, there’s rebase:

$ git checkout version2
$ git rebase master

Rebase, on the other hand, moves all diverging commits of feature to the top. This means that the diverging commits will have new hashes because history will be rewritten.

Accordingly, if you’ve previously pushed your version2 branch to remote, then the only way to update it is with force push:

$ git push origin version2 --force

However, if developers have checked out your version2 branch, then I don’t recommend this method. Stick with the merge.

Cherry Pick

Let's consider a special situation: we have two branches on a project (master and version2). we make changes on both of them and then commit and push them. Now we need to merge some of the master branch commits to the version2 branch. A lot of people have suggested using cherry-pick for this situation, so take your pick on whatever one makes the most sense to you!

git checkout name-of-the-correct-branch

# grab the last commit to master
git cherry-pick master

# grab the special commit to master
git cherry-pick master <commit-id>

# grab the multiple commit to master
git cherry-pick master <commit-id-1> <commit-id-2>

# grab all the commits from commit 1 to commit 2 (where 1 is older than 2) to master
git cherry-pick master <commit-id-1>^..<commit-id-2>
# If you want to ignore 1 itself, run:
git cherry-pick master <commit-id-1>..<commit-id-2>

# delete it from master
git checkout master
git reset HEAD~ --hard