How to setup Git on Windows [Git Bash and GitHub tutorial for beginners] - Things you were too afraid to ask your colleagues

While every developer today is expected to have a basic knowledge of Git and various third party version control systems based on Git like GitHub and BitBucket, there are a large number of devs still using Team Foundation Server (TFS) and SVN.

I won't make a case as to why Git is the most superior of the lot, you know it is when Microsoft itself has now migrated the entire Windows source code onto Git internally.

I just received a new laptop at work so might as well make this getting started guide.

Setup

Firstly, lets get the installation and setup out of the way. This is fairly straightforward :

  • Download Git for Windows here and follow the prompts in the wizard (there are a few preferences that you can change like your text editor to Notepad++ and Unix/Windows style commits, but I usually leave them as default)
  • Creating a "repo" - dedicate a folder on your drive of choice to be your Git repository say D:\MyWorkspace
Step 1 - Setting up your identifiers

This is basically used to identify who checks in the code and you need to set your user name and email by using the git config command

git config --global user.name "your name"
git config --global user.email "your email address"

Step 2 - Create a local Git repository

Navigate to your repo using the cd command and initialize your Git repo

git init

Setting up Git

Cloning a repo - you could also clone an existing repository available on GitHub either inside your own repo by:

git clone https://github.com/<username>/<reponame>

Or, cloning it into another local directory

git clone https://github.com/<username>/<reponame> <foldername>

Step 3 - Add your files/code

If you are using an IDE set your project work space as your Git repository to allow Git to begin tracking changes in your code. Most modern IDE's like Visual Studio and Eclipse allow for seamless Git integration.

In this example, we will use the touch command which will create a file in your repository or you can also create a sample text file manually and save it in your repository.

touch sampleFile.txt

Now that you have added a file, use the git status command to see what files are present in your repository that are uncommitted (untracked changes).

git status


Git Status
Step 4 - commit your code

Now that you have added your code to your repository its time to commit any untracked changes - a commit is basically what you call a check in in other version control systems.
You first need to add the files you want to commit using the git add command and then use git commit to commit any changes

git add <file>

git commit -m "Comments related to the commit"


Git Commit and Branch


Additional reading: git branches

Think of branches as different instances of your code, each having their own lifecycle. You could have a production branch, development and test branches for instance where only if all your functional test cases are passed only then will you be able to push your code to production.

git branch - shows which branch you're currently on

git checkout -b <branchname> - allows you to create a new branch from the point in your current branch

Connecting to GitHub

Firstly, signup/login to GitHub and create a new repository

Add a repository on GitHub

PUSH your local git repo into Github by setting the remote origin and using the git push command

git remote add origin <url of your GitHub repository>

git push -u origin master

PUSH

Similarly, to push a branch you would use:

git push origin <branchname>

As expected, your code should now reflect on your GitHub repository


Your code is visible in your repository

Additional reading: PULL requests

Finally, to get back changes onto your local repository we use the below command

git pull origin master


And, there you have it - Git is fairly simple if you understand the basics, this guide is in no way a comprehensive tutorial but it will surely help anyone who is using Git for the first time.


Comments

Popular posts from this blog

How to use the Viz in Tooltip feature in Tableau