Git Tutorial for Beginners…
Hello everyone, In this Git tutorial we will be learning about Git, how to create or initialize a repository in Git, how to clone a repository, how to commit code in your repository, how to create branches in your repository, how to push code to a remote repository and how to pull code from a remote repository.
This git tutorial is designed exclusively for people who want to start the topic from scratch. In this Git tutorial we will learn about how Git works. After taking this tutorial you will be up and running with Git. If you are willing to learn about how Git works, in detail, The problems that led to the creation of Git and How it solves those problems, you can check out this tutorial over here.
We will start with cloning repositories and initializing repositories. Then we will edit our code, and add the changes to the staging area which we will finally commit to our repository. We will be creating branches in our Git Repository. We will also learn how to use aliases in git. Following this we will end this Git tutorial with pushing code to a remote repository and pulling code from a remote repository. In this Git tutorial, all our remote repositories will be hoisted on Github, but in practice, it can be hoisted on Gitlab, Bitbucket or it can be hoisted on any remote server anywhere in the world.
If this sounds overwhelming at the moment, dont worry. In this Git tutorial we will be starting from the very basics and we will be explaining the meaning of these words in Layman terms besides we will also be working on Git and performing these operations.
Before moving forward ensure that you have the Git Installed on your Computer.
To check if git is installed in your computer, open up your command prompt or terminal and enter the command-
If Git is installed on your computer, the output will probably be a long list of Git commands (like shown in the figure), and if it is not installed on your computer, the output will be some sort of error.
But don’t worry, you can easily install Git on your computer
For Linux Users-
- Open your Terminal
- Run the command “sudo apt-get install git”
For Windows Users-
- Go to the following link- https://git-scm.com/downloads
- Download git for Windows from there
- Simply open the downloaded file and install it on your computer
For Mac Users-
- Open your terminal
- Run the command “brew install git”
Have a platform to hoist your remote repositories, it can be any platform but we will be using Github only and will recommend you to do the same.
Now with everything in place, we are ready to start our journey of learning git.
Before you can begin to use git, you need to provide your name and email to git so that it can identify you. Git will also use your identity when you commit changes to a repository, as it will show the changes made to the repository, name and email of the author who made those changes, the time at which those changes were made along with other useful information.
So it turns out that it is absolutely important to add your name and email to git.
For this, just run the following commands in your terminal-
git config –global user.name “Saurabh”
git config –global user.email “[email protected]“
The result is that git configures your name and email globally and this name and email will be used when whenever you commit changes to a repository.
Initialize Git Repository
Now we will initialize a git repository, but before that, what is a git repository? A git repository is simply a place/directory whose changes git will monitor. So, if you make any changes to a folder, subfolder, or file in a git repository, git will monitor that that changes.
So lets make a new folder and initialize a git repository inside it.
To initialize a git repository, open the directory or folder in which you want to initialize an empty git repository in your terminal and enter the command
git init .
This command initializes an empty git repository in the path you mention to it. And ‘.’ passed as path refers to the current directory, so git will initialize an empty repository in the folder which is currently opened in your terminal.
Congratulations!! You just initialized an empty git repository.
Cloning a Repository
To clone a repository means to make a copy of repository in your system from the original one.
To clone a repository into your system, open the directory in which you want to clone the repository in your terminal and enter the following command –
git clone <url of remote repository>
This will create a clone of that repository in your directory and will by default provide an origin(more about that in later sections) that points to the address of that remote repository.
Pat yourself!! You just cloned a repository into your computer from a remote server.
Git Status, Git Add and Git Commit
Now we will be adding files to our git repository, and let git track the changes we make to our repository.
Now, it may start to seem a bit overwhelming because of the amount of statements involved, but bear with us, it will start to make sense in a moment.
Lets add a file say file1.txt in this repository and add some text to it and save it.
The following command tells about the status of your repository
It tells if there is anything to commit, any changes to files/folders in the git repository will be reflected by his command.
In the image it is shown that git has tracked some new file with the name file1.txt.
Now, if we want to commit these changes to our git repository, we first need to add these changes to the staging area. In short, changes are first added to the staging area. And only the changes that are staged for commit are commited once we command the git to do so. For more detailed information about how changes are staged for commiting and how they are commited, you can check our tutorial about Git Commit here.
So now we will add the untracked files to the staging area using the command
git add <filename>
This command adds the file to the staging area, i.e it is now staged for commit and will be commited whenever the command to commit changes is given.
As you can see in the figure, when you check the status of repository by “git status” command, git has added file.txt to the staging are and it is now staged for commit.
You can add as many files to staging area as you want.
Now, time to get Commited(Sorry for the pun!)
Time to git commit
Simply enter the command-
git commit -m “<commit message>”
and this will instruct git to commit all the changes in the staging area.
The -m “<commit message>” part adds the commit message to the commit, which by the way is compulasary for every commit or your commit will be aborted. The result should be something as shown in the figure below, which shows that the commit has been made to the repository.
Congratulations!! You just made your first commit with git.
Git Log command allows you to see the changes you made to your repository with time.
To see the log of your repository, simply enter the following command-
This will print a log of the commits done so far on your repository, the time at which each commit was made, commit hash and information about the author who made the commit.
To get a shorter log, enter the following command
git log –oneline
Using Aliases in Git
Aliases are used in git to shorten a command that we use regularly. We assign an alternate command(custom made by us) to a standard command in git. It often leads to increase in efficiency and decrease in errors.
To alias a command in git, use the following command –
git config alias.name “<standard git command>”
where standard git command represents the command you wish to use alias for and name represents the name of the alias use wish to use inplace of the standard git command.
In the image above, we have created an alias for “log –oneline –graph –decorate –color” (which by the way is a very long command) as gr and as shown, they both produce the same result. In the other example, we have created an alias for command status as st and shown that it also produces the same result.
- Whenever creating an alias, never use ‘git’ inside the alias, i.e, inside the standard git command.
- Whenever the standard git command contains more than one word always enclose it within commas as we did in the first example.
Branching is one of the most useful features of git. It allows you to work simultaneously on a different “copy” of your code and try some new features. If those changes work out you can always merge them with the master(which is the main branch) branch. If however for some reason those changes are not required you still have your code safe in the master branch.
Creating a branch will “branch out” a copy of your code at that point so that you now have two parallel versions of your code. If you wish to learn more about Branching in Git, you can check out this tutorial over here.
To create a new branch from the current branch, Enter the following command-
git checkout -b <branchname>
This command creates a new branch with the branch name you provided to it as the last argument, and moves you to that branch.
To get the list of all branches in your repository, Enter the following command-
This command will list all the branches for you and highlight the one on which you are right now.
To move from one branch to another, Enter the following command-
git checkout <branchname>
This command will move from the current branch to the branch you provided it the name of as the last argument.
Here is what we did in the above example-
- The first command tells Git to create a new branch from the current(i.e, master branch in this case) branch with the name “newbranch1” and move to that branch. Any changes made to the repository from now on will be made ONLY ON this branch and none of the other branches. To make the changes of on branch appear in another branch, we need to merge the branches together. To learn about merging branches in Git, check out this tutorial over here.
- After executing the second command, Git displays us all the branches we have in this repository and highlights the branch that we are currently on.
- The third command tells git to move to the branch we mentioned to it(i.e, the master branch). After executing this command we have moved from the branch “newbranch1” to the master branch.
Pushing Code to a Remote Repository
Pushing code simply means that you are adding the changes you made to your code(i.e only changes which were commited) on local repository are being pushed(or being updated, in layman termns) to a remote repository.
In this tutorial we will be pushing our code from local repository to our repository on Github. Just incase you don’t know how to create a repository on Github, you can check it out here.
Now, before you push your code to a remote repository on a server, you need to add the address to that remote repository in your git. This can be done simply by the following command –
git remote add origin <url of remote repository>
What this command does is, it tells git to add the url you provide as the last argument in the command as a remote repository with the name origin(though it is not compulsory to name it ‘origin’ and you can name it whatever you feel like, it is considered as a standard to name it origin).
Once you have added a remote repository, you can push code to it very easily by the following command –
git push origin <branches to push>
This command will push the changes to the remote repository which you named origin. You also need to specify the branches you want to push, if you want to push only the master branch, then enter the command –
git push origin master
If you wish to push all branches, then enter the command –
git push origin –all ,or
git push origin -A
After this, you might have to enter your username and password for github(or any other server on which you hoist your remote repository)
Congratulations!! You just pushed your code to a remote repository.
Pulling Code from a Remote Repository
Now, we will learn how to pull code from a remote repository (i.e, how to make changes in remote repository be updated in your local repository). A remote repository may be updated by another person working on the same remote repository and we can update those changes in our local repository by pulling the changes from the remote repository. This is very simple.
Simply enter the following command in your terminal –
git pull origin <branches to pull>
This command tells the git to pull code from the remote repository which we named origin(refer to previous section).
The last argument we pass should contain the name of the branches we intend to pull from the remote repository.
In the figure shown above, we pull the changes from master branch only from the remote repository. Since no changes were made to the remote repository, hence it says “Already up to date”.
So, with this topic our Git Tutorial comes to an end. We hope we fulfilled the promises we made to you at the beginning of this Git Tutorial. If however, you found that this Git Tutorial was not upto your expectations or if you would like to suggest some improvement, you can always leave behind a comment or contact us. We would of course love to hear back from you, be it an improvement, a suggestion or even a random “Hi”.
Here are some of the topics which you may find interesting if you would like to know more about the subject.