Friday 17 October 2014

Install and use Git on Windows

Background

There are multiple code versioning tools. Some of them are SVN, Git, Mercurial, Perforce etc. In this post we will see how can we install git on windows and use it to create a local repository of a existing repository on github.

For installing git on Linux from source you can refer to one of my earlier posts - 


Installation

  1. Download the installer from the official git website.
  2. Run the installer. 
  3. If you are a beginner leave every setting in the installation workflow to default except probably the screen that says add git to the classath (This would alter your $PATH env variable).
  4. Finally click on finish to complete the intallation.

Screenshots for installation












Quick way to know if git is installed in your system is to open your command prompt and type git or git --version


If you see above output you have successfully installed git on your System.


Cloning a Git repository from Github

I have a repository on github that I had created fir creating a simple tic-tac-toe android application some time back. I am going to clone the same one. You can view the project at https://github.com/aniket91/TicTacToe

To clone a repository you have to use git clone repoUrl command. 
  •  C:\Users\athakur\GitSources>git clone https://github.com/aniket91/TicTacToe.git


Pushing changed to Github repository

Prior to pushing your changes you need to be aware of the changes that you have made. For that you can use git status command.

  • git status

Before pushing you need to  commit your changes. Use
  • git commit -a OR
  • git commit -m "git commit message"
to confirm your change. You will be asked to provide commit message. An empty commit message will abort the commit.



If you want to redo your commit then you need to execute - 

  • git reset --soft HEAD~1  
This will reset all your commit.

To push your changes you need to execute git push command. 

  • git push



You can see your commit changes on github. Below is the screenshot for my demo changes in this post.



Confused with master, remotes, origin?

You can run the following command to know more about repos your git knows -
  • git branch -a


Here, master is a branch in the local repository. remotes/origin/master is a branch named master on the remote named origin. remotes/origin/HEAD is the default branch for the remote named origin. This lets you simply say origin instead of origin/master.

So master is the branch name where as origin in the remote repo name.

Avoid Merge Commits

Whenever you have unpushed commits on your local and you try to do a git pull after those commits it will create a merge commit .
To avoid it follow one of the below mentioned methods,

  1. run git pull --rebase 
  2. To avoid running it with the rebase flag and to make the above the default behavior when pulling changes, git config --global branch.autosetuprebase always 
  3. In SourceTree under tools->options/preferences under the Git tab select "Use rebase instead of merge by default for tracked branches"

My Git Repositories


Related Links

t> UA-39527780-1 back to top