Getting started with Git
09 Dec 2014To get started with Git, download the latest version of Github for Windows.
Search for Git.exe
in your system and find its containing folder path. Add this Git path to Windows Environment variables.
Path=C:\Users\User\AppData\Local\GitHub\PortableGit_ed44d00daa128db527396557813e7b68709ed0e2\bin
###Add a Local Repository to Github with Command Line:
Create a new empty repository in Github. This is where the local repository is pushed.
In the Command Prompt, change the working directory to your local repository.
Initialize it as a Git Repository, with git init
.
Use git add .
to add all file contents to the staging area.
Run git status
to view the status of the files in the working directory and staging area. We can use git diff
to know which files have changed since the last commit.
Commit the staged files in the local directory.
Use git commit -m "commit message"
.
Copy the URL of Github Repository we created in the first step.
Run git remote add origin <Remote URL>
to set our remote repository.
git remote -v
to verify the remote URL.
Push the Local Repository changes to Github Remote directory and specify the branch. Anything in master
is always deployable.
Use git push origin master
At this point fill in the Github Account Credentials, and we have successfully added a local directory to remote on Github.
###Working with Branches:
Create a new branch and switch to the new branch using,
git checkout -b new-branch
.
Use git branch
to know which branch we are currently in, it is starred and displayed in green.
Push the new branch to remote repository,
git push origin new-branch
Give the Github account credentials to authenticate.
We can find that our new branch is pushed to remote succesfully.
Switch between the branches using,
git checkout branchname
.
To delete a branch, switch out of the branch you want to delete.
Delete a Local branch with, git branch -d branchname
.
We can delete a Remote branch using,
git push origin --delete branchname
as of Git v1.7.0
or use git push origin :branchname
as of Git v1.5.0
###Fetching a Remote Repository:
clone
is used to download a copy of a project to use the code, or to collaborate with someone on a project.
Run git clone <URL of the remote repository>
with the URL of the project we want to copy.
fetch
retreives any new work and updates made to the remote repository, by collaborators.
Use git fetch origin master
.
merge
combines our local changes and changes made by the collaborators. Use git merge origin master
.
pull
is a shortcut for completing both git fetch
and merge
in a single command. It grabs the updates and merges them with local work.
Use git pull origin master
.
Git commands used above for quick reference
// Add a Local Repository to Github with Command Line:
> git --version
# To find git version
> git init
# To Initialize the local directory as a Git Repository
> git add .
# To add all file contents to the staging area
> git status
# To view the status of the files in the working directory and staging area
> git commit -m "commit message"
# To Commit the staged files in the local directory
> git remote add origin <Remote URL>
# Sets the new remote
> git remote -v
# Verifies the new remote URL
> git push origin master
# Push changes to remote repository
> git checkout -b new-branch
# Creates a new branch and switch to the new branch
> git push origin new-branch
# Push the new branch to remote repository
> git checkout branchname
# Switch between the branches
> git branch -d branchname
# To delete local branch
> git push origin --delete branchname
# To delete a Remote branch as of Git v1.7.0
> git push origin :branchname
# To delete a Remote branch as of Git v1.5.0
// Fetching a Remote Repository:
> git clone <URL of the remote repository>
# Clones a repository to your computer
git fetch origin master
# Fetches updates made to a remote repository
git merge origin master
# Merges updates made online with your local work
git pull origin master
# pull is a shortcut for completing both git fetch and merge in a single command. It grabs the updates and merges them with local work.