Git commands and its usage – This article helps to learn Git commands quickly. Git is a distributed version control system, used to maintain and manage different versions of the code.

In Distributed Version Control Systems, we maintain a master repository, from this master repo, each team member clones the master repository into their local repository, make changes and commit to their cloned version and then push his changes back to the main shared repository.

Git Commands and it’s usage

In general we do hear #3 most repeated words when we start working on Git.
1. Remote Repository – Git master repository hosted on web.
2. Stage area/index – It is a holding area to prepare changes for a commit. We can add files to the next commit or even part of the files to the next commit.
3. Working directory – Special directory .git is maintained for the actual work, it contains tracked and untracked files.

To work on Git in windows, first we should download git software, here is the link

Open Git shell to execute git commands instead of UI.

First we have to create a Git account in GitHub, creating an account is free as Git is an open source for personal usage. Here is the link

  • Git commands (git init) Initialize Repository 

    • Create a directory(say Test) and initialize the project.
    • C:\Test> git init – This command initializes empty git repository by creating .git folder as in the below image.

Git init command

  • Git Commands – Git Add and Git Status

    • Add a file FirstCommit.txt to the working directory (C:\Test)
    • To track changes made to FirstCommit.txt file, add it to Staging area by using git add command.
    • When added a file in working directory, git shell looks like below image.
    • Here +1 in red color represents #1 new untracked file added to the working directory.
    • Git working directory

Git working directory

  • Git Status

    • Command to see the changes > git status
    • Below is the response of the status command.
    • Git status command to see what files were added

Git status command to see what files were added

As above, to track the changes we need to add it to stage area by running >  git add

Git add command

This command moves changes in existing file to stage area.

FirstCommit.txt was added to stage area and its under tracking, so color got changed to Green.

Run > git status command to see the files in stage area.

Git status after executed add command

Files under changes to be committed means they are in the Staging Area, and they are not in our repository yet.

To add all untracked files into stage area (to add specific file, we need to pass file name like above)
> git add .

  • Store Stage Changes:

    • To store our staged changes we run the commit command with a message describing what we’ve changed.
    • >git commit -m “comments…”
    • All our changes now stored.

Git commit

  • Git Log

    • To see what are the commit we have done.
    • > git log
  • Create repository in remote:

    • So far we added a new file and stored changes, to add all our changes to remote repository, we need to create a remote repo first.
    • Open git hub > login to account > repositories > provide name then create. (suppose created First repo)
    • > git remote add origin<git user name>/<repo name>.git
  • Push To Repo:

    • The push command tells Git where to put our commits when we’re ready, so let’s push our local changes to our origin repo (on GitHub).
    • > git push -u origin master
    • The name of our remote is origin and the default local branch name is master.
    • git -u command tells Git to remember the parameters, so that next time we can simply run git push and it will know what to do.

Git push

  • After pushed the changes to remote repo, your remote repo looks like below.

Git remote repo

  • Pull From Repo:

    • We’ve invited other people to our GitHub project who had pulled changes, made their own commits, and pushed them.
    • We can check for changes on our GitHub repository and pull down any new changes by running
    • >git pull origin master

Git pull command

  • Branch creation:

    • Developers creates separate branches for any task or bug and the branches will be merged to main branch once the task got completed
    • git branch bugfix
    • To work on branching concept, we have added #2 lines of code to FirstCommit.txt and pushed to remote.
    • Here is the file content.
      • First Commit from the user
      • Second commit as a bug fix
    • git bash command shows number of files modified in red color.
    • After commit changes, if we see the status, it shows “your branch is ahead of ‘origin/master’ by 1 commit” message and green color upside arrow mark.
    • Below is the screen that represents above mentioned notes.

Git commands

  • Switch to Branch:

    • Now if you type git branch you’ll see two local branches: a main branch named master and your new branch named bugfix.
    • You can switch to branches using the git checkout <branch> command. Try it now to switch to the bugfix branch:
    • > git checkout bugfix
Git branch creation then switch
  • You can see branch name changed to “bugfix” from “master”.
  • Commit the changes after removing the bug fix from the FirstCommit.txt file

Git status

  • Switch back to Master Branch
    • Use the Git Command > git checkout master
  • Merge to branch:
    • To merge our changes from the bugfix branch into the master branch, we need to be in master branch.
    • > git merge bugfix
    • In above screenshot, it shows 1 deletion and your branch is #1 commit ahead.
    • To make our branch and remote repo in sync, push everything what we have been working to your remote repository.
    • > git push

Git merge command

  • Remove branch:

    • Remove the bugfix as fixed it. It can be done as follows.
    • You can use git branch -d <branch name> to delete a branch. Go ahead and delete the bugfix branch now
    • > git branch -d bugfix

Git remove branch command

  • Clone repo :

    • To start work on existing git remote repository, fist we need to clone remote repo to working directory.
    • > git clone <repo address>
    • To get repo address: Open repository in Git, click on clone or download, click on icon it copy the repo link to clip board, you can copy link to use.

These are the most useful Git Commands which are used very frequently.

Edit git remote origin and store git credentials
Agile Process and Terminology