Basics of Git, Explained (Part 2)
Tags: git, github, beginners, dvcs
Published: 2020-04-09 06:26:14

So in my last [blog](https://dev.to/thewires2/basics-of-git-explained-29kj) i discussed about initialising a git repository and making a first commit. Today we shall discuss about working with a remote repository through git.

Before we get started, we have to configure our user information in the local machine so that the git can keep a record of who is making the changes.

##__1.CONFIGURE USER__

We will configure our username and our email by entering the following commands:
```commandline
$ git config --global user.name "[your-username]"
$ git config --global user.email "[email address]"

for example

$ git config --global user.name "thewires2"
$ git config --global user.email "arnavbagchi04@gmail.com"
```
##__2.MAKE CHANGES__

Now that our username is configured, we are ready to start working with git. We will first see a few useful commands to make user your commit is done properly before pushing it to a remote repository

```commandline
$ git status
```
The git status command displays the state of the working directory and the staging area showing us which files have been staged and which files haven't. The status command does NOT show any information about the committed project history.
```
$ git diff
```
This is a highly useful command which shows us all the file differences that are not yet staged. To exit the diff command press Q

```commandline
$ git add [file name]
```
This command is used to stage all the current changes in the file so that its snapshot will be taken in  preparation for versioning.

```commandline
$ git diff --staged
```
Use this command to see the difference between staging and the last file version

```commandline
$ git reset [file name]
```
Another highly useful command, it unstages the currently staged file but it does not change any contents of the file itself.

After all the stages have been staged, we commit the changes with a good descriptive message of what changes have been made in the commit. I have discussed on how to make a commit in my previous [blog](https://dev.to/thewires2/basics-of-git-explained-29kj). Now that the commit has been created in the local repository, we want to push the changes to our remote repository.

##__3.ADDING A REMOTE__

Now we want to add a remote repository. We will require the URL of the remote repository and we will also have to name the remote. By convention, the remote repository is named as origin. Usually the remote URL is a github/bitbucket repo link.

```commandline
$ git add remote <remote name> <repo link>

for example

$ git add remote origin https://github.com/thewires2/Django_Project
```

The above given example is a django project which i am working on my local machine and i push the code to the remote github repo so that my partners can see the changes.

##__4.PUSHING TO REMOTE REPOSITORY__

Now we shall push the files/commits to our remote repository.This shall include all the changes that have been committed in the local repository.

```commandline
$ git push origin master
```
Here the name of our remote repository is origin as we previously named it. Master is the main branch of the git repository in which the final changes are committed.

There you go! You have successfully pushed your code to the remote repository!

##__5.PULLING FROM REMOTE REPOSITORY__

Now suppose that your friend wants to download the remote git repository.
They will have have to initialise a an empty git repository and add the remote as shown above.
Now its a simple pull from the remote :)
```commandline
$ git pull <remote name> <branch name>
$ git pull origin master
```
The committed changes can be viewed by using the *git log* command.There we go! We have successfully pushed and pulled a remote repository!

I will be discussing about branches with emphasis on rebase in my next blog post. Till then, Ciao!

Hey there !
I am Arnav , an engineering student hailing from Mumbai.