Adding Version
Control (Git)
Basic git commands
What is GitHub?
GitHub is a code hosting platform for version control
and collaboration. It lets you and others work together
on projects from anywhere.
Setting up your Development
Environment
Software Requirements
•Text editor of your choice
•Browser of your choice
•Command line shell
•Files required for the exercises
Setting up Git
• Downloading and Installing Git
• To install Git on your computer, go to https://git-scm.com/downloads
to download the Git installer for your specific computing platform.
• Then, follow the installation steps as you install Git using the installer.
• You can find more details about installing Git at
https://git-scm.com/book/en/v2/Getting-Started-Installing-Git. This
document lists several ways of installing Git on various platforms.
Global Configuration for Git
• Open a cmd window or terminal on your computer.
• Check to make sure that Git is installed and available on the
command line, by typing the following at the command prompt:
git - -version
• To configure your user name to be used by Git, type the following at
the prompt:
git config --global user.name “some name”
• To configure your email to be used by Git, type the following at the
prompt:
git config --global user.email email_id
• You can check your default Git global configuration, you can type the
following at the prompt:
git config --list
Basic Git Commands
• At a convenient location on your computer, create a folder
named git-test.
• Open this git-test folder in your favourite editor.
• Add a file named index.html to this folder, and add some HTML code
to this file.
• Initializing the folder as a Git repository
• Go to the git-test folder in your cmd window/terminal and type the following
at the prompt to initialize the folder as a Git repository:
git init
• Checking your Git repository status
• Type the following at the prompt to check your Git repository's status:
git status
• Adding files to the staging area
• To add files to the staging area of your Git repository, type:
git add .
• Committing to the Git repository
• To commit the current staging area to your Git repository, type:
git commit -m “Message”
• Checking the log of Git commits
• To check the log of the commits to your Git repository, type
git log --oneline
• Make any change in the html file
• Add a sub-folder named templates to your git-test folder, and then add a file
named temp.html to the templates folder. Then set the contents of this file to be
the same as the index.html file above.
• Then check the status and add all the files to the staging area.
• Then do the second commit to your repository
• Now, again modify the index.html:
• Now add the modified index.html file to the staging area and then do a third
commit.
• Checking out a file from an earlier commit
• To check out the index.html from the second commit, find the number of the second
commit using the git log, and then type the following at the prompt:
git checkout <commit id> “file name”
• Resetting the Git repository
• To discard the effect of the previous operation and restore index.html to its state at
the end of the third commit, type:
git reset “index.html”
• Then type the following at the prompt:
git checkout --index.html
• You can also use git reset to reset the staging area to the last commit without
disturbing the working directory.
Online Git Repositories
• Sign up for an account either at Bitbucket (
https://bitbucket.org) or GitHub (https://github.com). Note
that private repositories on GitHub requires a paid account,
and is not available for free accounts.
• Then set up an online Git repository named git-test. Note
the URL of your online Git repository.
Set the local Git repository to
set its remote origin
• Set the local Git repository to set its remote origin
• At the prompt, type the following to set up your local repository to link to
your online Git repository:
git remote add origin <repository url>
• Pushing your commits to the online repository
• At the prompt, type the following to push the commits to the online
repository:
git push –u origin master
• Cloning an online repository
• To clone an online repository to your computer, type the following at the
prompt:
git clone <repository url>
Additional Resources (Git)
• Git site http://git-scm.com.
• Installing Git chapter from Pro Git
• Git reference manual
• Quick reference guides: GitHub Cheat Sheet (PDF) | Visual Git Cheat
Sheet (SVG | PNG)
• Atlassian comprehensive Git tutorial