[2/2] What is Git? Beginner's Guide to using GitHub

[2/2] What is Git? Beginner's Guide to using GitHub

Hello World! This is the second part of my blog on 'What is Git? Beginner's Guide to using GitHub' and in this part, we will explore some of the most essential and yet super simple commands for any beginner to get started with GitHub.

In my previous blog, we learned about version control technology and git which is very essential to know before getting started with this blog so please give it a read before if you haven't already.

Previously, the only way to work with GitHub was with the help of git bash but, this isn't the same now. Currently, we have three ways through which we can get our work done. The following are the tools available as of today that help us work with GitHub:

  1. Git Bash - Command-line prompt to run git commands
  2. GitHub Desktop - A GUI app for desktops
  3. Git CLI - GitHub’s official command-line tool.

And in this blog, I will be making use of Git Bash so without any further delay, let's get started.

Step 1 Installing git on your Windows

  • Download the Git Bash setup from the official website: git-scm.com
  • Download the installer.
  • Run the .exe file you just downloaded and follow the instructions in the installer.
  • Run Git Bash by right-clicking on any folder and selecting the Git Bash Here option from the context menu(right-click menu).

Step 2 Basic configuration

$ git config --global user.name "your user name"
$ git config --global user.email "your@user.email"

Step 3 Initialising git

In this step, we will start versioning our project. We will have to navigate to the directory where we want to keep our project with the help of terminal using the standard 'cd' command. Initialize a git repository like this:

$ git init

This creates a new subdirectory named .git that will contain all of the necessary repository files.

Step 4 Add and commit files

In this step, we will start tracking our files and do an initial commit. So, create the files which you want git to version control if you don't already have them in the directory. And for git to start tracking those files, we need to add and commit them to the git using the following commands.

$ git add <file>
$ git commit -m 'first commit'

Step 5 Remote backup

With this, we’ve started versioning our GitHub project locally and if we want GitHub to save and backup the project remotely, we'll have to create a remote repository on GitHub. So go to github.com, create an account, and create a new repository. Then, use the link of the repository to add it as the origin of your local git project i.e where that code will be stored.

### An example using one of my repos
$ git remote add origin https://github.com/Yogita98/Cardiac-Arrhythmia-Classification.git

Step 6 Push the code on GitHub

In this step, we will push our code to GitHub and GitHub will start tracking it!

$ git push origin main

Extras

Check the status of your project

Using the below command we can see which files have already been committed and which haven't. If all the files are committed, then the command will output something like this:

$ git status
### On branch main
nothing to commit (working directory clean)

And if you add a new file to your project, the following will be shown as the status of your project

$ git status
### On branch main
### Untracked files:
###      (use "git add <file>..." to include in what will be committed)
###
###     README
nothing added to commit but untracked files present (use "git add" to track)

Advanced commits

Some advanced level git commit practices are shown below along with the description of each

### Use this to write a short commit message
$ git commit -m 'commit message'

### Add a file and commit it with a single command
$ git commit filename -m 'commit message'

### Modify your most recent commit message
$ git commit --amend 'new commit message' 

### Organise your commit history
$ git rebase -i

Git branching and merging

The main branch of our GitHub repository should basically contain working and stable code and in situations where we have to add a new feature to the existing project, we need to have a separate copy of the project source code. Because adding new code will run through a lot of trials and tests and will eventually break the existing code which we do not want. Hence, with branching, we can isolate the stable working project by putting it to the main branch and creating new branches for new features. When we first create a branch, a complete clone of the main branch is created under a new name. We can then modify the code in this new branch independently. Once the new feature has been fully integrated and the code is stable, we merge it into the main branch!

Branching

The following commands will help us to start working on a new branch

### Create a new local branch to work on
$ git checkout -b branchname

### Switch between 2 branches 
$ git checkout branch_1
$ git checkout branch_2

### Push your new local branch to remote as a backup
$ git push -u origin branch_1

### View all your current branches for the repository (both local and remote)
$ git branch -a

### Rebase the main branch into a local branch
$ git rebase origin/main

### Pushing local branch after rebasing main into local branch
$ git push origin +branchname

Merging

Now to merge the code changes that we have been doing on that new branch to the main branch, we will employ the following process:

### Checkout to the main branch
$ git checkout main

### Merge the new branch to the main branch
$ git merge branch_1

Backtracking and Reseting in Git

Mistakes happen very frequently when we're coding or doing anything per se so to fix such mistakes pushed to our branch, we can make use of the git reset command.

### Switch to the version of the code of the most recent commit
$ git reset HEAD 
### Go back to the commit before the most recent commit   
$ git reset --hard HEAD~1    
### Go back two commits before the most recent commit 
$ git reset --hard HEAD~2     (going back two commits before HEAD)

The above commands are known as 'soft' reset commands which means that our code is reset, but git will still keep a copy of the previous code just in case we need it in the future. But if we want to permanently get rid of that code, we can do so by making use of the hard flag which is also called as a 'hard' reset.

$ git reset --hard HEAD

I hope you find the above information useful. Do follow back if you loved the content and leave your honest feedback about the blog ❤️❤️ Stay stunned and happy blogging!