Introduction
“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 is easy to learn and has a tiny footprint with lightning fast performance. It outclasses SCM tools like Subversion, CVS, Perforce, and ClearCase with features like cheap local branching, convenientstaging areas, and multiple workflows.” http://git-scm.com/
If you don’t already know what Git is, take a crash course. Git (Wikipedia)
Installing Git
Download Git
There’re many Git clients available for windows, like Git, msysgit, QGit, GitCola, TortoiseGit, SmartGit (FREE and Commercial) and others. not to mention existing eclipse, visual studio extensions. You may choose what’s suits you best.
I’ll demonstrate using the GitHub for Windows GUI. Download it Here.
Install Git
Those instructions are for version 1.8.0-preview20121022. your installation may be different.
Installation is fairly simple, Just press <NEXT> 6 times to the end (don’t forget to READ the GNU General Public License!). the default options are ok for any starter.
If you know what you are doing, read each step carefully, if you need to add the Git or Unix tools from the windows command line, you should enable it during the installation. i recommend Git Bash only, Windows command Prompt (second option) ok too. should be safe, I find it rather useless. the third option is only for experts. do it on your own risk!
By default the Git Setup uses OpenSSH bundled. If you are using Putty, you can enable the Tortoise PLink at the setup wizard.
I recommend leave the default line ending windows style.
Using Git
Start a repository (git init)
If the Repository meaning is unknown to you, read this.
For Start your will create your own local repository.
You need an empty folder to create your repository in. create a <New Folder> folder on your desktop, drive d, or anywhere you want, as long as you remember where.
Now, Like almost anything in git, start the new repository (git init) can be done in several ways:
Git Context Menus
Maybe, the simpleist way, is the context menus. select or create a new empty (or not) folder anywhere, right click it and then select <Git Init Here> (It may be under X64 menu in x64 systems).
Git Gui
Start your Git Gui > Create New Repository > Browse (Select your empty folder) > Create.
Git Bash
It may not suited for anybody, but: right click a folder > git Bash
git init
or start Git Bash from your start menu and then use cd to find your folder, for example:
cd /D/Projects/c++/New Folder
git init
Command Prompt
If you have installed the command prompt you can use windows command prompt to locate the folder and then <git init> the same you whould do in git bash.
Result
Any way you’ve choosed, will create an hidden <.git> folder inside your folder which saves all the data for your repository. you can delete the .git folder to delete your local repository and disband anything you made using git and your folder returned to be an normal empty folder.
Repository Status
You can check your repository status:
Git Gui
if you are just created your new repository, you are in the status screen. Just press to Rescan button to update.
You can load Git Gui anytime > Open Existing Repository > Browse for your folder.
Context Menus
another easy option is to right click the folder on your file browser and then select the <Git Gui> context menu and read the above.
Git Bash
you can now use the
git status
from Git Bash or command prompt anytime to view your local git status (you need to be inside the project folder for this command to work)
Add Files To Repository
Your repository saves the folder versions as commits. before you can commit each commit, you need to add the files you want to be in this commit. if your folder is still empty, you can create now a file or two. just for practice.
maybe it’s a good time to create your README.MD file.
Identify yourself
The first thing to do before you can commit is to identify yourself so Git will know what name to sign on your commits. You can change it anytime but commits you’ve made with this sign will stay. if you won’t configure it, you will get an error later when you’ll try to commit.
Identify yourself can be done only by using the Git Bash or command prompt:
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
Omit –global to set the identify only in this repository.
Add files to repo and commit
Context Menus
The context menus have only one option when you right click the folder or anyfile inside it – <Git add all file now>. It will check all the files inside the folder as belong to the local repository and will add(flag) them as to be saved in the next commit.
This feature won’t suit in most cases as it set all files and won’t let you leave files as not belong to the repo – which is commonly done.
Git Gui
Took me time to find this, click on the icons of the files in the Unstaged Changes area to add them to current commit. you can also click the Stage Changed button to add them all.
click on the file icon again on the Stage Changed area to remove the file from this commit.
After you’ve selected the files (and directories) to be in the commit, Insert an Initial Commit Message and Press the Commit button to commit :).
Git Bash
Beside the social benefits of git, local repository helps you track versions of your files. compare them, and restore older version if needed. when git init command creating the .git folder in the first time, it doesn’t track no file. to add files for track you need to use the
git add <filename>
for example you can create an empty (or not) file called README.MD and type:
git add README.MD
you can use wildcard (such as git add *.conf). this way you can add files to the current commit, and when you ready type:
git commit -a
you need to provide a commit description. using vim by default (can be changed), and you updated your repository with you first commit.
Result
Inside your hidden <.git> folder, git has saved a copy of all your (selected) files inside this repository. this called a commit. now you can continue edit your files, knowing you can always go back to this point again.
Clone a repository
If you want to get a copy of an existing Git repository — for example, your github hosted project or other project you’d like to clone local or contribute to — the command you need is
. the following example will cover this topic clearly.
REMEMBER: to change drive inside git-bash use the CD /D/FOLDER syntax.
git clone https://github.com/WordPress/WordPress
this line will create a folder inside wherever folder you are located in called WordPress and clone (copy) to it all the files from wordpress repository. you will see inside a .git folder also, which mean you can continue now with your local repository, and even update the source if you have the permission to do so.
to update your local version with the server later:
git pull
you can also use the Git Gui to clone existing repository, just supply the source location and the target directory.
Use GitHub
After everything installed and configured, I’ll demonstrate using the above functions to connect to GitHub repository. I’ll demonstrate only using Git-Bash, but feel free to use the other methods as you like.
Create repository
First step is to create your repository at Gitghub. if you haven’t yet read the following:
Create A Repo – goto New Repository page in github and create your new repo. this tutorial is for public & free repo.
you can set a README.MD file to be clone ready immediatly. the following is for a new repo without README.MD:
after you created a new repository you need to git it to a folder.
Connect to Github
Git Bash
follow inside new directory:
- git init your empty directory.
- create a README.MD file or any other file.
touch README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/<your-user-name>/<project-name>.git
git push -u origin master
After the last command (git push) you will be requested to insert your Github username and password. everytime.
To disable username & password request each time, you need to use ssh authentication, which is behind the scope of this article.
Existing Source
if you already have a project your want to create the repo from just go inside your folder, git init it like above, git add neccesary files (you can use wildcard!) and then resyne the last three line above.
Existing Repo
if you have a local repo which you want to link to your new online repo just use the last two lines of the bash code above.
* more info: Create A Repo tutorial on github
Push/Pull from/to repository
to push your updates to the server:
git push
to update your local version with the server is the same:
git pull
Reset Changes from last commit
to reset all the changes you made from the last commit just use:
git reset --hard
Check Difference from last commit
to see the changes made in your git from the last commit use:
git diff
‘Q’ for exit. 😉
Those are the basics of Git. you can do a lot more with version control and collaborate with others.
Happy Git-ing…