Skip to content

Basic git bash command tutorial to get started with version control system i.e. initializing repository , commiting changes, pull & push changes, working with branches etc.

License

Notifications You must be signed in to change notification settings

symonhasan/all-about-git

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 

Repository files navigation

All About Git

Table Of Content

Getting Started

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.

Git Installation

Git can be simply installed from Git-Scm for any operating system as it is available for Windows , Mac & Linux. Installation process is very simple.

Git Version Check

After sucessfully installing Git to your machine you can now check version by typing the following command in your Terminal or Git Bash

$   git --version

You may now see your installed Git version. git version check

Fig - git --version

Git Global Config

Once you have sucessfully installed git now you need to set up some global configuration variable. Now these variables are important because if you are working with other developers then they need to know who is making which changes and many other things. Now there are two important variable. One is user.name and another one is user.email. Of course there are lot more than these two but as we are getting started as beginner these two are enough for us.

  • To set your username
$   git config --global user.name "<your name>" 
  • To set your email
$   git config --global user.email "<your email>" 
  • To list all your configuration
$   git config --list

Snapshots are given below

git global config

Fig - setting config variable

git global config list

Fig - git config --list

Need Help?

If you need help regading any keyword you can try any of these two command

$   git help <keyword>

or

$   git <keyword> --help

Snapshots git fetching help git config help command

Fig - git config --help

Setting Up Local Repository

Initialize

Let's say we have a project or we have a directory for project which we want to track with git in our local machine. To do so first we need to go to that project directory. In my case my project folder is currently empty as you can see in Fig .

empty project folder

Fig - empty project folder

Now to track this folder with git in our git bash we need to type the following command

$   git init

This command will create a .git folder in our current working directory which stores all information about our project.

initialize git

Fig - initializing git

To stop tracking our project with git we need to remove the .git folder from project directory. Our project will no longer be tracked with git. This will also remove all branches and commit we have made in our project if the project do not exist in remote repository.

remove .git folder

Fig - removing .git folder

[ Optional ] If we need to initialize our local repository with README or gitignore file we can do this with following command.

$   touch README.md
$   touch .gitignore

add README add gitignore

Fig - adding README and gitignore file to project folder

Checking Git Status

Before dive deeper into things there are basically three area while tracking a project with git. The first area is our working directory where we make changes to our files , projects etc. The second area is called staging area where we organize our files which we want to commit. And the last area is .git directory or repository where all the comiited changes are saved.

Now current status of our local repository can be checked using the following command

$   git status

Now in our project if we now run this command we will see this

git status

Fig - current status of local repo

Add files to staging area

Now this red color file are untracked file or the file we have made changes in our working directory. To add this file to staging area we need to type the following command

$   git add -A

This command will add all the current untracked files in staging area. In Fig the files colored in green are currently in staging area and are ready to be commited.

git add all

Fig - add all files to satge area

To remove all files from staging area we can simply type

$   git reset

This will remove all file from from staging area. git reset all

Fig - remove all files from stage area

To add a sepcific file to staging area we can type

$   git add <filename>

git add specific file to stage area

Fig - add specific file to stage area

To remove a specific file from staging area

$   git reset <filename>

git reset filename

Fig - git reset specific file

Now for our project let's add all file to staging area.

all files to stage area

Fig - all files to stage area

Commit Changes

To commit changes that we have made in our project first we have to add all our files that we want to commit in staging area with git add command. We can see the new changes we have made in our files using

$   git diff

After adding files in staging area now we can make changes to our repository by typing the following command in our git bash.

$   git commit -m "your massage"

git commit

Fig - commit changes

We can also see all our commits using the following command. It will list all our commits we made in our repository till now.

$   git log

git log git log

Fig - git log

Transfer Local Repository To Remote Repository

  • Browse Github to create a new repository.

  • Give your repository a name. Do not initialize the new repository with README, license, or gitignore files. You can add these files after your project has been pushed to GitHub if not exist. In our case we already created both README and gitignore file.

  • After creating new repository you may see a page with a remote repository url. In my case the url is shown in Fig . Copy this url

