- GIT BASICS -
What is Git ?
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.
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.
Grab yourself a copy of git and get it installed on your system.
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
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
$ git clone /d/my-project
CLONE A REMOTE REPO
$ git clone remote-repository-url
$ git clone https://github.com/angular/angular.git
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
$ git add * (use * to add all changed files)
$ git add helloGit.txt
Describe your changes and Commit
$ git commit -m “added new file(s) to the repo.”
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]
$ 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>
$ 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]
$ 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]
$ git push -u origin master
Fetch New Changes From REMOTE
$ git pull [local-branch-name] [remote-branch-name]
$ git pull origin master
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.
displays the current state of the working directory and the staging area
$ git status
displays the project history, filter and search for specific changes
$ git log
$ git log -n 3 (display last 3 commits)
$ git log –pretty=online
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
$ 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