Introduction to Git command (Part -1)

hiddenntreasure
Big0one
Published in
8 min readJun 5, 2020

Git vs Github vs Version_control

Version control

Version control is a system of recording changes to a file or set of file over time so that we can call specific version of record later. For example : Creating a web page which contain images and texts. And these data is changing over time. Version control helps to keep tracking of these changes. So that admin can easily recall previous state/version whenever faces problem in latest version or can compare latest version with previous version.

Besides version control helps multiple developers or team members to work together on the same project. Keep every member update to the latest version/development of work. And also keep tracking of work of other team-mates over the time. Which ensure the collaboration of team members, flexibility of work and faster delivery of product. Software developer used version control for managing code and associate files.

Why Version Control?

Suppose developer working on jupyter notebook. And he keep changing his code. But he can’t look back his initial work because there are no record of changes. To overcome the situation he can use version control tools to keep track of his works.

Git

Git is a Distributed Version Control tool that used to manage development projects and keep truck of the code history.

Git installs locally on computer. Its a open source and command line tool.

Git has a feature branching which helps you to create new branch. Team members working on different branch for various development purpose of a project. Initial branch is called ‘master branch’.

New branches record their changes over the time using git. That’s how one who working in a branch keep track of his work. After final touch , new branch merge with master branch. And master branch should be fulfill user expectation.

So,master branch will represents the latest code “that work”,where other new branches for various “experiments” in progress. In Article part-2, will discuss details about branching and its deployment in working environment.

Download git from below link. And open ‘git bash’ to perform various version control task. We will learn commonly used command line later on this article.

Github

Github is a cloud server for code sharing and publishing project. Its a social network for programmer all around the world.

Git is the heart of version control and Github is the soul. Don’t get confuse between heart and soul. Even i was confused and thought git and github is almost same.But Git keep track of the changes and github save the changes on remote server. With version control system like git,one can clone/copy Github repository project in his local machine. And start developing the project locally,commit changes and push/update it to github server for saving latest version of work. Git is used for commit and push operation. Then the owner/leader of the project can examine the latest development of the project.

Github is web based graphical user interface while git is a command line tool. Github offers three features fork,pull request and merge. Github offers some git operation in gui form.

Sign up in Github using below link. Create repository for your project. You can also share a repository with your teammates for collaborative work on a project.

Commonly used Git command

open ‘git bash

  1. Git clone:

Git clone is the command for identical copy of a projects repository’s latest version from github repository to local computer. It downloads source code of a project.

git clone <https:// link of project repo github>
clone with HTTPS

Copy the URL of the repository and paste it after ‘git clone’ to download the project on existing current working director.

2. Git branch

Branching is important in software development process. Branch is like creating a tree like working environment. It helps team members work in parallel on a project from remote destination. ‘git branch’ command used to create ,delete or list branch.

git branch <branch_name>git push -u <remote> <branch-name>

First command will create a branch in local repo. Then it need to push to remote server. That’s why ‘git push’ .

Listing of branch :

git branch --list

This command will show existing branch of the repo.

Existing branch list

Delete a brach:

git branch -d <branch_name>

This will delete the specify branch from the tree.

3. Git checkout

We need to switch to the new branch for working on it and for that ‘git checkout’ command is important. But before that current branch must be commit for the record of changes and should exist in local machine.

git checkout <branch_name>

This will switch to the specified branch. But if we want to create new branch and switch at once then use below command

git checkout -b <baranch_name>

Now we can edit ,add and commit in our new branch and then push to remote server like github.

4. Git add

The git add is a command ,which add changes of the current directory to the staging area. With this you want to tell git that you want to update file or full local repository in the next commit.

git add <file_name> 

This will add specified file only.

git add -A

This will add new/update/renamed files of the current directory to staging area.

new/update/renamed

git status command showing directory changes in the terminal. We will learn about ‘git status’ later.

5. Git commit

git commit is used to save changes in local repository. It is a checkpoint in the development process where you can back whenever needed. It saves your changes in local repository only.

git commit -m ‘My Message about commit’

We leave a short message with it too.

6. Git push

This command is used after ‘git commit’ to upload local repository to a remote repository. It only upload commit changes like update a file or rename or new file. That’s mean those updates which are add and then commit as described above.

git push <remote> <branch_name>

If our branch is new then we used below command for push.

git push --set-upstream <remote> <branch_name>

7. Git status

Git status will give us all the information about the current branch.

git status

We can gather information like:

  • Whether the current branch is up to date
  • Whether there is anything to commit, push or pull
  • Whether there are files staged, unstaged or untracked
  • Whether there are files created, modified or deleted

Lets explore git status:

1

We are in ‘develop’ branch which is up to date and nothing to commit.we know how to create a new branch and switch to it.

2

Now we create a .txt file ‘abc.txt’ in the branch. git status saying that there is nothing to commit but untracked/unstaged file named abc.txt . As the file isn’t add yet it is taged as untracked file.

3

We add the file using ‘git add’. This time git status is saying that our branch has a new file : ‘abc.txt’ which is staged now.

4

After commit git status is saying that our branch is ahead of the remote branch(develop) by 1 commit. (Push so that change take place in remote server.but we will do it later.)

5

We modified abc.txt . And run git status command which indicate that the file is modifed. ‘modified’ is red cause the file isn’t add yet to staging area and bottom line says that ‘no changes added to commit’

6

After adding the file ‘modified’ message turned into green which means it is staged. Also, the message about nothing to commit has disappeared.This means that we have our file inside the staging area, it is being tracked and there are changes to be committed.Commit the file to commit the changes.

7

Delete abc.txt from directory. And delete message is showed in red color.

At the end if we push now, then empty abc.txt file will upload to github repository. Because we commit the file when it was empty.

8. Git pull

The git pull command is used to update our local version of repository from a remote repository. By default it perform two task one is ‘git fetch’ which fetch repository from remote server. Another is ‘git merge’ which applies the changes to local repository. It can create conflicts.

git pull

9. Git merge

The git merge is used when everything in the development branch worked fine. Then you want to merge the branch to its parent/master branch. ‘Git merge’ integrates development branch to its parents branch with all of its commit over the time.

First switch to parent branch

git checkout <parent_branch_name>

Then update local parent branch to avoid conflicts. Our latest version of parent branch is stored in remote server(github) and ‘git fetch’ updates parent branch from their.

git fetch

Finally merge with our development branch.

git merge <development branch>

Q1: Why git is called Distributed Version Control ?

Because your clone/copy can be someone else’s master, and you can push your clone towards any other repository. There is no need for a concept of one “true” master; you can run it as a distributed grid of repositories.

Conclusion

This is the first part of git series. Here i tried to explain version control,git and github ‍and their differences. And then described commonly used git command in git community. This article designed for those who are new to git. That’s why i repeated many things again and again. I collect information from different sites and used my personal experience. So,please response if you find any mistake. Thank you.

--

--