loading...

Basics of Git, Explained (Part 2)

thewires2 profile image Arnavbagchi ・3 min read

So in my last blog 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:

$ 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

$ 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

$ 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.

$ git diff --staged

Use this command to see the difference between staging and the last file version

$ 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. 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.

$ 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.

$ 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 :)

$ 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.

Posted on by:

thewires2 profile

Arnavbagchi

@thewires2

I'm an engineering student. I'm interested in coding.

Discussion

markdown guide