Tuesday, November 18, 2014

Adding Git Version Control to Your Project

Related Pages: http://kahthong.com/2012/05/how-use-google-drive-or-dropbox-host-your-private-git-repositories

Install Git

This section teaches you how to install Git 1.8.3. See feeding.cloud.geek.nz for more information. I googled "Download Git For Windows", selected Download Git for Windows - Git - Downloading Package (Git SCM).
  1. Download the installer to your Desktop.
  2. Select "Run Git from the Windows Command Prompt".
  3. Select "Checkout Windows-style, commit Unix-style line endings".
Now, we will create the central repository and start version control for a project.
  1. Open Git Bash.
  2. Run the following commands to configure Git:
    git config --global user.name "Scott Izu"
    git config --global user.email "scottizu@gmail.com"
    • The "git config" commands setup some common parameters.
    • Replace "Scott Izu" with your name and "scottizu@gmail.com" with your e-mail address.
    • These configurations are needed before you can commit changes and are used to identify who made a given commit.
  3. Run the following commands to create the central repository:
    mkdir /c/Users/Scott/GitRepos/myrepo.git 
    cd /c/Users/Scott/GitRepos/myrepo.git 
    git --bare init --shared
    • The "mkdir" command created a folder (ie "C:\Users\Scott\GitRepos\myrepo.git"). You will need to replace "/c/Users/Scott/GitRepos/myrepo.git" with your central repository folder.
    • The "git init" command turned the folder into a central repository.
    • The central repository is typically setup on a server.
  4. Run the following commands to create a local repository:
    mkdir /c/Users/Ben/workspace/myrepo
    cd /c/Users/Ben/workspace/myrepo
    git init
    git remote add origin /c/Users/Scott/GitRepos/myrepo.git
    git config branch.master.remote origin
    git config branch.master.merge refs/heads/master
    • The "mkdir" command created a folder (ie "C:\Users\Ben\workspace\myrepo"). You will need to replace "/c/Users/Ben/workspace/myrepo" with your local repository folder. Imagine Ben is one developer working on this project.
    • The "git init" command turned the folder into a local repository.
    • The "git remote" command sets the myrepo.git folder as the central repository for this local repository.
    • Origin will be used as a shortcut to reference the central repository.
    • Both the local repository and central repository have no commits yet (Git tracks commits).
  5. You can create a Git Gui Visualization for the local repository:
    • Open the Git Gui.
    • Select "Open Existing Repository".
    • Browse to the folder just created (ie "C:\Users\Ben\workspace\myrepo").
    • Open->Repository->Visualize All Branch History
    • After your first commit, you can reload this visualization by choosing File->Reload from the Git Gui (gitk).
  6. Add some files to the myrepo folder (ie Copy the "C:\Users\Scott\workspace\my-app" Java project folder from the previous post).
  7. Run the following commands to create a commit in the local repository and then push the commit to the central repository:
    cd /c/Users/Ben/workspace/myrepo
    git add -A
    git commit -m "Created the my-app project"
    ... Refresh Git Gui ...
    git push origin master
    ... Refresh Git Gui ...
    Before commit: After commit, before push: After push:
    • The "git add" command stages all changed files in the directory for a commit.
    • The "git commit" command creates a commit. After executing this command, you can ... and see that you have a local (green) branch named master.
    • The "git push" command moves the commit from the local repository to the central repository.
    • This local repository is not connected to a central repository so you should only see the local (green) master branch.
    • After the "git commmit" command, you can see that the commit exists on the local master branch (green).
    • After the "git push" command, you can see that the commit exists on the central master branch (brown).
You may try making some additional changes, commits and pushing those commits to the central repository.  In the future, you will be able to rollback to previous versions of the code by using the commit number.

This post was reposted from http://scottizu.wordpress.com/2013/08/15/adding-git-version-control-to-your-project-part-i/, originally written on August 15th, 2013.

No comments:

Post a Comment