Git is a free and open-source software that is used for version control and for source code management.
For a beginner, it might be a little difficult to grasp the concepts of Git. So this post is for the newbies out there.
The Setup :
First you have to download Git to your system. You can either go to https://git-scm.com/downloads/ and download, or you can use the terminal.
I am using ubuntu, so I used this command to install git:
sudo apt-get install git
Now let’s create a directory in out system where we will be working. Navigate to the location you want to store the project and create the directory.
mkdir myproject cd myproject
Now initialize git in your directory :
You can configure your name and email id with the following code :
git config --global user.name "yourname" git config --global user.email "youremailid"
If you already have a repository in github or if you want to clone someone else’s code, you can do it now :
git clone /path/to/the/repository
If you are cloning from a remote server use this:
git clone username@host:/path/to/the/repository
Now you can go ahead and start coding. And you can start pushing the code.
Check the status of your code. You can see all the changes you have made here.
Stage all the files to be pushed
git add .
or if you don’t want to stage every change, use
git add <filename>
Then you can go ahead and commit the changes.
git commit -m "Commit message"
Then push the code to the master branch.
git push origin master
You can pull the new changes from the remote repository to your local by pulling the code.
If multiple coders are working on the same repository, it’s advised to branch out from the master. You can branch out even if you want to work on something else without changing the existing code. Suppose you are going to work on a bug fix. You can create a branch for that by using :
git checkout -b branchname
Now you have made a copy of the master branch in the new branch. You can commit and push the code to this branch and it wont change the code in the master branch.
But remember to use this branch name while pushing the code
git push origin branchname
After working on the changes, you can checkout to the master branch and merge the code, if you want to.
git checkout master git merge branchname
And delete the branch if needed
git branch -d branchname
You can check which branch you are currently in by the command
You can view the log of your commits using
I think these are all the git commands that you need to know when you are starting out.
Good Luck. Happy coding.