remote url of new repository

Fig - remote url of new repository

  • Open Git Bash

  • Change the current working directory to your project directory or local repository

  • Now run this following command to your git bash.

$   git remote add origin < remote repository URL >
$   git remote -v
$   git push origin master

transfer local repo to remote

Fig - adding local repo to remote repo

  • Refresh your github repository page and you will see all your files and commits you made in your local repository is now available in your remote repository.

github page refresh

Fig - github page after adding local repo

Clone Remote Repository

To clone a remote repository first you need to go to the directory where you may want to clone your repository. After that just type the following command in your git bash.

$   git clone <url of your remote repository>

To list the information about the repository you have cloned you can try

$   git remote -v

To list out all branches your cloned repository have you can try

$   git branch -a

Push Changes

To push changes we made in our local repository to remote repository we need to ,

  • Add your changed files to staging area
  • Commit our changes

git commit change

Fig - commit change

  • Pull the changes other developers have made so far. Now this is important when two or more developer is working on a project so that before making any changes to remote repository from your side you are updated with what changes other developers have made till now. Pulling the changes can be done with the follwing command
$   git pull origin master

git pull changes

Fig - pull changes to master branch

  • Now push your changes using following command. Pushing is important so that other developer may fetch what changes are made from your side.
$   git push origin master

git push change

Fig - push changes in master branch

Here origin is your repository and master is the branch where you are pushing the changes. Though it's not best practice to direcly push changes to master branch.

Work with new branch

As I mentioned in previous section that it's okay but not best practice to directly commit and push changes to master branch. A common workflow which most of the developer suggest is ,

  • Create a branch for your desire feature you want to implement.
  • And then work on that branch i.e. Commit , Push , Pull etc.
  • After sucessfully implement the features merge the branch with master branch.
  • Remove the branch.

Now this help to unit test individual feature of a project before merging them to master branch.

Create a new branch

To list all the branch in our local and remote repository

$   git branch -a

In our case our local and remote repository have only one master branch.

git branch -a git branch list

[ N.B. Here text in red color indicates the branch that are present in remote repository and others are branch present in local repository. ]

Now lets see current status of our remote repository in the figure below

git push change

As you can see currently we have only one remote branch which is master branch. And the repository contains one folder and two files. Now lets create a new branch for our further activities. To create a new branch we need to type the following command in our git bash.

$   git branch <branch name>

Now if we list our all branches using git branch -a we will see a new branch in our list.

git branch create git branch list

To head to a new branch we need to type

$   git checkout <branch name>

git checkout

Commit to new branch

Now let's create a new text file named test and commit it in our newly created branch.

git create file

Push to a Branch

To push our new branch to remote repository we need to type

$   git push -u origin <branch name>

[N.B. -u is important if the branch is not present in remote repository. If the branch is already present in remote repository we can simply use git push ]

git push new branch

Now if we see to our remote repository we will see our newly created branch have been pushed. But the file test.txt is not added as the branch is not yet marged with master branch.

remote repo

Marge a branch

To merge a branch we need to follow some steps ,

  • Checkout to master branch
$   git checkout master
  • Pull the changes other developers have made.
$   git pull origin master

git checkout master

  • View which branches are merged till now.
$   git branch --merged

git checkout master

  • Merge our branch with master
$   git merge <branch name>

git checkout master

  • Push the changes to master.
$   git push origin master

git checkout master

Now if we go to our remote repository we will see our changes from test branch have been added to our master branch as we marged it.

git checkout master

Delete a branch

Now as we have marged our test branch with master branch we can now delete our branch.

  • First check wheather the branch is marged with master or not using git branch --merged

git checkout master

  • Now delete the branch from local repository
$   git branch -d <branch name>
  • Delete the branch from remote repository also
$   git push origin --delete <branch name>

git checkout master

Now if we see our remote repository we will see our test branch have been deleted from our repository.

git checkout master

About

Basic git bash command tutorial to get started with version control system i.e. initializing repository , commiting changes, pull & push changes, working with branches etc.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published