Getting Started with Git-Part1
What is GIT
Git is a free and open-source distributed version control system which lets one easily maintain and distribute multiple versions of a product with ease and high scalability.
What is a Distributed Version Control System:- This means that the changes made to your source code are stored on a central server as well as on their local PC and hence the changes can be synced Easily.
Why do we need GIT:- In today’s world developers work side by side in parallel on the same code from multiple countries. So a version control system like Git is needed to ensure there are no code conflicts between the developers. Moreover, several times their are pitfalls and bugs and new features which require a significant urgency for completion. Hence Git comes to play as it helps the developers to switch back and forth between multiple version of their code.
Installing Git
- Download and Install Git from https://git-scm.com/downloads
- Install Notepad++ or Atom or any other Text Editor.
Setting Up Git
Launch Git Bash or Terminal and Enter the below commands
git config --global user.name "yourname"
git config --global user.email "youremail"
Create a Local Repository
Create a New Folder, let's call it blog. This Folder can be referred to as a repository . We will discuss this in detail in later series of blogs
cd blog
git init
This will initialize our local Git Repository in blog folder
Staging Change
Whenever you make a change to any file you need to add your changes to staging area for the git to recognize it. You can do so by
git add <file name>
You could alternatively add all the files in one go using
git add --all
Committing Changes
Committing changes is the git’s way of telling to save your changes. When you commit a change you also need to provide a message which signifies what changes you made so it's easy for you to remember.
git commit -m "Your Commit Message"
Sometime you might want to Add your Changes as well as commit them then you can do that in this way
git commit -am "Your Commit Message"
Restoring the Changes
Sometimes you might want to revert a commit or changes to a file then you can do this
git reset <file> ORgit reset <commit>
The Remote Git Repository
Until now, we have been working only in the local repository. Each developer will work in their local repository but eventually, they will push the code into a remote repository. Once the code is in the remote repository, other developers can see and modify that code. So we will make a repository on GitHub. You can follow the below steps for the same
- Go to https://github.com/ and create an account.
- After registering in the GitHub homepage, click on Start a Project to create a new Git repository. Give the repository a name and click “Create Repository”
- Give the name as
blog
Then click on Create.
Now Copy <Your Repo Url> which in this case is https://github.com/RaghavAwasthi/blog.git
. We will use this in the next Step
Pushing your Changes
As mentioned earlier that git is a distributed version Control System you would need to upload your changes to the central server. In Git’s way, it means that you want to push a change. Your changes can be pushed to any online VCS Tool as per your choice like GitHub, Gitlab and so on. It can be done as follows.
git remote add origin <Your Repo URL>
git push
Replace <Your Repo Url> with the url copied on previous step.
Note:- You need to specify remote only once. Next time onwards you can directly use
git push
Pulling Changes
You will often need to pull the changes made by others (by pushing) . You might have already guessed the command to do the same.
git pull
The Above Diagram can be used to visualize the concepts more clearly
We will discuss the concepts of remote and branches in detail in the next blog post So stay Tuned.I will add links to other parts below.
BONUS
In order to quickly manage your repositories Install Hub a command-line tool to create GitHub repositories from the command line
To install Hub type this in any shell
choco install hub
To create the Repository on Github type this
hub create
git push
If You want to drop feedback feel free to ping me Linked In, Gitter