top of page
Search

How basic git command can keep you out of trouble

Updated: Apr 17, 2020

This article focuses on people who are going to join a company soon or students working on different projects and want to manage and sync there work with there group members, and don't have a clear idea about how to work in github, as a developer you should be a team player, alone you cant make a full fledged system with lots of features.

Github helps you with all this work.

Hello guys zeeshan here, So, here i am saving the day with the most important and crucial tool to be known by every developer in the market and one of the most basic and helpful for any developer out there, So in this lock down you can learn and make a most of your time rather than posting some weird games on social sites 😁

In this article we are going to learn the following:

1. What is Github?

2. Why use Github?

3. How Github works?

4. How to install github in windows and linux.

4. Github basic Commands.

1. What is Github?

Github is a code hosting platform for collaboration and version control. Github helps you and your group to work together. Git is a version control system, what it means is, when a developer create an app for example, they make constant changes to the code, releasing new versions after the first official. version control system keep this revisions straight, storing the modification in a central repository.

so, if git is a version control system better than any other alternatives. What makes it so special? Git is a command line tool, but the center around which git revolve is the hub github.com where developers store there projects and network with similar minded peoples.

signin on github account at https://github.com/

2. Why use Github?

For me github is faster than any other collaboration software github is just not a version control software it is a community/network of developers sharing there work with each other on there repositories and we can work on them as most of them are open source. you can work on them in your local machines and commit them whenever you suits best on remote repositories. you can add issues which can be solved by any one connected on github, lot of the great open source applications are contributed on github and there will be more. that is why i prefer using github for keeping the record of my projects.

3. How Github works?

What are the essentials that make what github is?

1. Repositories

2. Branches

3. commits

4. git (version control software github is built on)


1. Repositories

A repositories usually abbreviated as "repo" is a location where all the files for a particular projects are stored. Each project has its own repo and you can access it with a unique URL.


2. Branches

A github branch is used to work on different version of the repository at the same time. by default a repository has a master branch. and any other branch is a copy of master branch. We create branches to work on bug fixes and feature work separate from master branch and when the changes are made they are merged to the master branch.


3. Commits

At github changes made are called as commit. each commit has a message why it has been done and each commit has a unique id


4. How to install github in windows and linux.

so, lets install the github in your system.


1. For installing git in ubuntu

# apt-get install git

2. For installing git in windows

goto > https://git-scm.com/downloads and that is it.


now, ill be demonstrating the the basic commands of git on windows git bash but the commands will be similar for the linux user as well.

so, what with the git? using git command line you can initialize local git repositories work on them and commit those work on the remote repository at github and many more. you can clone the remote repository on local machine and commit the work done.


lets start with the working on git bash, here ill be demonstrating the basic commands of git and there will be another article for the advance commands of git. but for now lets learn the basics of git.

4. Github basic Commands.


  • git init

This command will make your folder in a local machine an empty git repository.

syntax:

git init

So, before initialize the empty git repo you need to make one folder and in that folder you will use git init command

Here, we have made a directory "mzeeej-repo" which we have initialized and made a empty git repo.


  • git add

This command will add file in the staging area, before a file is available to commit to a repository. The file is needed to be added to the git index (Staging area). because git not always track each and every modified file, so whenever we commit any file git search for those file in the staging area, therefore before committing the file we need to add those file in the staging area using "git add".

syntax:

git add <file name> or git add . (for adding the whole directory)

so, lets add a file in a empty git repo "mzeeej-repo" ad add it to staging area using "git add".

here, we have added two files in the empty git repo "helloworld.py" and "test1.py" after that i have added a file in two ways 1. by using the file name that is "git add helloworld.py" and added every file in the repo using "git add ." This will add the files in the staging area and after we commit the file the git will search for update in the staging area.


  • git commit

This command will commit the files stored in the staging area with a unique id and a message. lets see that.

syntax:

git commit -m "message"

lets commit the file that we have added before.

here you can see that 2 files have been added with a unique id.


  • git status

This command will return the current working branch and the files that have been added to staging area but not committed it will show that. If there is nothing to commit it will show there is nothing to commit

syntax:

git status

now, we will add one more file "test2.py" to the repo and add it the staging area but will not commit it and lets see what will "git status" returns.

You can see that git status has return the current branch that is "master" and showed the changes that needed to committed that is "test2.py" to be committed so after committing we can see that it is showing the branch name and there is "nothing to commit".


  • git config

Now, with git there are many settings and configurations possible, git config is basically how to assign this configurations, Two important setting are "user.name" and "user.email" this will tell that from where the commit is performed on a local computer.


There are two ways either make the user.name and user.email globally config or on the current repository settings.

syntax:


//globally
git config --global user.name "<username>"
git config --global user.email "<example@email.com>"

//current repo setting
git config user.name "<username>" 
git config user.email "<example@email.com>"

Now, lets add the username and user email.


  • git branch, merge, checkout

Why we need branch?

Branch is use to create another line of development and by default git has a master branch usually branch is created to work on a new feature, so if you are working on a web application you might wanna work on the cart feature. For that we will create a new feature branch and once the feature is completed it is then merge back to the master branch and we can delete the feature and every branch is referenced by head which points to the latest commit of the branch whenever you make a commit head will be updated with a latest commit.

syntax:


git branch //for checking the current branch you are in
git branch <branch name> //creating branch with branch name while staying in your current branch
git checkout <branch name> //for switching into this branch from current branch
git merge <branch name> //after the feature is committed you can merge the files with the master branch and delete this branch
git branch -d <branch name> /*for deleting your branch name if the branch has some uncommitted files it will give the warning to commit it first but instead of -d if we use -D it will directly delete the branch without any warning*/
git checkout -b <branch name> //this will create a new branch and switch you to that branch.

Now, lets perform this commands on our git terminal.

Above you can see that i have created a branch feature and updated file "test2.py" and perform commit operation and merge the feature branch with master branch and after all this i have deleted the feature branch because there is no need to keep it after merge. here i have used branch operation, merge operation to merge the changes in master and checkout operation to switch between the branches and also some sub operations like delete


  • git remote

This command will connect your local repo to your github account remote repository or anyone else public repository by there https link.

syntax:

git remote add origin <remote repo link> 

We will copy the link from our remote repo from my github account and paste it in the following command. this will connect our local repo to the github remote repo.


  • git clone

To create a local working copy of an existing remote repository "git clone" operation is used to clone and download remote repository to a local computer git clone is equivalent to "git init" when working with remote repository it will create a directory locally with all the files and history.

Syntax:

git clone <remote repo link from github>

you can see that all the file in the remote repo are now in my local machine. the remote repo is now has a clone in my local repo, i can make change and pull the changes on to my remote repo.


  • git pull

This command is used to pull the changes made on the remote repo to our local repository.

syntax:

git pull origin master

Lets add a file "test 1" with some text and commit the changes and pull the origin master on the local repository.

You can see after pull operation the changes have been made in the local directory that is test 1 is added into the local repo.


  • git push

This command is used to push the changes made in the local repository to the remote repository.

syntax:

git push origin master

lets make a file name "pushingfile.py" and add and commit it before using push operations. this will push the the changes made in local repository to the remote repository.

You can see that the changes have been updated in the github remote repository.

So that's it guys, I hope you can know can collaborate with your project group smoothly and faster, you understood how git commands are used for pulling pushing and branching. there are many more advance git commands used which ill be posting later so subscribe to our newsletter and stay updated. Thank you😁. i'll be posting more interesting technical tools guide bye!!


3 comments

Recent Posts

See All
Favorite Links
Recent posts
bottom of page