Git Basics
- GIT BASICS -
What is Git ?
Version Control
Git simply records changes to a file or a group of files over time.
You can revert files back to a previous state.
You can review all the changes that has ever been made to your file
Distributed Version Control
Git has a remote repository which exists in a server somewhere.
It has a local repository which is stored in every developers computer.
Content Tracker
Git can be used to store content.
It is mostly used to store source code.
Free & Open Source
You have the freedom to share and change the software however you like.
Create your repository
A Repository is a .git/
folder that will exist once created inside your project folder.
It is responsible for keeping track of every changes made to files in your project folder.
It is simply a history book of the files in your project folder.
Lets launch a git bash terminal and fire some code …
Step 1 : Go to your project`s folder
$ cd /c/user/your-project-folder
$ cd /home/user/your-project-folder
$ cd /Users/user/your-project-folder
Step 2 : Initialize Your Repository
$ git init
Clone A Repository
You can also clone ( get a full copy of ) an existing repository if you don`t want to create a new one.
Clone your OWN LOCAL REPO
$ git clone /path-to-your-own-local-repository
Example :
$ git clone /d/my-project
CLONE A REMOTE REPO
$ git clone remote-repository-url
Example :
$ git clone https://github.com/angular/angular.git
Save Changes
Once you have your git repository setup, you can start making changes in that repository and commit it to git.
Assuming that you have already setup a git repository …
GO TO YOUR PROJECT FOLDER
$ cd your-project-folder
CREATE A FILE WITH SOME CONTENT
$ echo “Hello git” >> helloGit.txt
ADD FILES WITH CHANGES TO THE STAGING AREA
$ git add file1 file2 file3
or
$ git add * (use * to add all changed files)
Example :
$ git add helloGit.txt
Describe your changes and Commit
$ git commit -m “added new file(s) to the repo.”
Undo Changes
You can also discard all your recent, uncommitted local changes made to a file if you want.
Doing that however is not always recommended.
Discard Changes Made To A File
$ git checkout — [file-name]
Example :
$ git checkout — helloGit.txt
Undo The Most Recent Commit
$ git reset HEAD~1
Roll Back To A Previous Particular COmmit
$ git reset –hard <commit-name>
Example :
$ git reset –hard f4212332
Push & Pull Changes
If you had cloned a repository from a remote source like GitHub, you can now contribute to it with the changes that your have made in your local repository.
If you had not cloned your repository and you still want to send your changes to a particular remote repository. You first need to establish a connection to it.
Alternately, you can also bring in new changes from a remote repository to your local repository.
Establish A Connection If Had Not Cloned
$ git remote add [give-a-name] [remote-repository-url]
Example :
$ git remote add origin https://github.com/angular/angular.git
You can confirm your remote connection with :
$ git remote -v
Send Your Changes
$ git push [local-branch-name] [remote-branch-name]
Example :
$ git push -u origin master
Fetch New Changes From REMOTE
$ git pull [local-branch-name] [remote-branch-name]
Example :
$ git pull origin master
Do Inspections
There are different ways to inspect changes and observe what is actually happening under the hood in your repository.
Things including logs, file history, current state of the project directory can all be seen using different git commands.
Git Status
displays the current state of the working directory and the staging area
$ git status
Git Log
displays the project history, filter and search for specific changes
$ git log
$ git log -n 3 (display last 3 commits)
$ git log –pretty=online
Branch
Branching is one of gits killer features unlike other version control tools.
Git has a default branch called the master branch.
Branches help you to experiment with new features or changes that you are currently unsure about without tampering with your original project.
You can simply merge your new features with your current project or discard them later on. Its all upto you.
Create a New Branch And Switch To It
$ git checkout -b branch-name
Example :
$ git checkout -b new-test-wheels
Switching Back To Master
$ git checkout master
Merge Your New Branch With Master
$ git merge new-test-wheels (Should be done from the master branch)
Delete the Feature Branch
$ git branch -d new-test-wheels
push The Feature Branch To REmoTE REPO
$ git push origin new-test-wheels