A simple guide to start with GIT

Pamoda Wimalasiri
5 min readNov 10, 2018

--

Git is a version control system for tracking changes in local files and the collaborative work of multiple people. It is generally used for source-code management in software development but can be used to keep track of the changes of any set of files.

Set up git :

Follow this link — https://git-scm.com/downloads

Create a new repository locally

Let’s create a local repository. You can run the following command inside the directory.

git init

Create a Local Copy of a Git repository

git clone path/to/remote/repo

Add a remote

Remotes are used to communicate the local changes with the external world. You can push changes in the local disk to a remote repository or you can pull changes from a remote repository.

git remote add <remote> <remote_url>

You can verify the remotes you have added,

git remote -v

GIT 3 tree Architecture

To understand the behavior of git, we can use the metaphor of the three trees. These are different collections of files with different status.

  1. working directory — holds the actual files
  2. Index or Staging — the place where files get prepared to be packaged into a commit object
  3. HEAD — points to the last commit that is done
3 Tree Architecture

The above diagram explains the three tree architecture of git.

Stage Changes

You need to add your changes to the staging area before committing them to the local repository.

git add <file_tobe_added>git add *

If you want to know what is in your staging area or the index, run

git status

Did you add anything that you do not want to commit? You can simply remove files from the staging by running

git rm --cached <file_tobe_removed>

Commit Changes

Tracked files are still not in the repository. You have to commit them.

git commit -m “The commit message for the changes"

If you want to undo the last commit in the local repository and move HEAD back by one commit, run the command

git reset --soft HEAD^

The “- -amend” option lets you modify the last commit by adding more changes and /or overwriting the last commit message.

git commit --amend -m "The modified commit message"

If you want to see all the commits you have done, run

git log

Push changes

After you commit the changes, they are in the HEAD of your local repository. To send those changes to a remote repository, execute

git push origin master

“master” can be any branch you need.

If you have not cloned an existing repository and want to connect your repository to a remote server, first you need to add the remote server

git remote add origin <server>

and then push the changes to the selected remote server.

Working with branches

GIT Branch

Branches are used to develop features isolated from each other. Upon the completion, the branches can be merged back to the master. The master branch is the default branch when a repository is created.

Let’s go through some useful commands when working with branches

Create a new branch and switch to that branch

git checkout -b <new_branch_name>

Switch to a branch

git checkout <branch_tobe_checkout>

Delete an existing branch

git branch -d <branch_tobe_deleted>

Local branches are not available in the remote repository until you push them.

git push origin <branch>

Update and merge the changes

Do you want to update your local repository with the content of a remote server? You only have to get a pull of that remote server.

git pull <remote_name> <branch_tobe_pull>

The command fetches and merges the remote changes with the local.

To merge a branch to the current directory, execute

git merge <other_branch>

Git tries to auto-merge the changes at both these cases. Sometimes there can be conflicts. Then you have to manually merge these changes by editing the files.

Before merging changes, you can preview the differences using the command,

git diff <source> <target>

Git Tags

Git has the ability to tag specific points in history as being important. You can use this functionality to mark release points.

git tag <tag_name> <commit_id>

This command creates a tag with the <tag_name>. The <commit_id> is the commit you want to refer with the tag.

You can view all the tags in a repository. Just run the command,

git tag

The above command will list out all the tags in the repository. If you need to check out a specific tag, run

git checkout <tag_you_want>

Let’s have a look at some advanced commands.

git squash

A series of commits can be squashed into a single commit. This is important because at the working directory you will add several commits with small changes but they are not necessarily important to be retained in the history.

Here’s how squashing is done

git rebase -i HEAD~3

In the text editor that comes up, replace the words “pick” with “squash” next to the commits you want to squash into the commit before it. Last 3 commits will be considered.

git cherry-pick

Cherry pick chooses a commit from one branch and apply it onto another.

Make sure you are on the branch you want to apply the commit to.

git cherry-pick <commit-hash>

If you cherry-pick from a public branch,

git cherry-pick -x <commit-hash>

git stash

Git stash can be applied if you want to save the current state of the working directory and index but want to have a clean working directory.

git stash

The command will save the local modifications and

git stash pop

will immediately drop the changes saved in the stack to the working directory.

Cheat Sheet

git init = create a local repository
git clone /path/to/remote/repository = Create a local copy
git remote add <remote_name> <remote_url> = add a remote repository
git remote -v = see all the remote repositories added
git add <file_tobe_added> = add one file to the staging
git add * = add all the changes to the staging
git status = explore the staging
git rm --cached <file_tobe_removed> = remove a file from the staging
git commit -m “Commit message” = commit changes
git reset --soft HEAD^ = undo the last commit
git commit --amend -m “Modified Commit message” = modify a commit
git checkout <filename> = replace local changes
git log = view all the commits
git push origin master = push changes to the remote server
git checkout -b <branch> =Create a new branch and checkout
git checkout <branch> = Switch to a branch
git branch -d <branch> = delete a branch
git pull <remote_name> <branch> = get changes of the remote
git merge <branch> = merge a branch to the current branch
git diff <source> <target> = preview the differences
git tag <tag_name> <commit_id> = create a tag from a commit-hash
git tag = view all the tags
git checkout <tag_you_want> = checkout a specific tag
git rebase -i HEAD~3 = squash the last 3 commits
git cherry-pick <commit-hash> = get commit from a branch to another
git cherry-pick -x <commit-hash> = cherry-pick from a public branch,
git stash = save the current modifications temporary
git stash pop = drop the temporarily saved changes

I hope this article provides you few basics of GIT which can help in daily version controlling.

Please leave your valuable comments and suggestions.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Pamoda Wimalasiri
Pamoda Wimalasiri

Written by Pamoda Wimalasiri

Associate Technical Lead @ WSO2 focused on the IAM domain

Responses (1)

Write a response