Git and GitHub: Part 2

Harsh
6 min readAug 5, 2020

In Part 1, you all learned about some basic Git commands and I hope you now have a clear understanding of them. So let’s move forward and learn some more interesting things about Git.

Let’s GIT it!

Configuring our editor

Before we move forward, let’s first configure our editor because there will be further use of it. To configure the editor, use this command-

Syntax : git config — global core.editor “notepad”

This will set notepad as your default editor.

git amend

git amend is used to modify our commits.

Let’s take an example, where you have added two new files into your repository and now you have to do a git commit. After you have done a commit, you realize that you have mentioned only one file in the commit message and you forgot to mention the other.

Now, you want to change the commit message and mention the other file too. To do so, you can use this command-

Syntax : git commit — amend

As soon as you type this command, a text editor will open in which your last commit will be present. Change the commit and mention about the other file and save the changes and close the text editor.

And when you will close the editor, your changes will be reflected and your commit will now mention the two files that you have recently added.

You can also do git log to check your last commit and see the changes.

git revert

git revert is used to revert back our changes that we have recently made to our file.

Let’s make some changes in index.py and do a git commit to tell about the changes we have just made in our file.

Now, let’s call our file by typing the name of it so that it can display its contents.

Oops! It’s showing an error saying that I forgot to add inverted commas to the print statement due to which command prompt is showing an error. So what can we do now? Don’t worry! We can revert back our changes by using the git revert command.

Syntax: git revert HEAD

git revert command reverts back our changes that we have just made to our file. As soon as we type git revert, a text editor will open displaying our last commit with a unique commit id.

In the text editor, mention the reason for your rollback i.e. why you are reverting back the changes. Save your information and exit the editor.

Now, when you try to call your file to display it’s content, you will notice that your previous changes are no longer there and your file is running successfully.

There are more syntax for git revert which we will learn in the further blogs.

Branches in git

Branches play an important role in git. The default branch name in Git is master branch. We often create new branches to isolate our code from the original code and our work from others working on different branches. Later on we can merge our branches back to the master branch.

Merge is to combine a branch with the main branch i.e. master

Take a hypothetical situation as an example. Consider that you are working on a project and have been told to work on the Sign Up page and another person is assigned to design the login page. In cases like these, both of you can create your own branches, make edits until your code runs fine and once that’s done, you can merge your branches with the master branch.

git branch

git branch is used to tell the name of the branches present in our git repository.

To check how many branches are present there in our Git repository use this command-

Syntax: git branch

Since there is no branch created yet, it is showing the default branch of our git repository i.e. master

Adding a new branch

To add a new branch to our git repository we can use this command-

Syntax : git branch <name of the branch>

So, I have added a branch called new to our repository. To check it, we can use the git branch command to list our branches present in our git repository.

Finally, a new branch has been created in our repository.

git checkout

The git checkout command switches between the branches in our repository. At present, we are working on the master branch. Now we have to work on the new branch. To change from master branch to new branch, use this command-

Syntax: git checkout <name of the branch>

That’s it! We are all set to work on our new branch new.

But, isn’t this process too long?

First we have to create a branch, then switch on that branch to start working on it. We also have a shortcut for doing this. By using the below command, you can create a branch and directly switch on to it without any further commands.

Syntax: git checkout -b <name of the branch>

You can see that by using the above command, a new branch new_feature was created and we were automatically switched onto it without any further command. This saves your time and is good for future.

Deleting a branch

To delete a branch which is of no further use, use this command-

Syntax: git branch -d <name of the branch>

That’s all for this Part 2. I hope you all understood the commands. If you have any doubt(s) regarding this, you can leave a comment or contact me on LinkedIn.

--

--