A Short Refresher On Git

Oct 13, 2017
>

Git is the most popular version control management tool/system today. Being distributed in nature, it facilitates lower risk of losing the whole repo out-of-the-blue. Because, user always has a copy of the repo. And there are a lot of vendors, offering hosted git server. And most the offerings are free with a reasonable limit.

I’ve been using Git for quite a long time. Though I’m not that much familiar with advanced concepts or complex scenarios regarding Git. To do day-to-day normal tasks, mostly I use only six commands(status, commit, branch, checkout, push, pull), with some flags some times. It’s true that I don’t do very complex things with it. But those four commands are enough for me. In this post, I’m iterating those commands one-by-one, possibly with some comments.

Okay, let’s start by creating a git repo with init command.

mkdir new_git_repo_folder
cd new_git_repo_folder
git init

It’s as simple as that. After running this command, you can see, one folder with name .git has been created. This folder is going to hold all the meta information for the repository. Git actually maintains hash for a particular commit. Those information also reside in this folder. Now create some content.

Most of the Git host servers shows the content of README.md as default face of a directory. It’s kind of index.html in a website. The extension .md stands for mark-down. It’s a pre-defined format to write documents, that can be converted into HTML/PDF or any other type, by some tool. There are a lot of implementation of this converter. Though there is a standard for the syntax for mark-down but it mostly depend on the converter in use. GitHub, GitLab, Bitbucket all have implemented their own version of mark-down to HTML converter. Though their most of the syntaxes are same, there are some extras also. But for now, we don’t have to bother with the mark-down syntax compatibility of various providers. We are gonna just add one README.md file.

echo "# New Git Repo" > README.md

The leading # will tell the converter to make the title as h1 element in HTML. Git has two very important concepts. HEAD and stage.

git status
git add README.md
git status

Add the user info to the local repo.

git config user.name "Arnab Das"
git config user.email "[email protected]"

Add user info for the work environment.

git config --global user.name "Arnab Das"
git config --global user.email "[email protected]"

Now commit.

git commit -m "Adding README.md"
# or, to add author in commit itself
git commit --author "Arnab Das <[email protected]>" -m "Adding README.md"
git status

Add the remote. It’s possible to mention the name of the remote other than origin. It’s just the most common one.

git add remote origin <origin-url>
# for the first time it's required to mention the remote name and branch name
# the -u flag persists the mapping of the local to remote branch
# later just "git push"
git push -u origin master

Create new branch and checkout to the branch.

git checkout -b new_branch
# do some changes
git push -u origin new_branch

Clone a git repo.

git clone <git-repo-url> [optional-folder-name]
cd new_git_repo

Checkout to a branch. Both the following commands does the same thing. Creates a local branch named new_branch from the remote branch new_branch. Only difference is that, it’s possible to mention a different branch name in the first command.

git checkout -b new_branch origin/new_branch
# or
git checkout -t origin/new_branch

See the branch names.

# lists only the local branches
git branch
# lists local along with remote branches
git branch -a
# search for a branch name
git branch --all | grep <string-to-search-for-in-branch-names>

Delete a branch locally.

git branch -d <branch-name>
# if branch has some new commits delete forcefully
git branch -D <branch-name>

Delete the branch in remote.

git push <remote-name> :<branch-to-delete>
Blog comments powered by Disqus