Basic Git Commands Every Developer Should Know(Git & Github)

Abdullah Al Jahid
11 min readDec 29, 2020

--

This is a short article on the usages of git and github. It covers the installation process of git, some basic git commands every developer should know.

Git is a version control system/tool that lets you manage and keep track of your source code history. GitHub is a cloud-based hosting service that lets you manage Git repositories. Git is the tool, GitHub is the service for projects that use Git.

Installation

To install git follow the following link:

https://git-scm.com/book/en/v2/Getting-Started-Installing-Git

If you are using a Linux machine(Ubuntu) you can install and use git/git bash via terminal. To install git you have to run the following commands on your terminal.

Step-1: Update Default Packages

$ sudo apt update

Step-2: Install Git

$ sudo apt install git

Step-3: Confirm Successful Installation

$ git --version

Output: git version 2.17.1

Step-4: Set Up Git

$ git config --global user.name “yourName”$ git config --global user.email “youremail@domain.com

Replace “yourName” with your github username and “youremail@domain.com” with your email address.

You can see your git config info by running the following command:

$ git config --list

If you need to edit this file, you can use any text editor such as nano or gedit.

$ nano ~/.gitconfig

Initialize Git (Normal Directory -> Git Repository)

Now go to the specific directory which you want to track with git. Then open git bash /terminal there and run the following command.

$ git status

Git will not make any directory automatically tracked. You must tell git to track your directory by a simple command:

$ git init

After running this command, your repository is a git repository. You can start your work now. To check if git initialization was successful or not run $ git status again. Output will be something like this:

Commit Changes

As you have already initialized a directory as a git repository to track your files, whenever you get to that directory you can run $ git status to see the current state of your git repository.

Here note that the working tree is clean/ nothing to commit means all changes are tracked successfully. untracked means we didn’t add it for tracking. new file/modified means we added for tracking but didn’t commit the changes. stage means to add for tracking, unstage means not to add for tracking.

To stage changes for a selective file run this command

 $ git add <filename>

To unstage changes for a file run

$ git rm --cached <filename>

To commit the changes of staged file(s):

$ git commit -m “Write your appropriate commit message here”

To stage changes for all untracked files and commit them:

$ git add .$ git commit -m “Write your appropriate commit message here”

If you are lazy like me then you may try the next command to stage all changes and commit them(skipping “ $ git add .” command)

$ git commit -a -m “Write your appropriate commit message here”

If you want to discard your changes(staged files) you can use the following commands.

$ git rm --cached <filename>(when files are staged) or$ git restore <filename>

Publish Local Git Repository to Github

If you want to publish your repository, github is the most popular medium to do this. This is called git remote. Let’s do this. If you are doing this for the first time, it is a little stressful, but don’t worry.

Step-1: Create a new repository in your github account.

(Go to github -> Repositories ->New)

Step-2: Give a valid name of your repository

Step-3: Choose Privacy setting(Public or Private). If you make it public anyone will be able to see your repository.

Step-4: You can initialize your repository with a README.md file if you want. But if you are going to import an existing repository you should skip this step. I’m going to skip this step.

Step-5: Finally click the “Create repository” button.

If you work with a new repository (I mean don’t have locally) then follow the next few steps.

Step-1: Initialize a directory as a git repository via $ git init

Step-2: Create a new README.md file

Step-3: To stage changes $ git add README.md

Step-4: To commit changes $ git commit -m “commit message here”

Step-5: Set master as a default branch $git branch -M master (can be skipped)

When you are done. That means now you have a git repository locally. Now we will publish this on github or we may say we will push it to a remote repository.

Get the link of that repository you have created earlier on github. Get the HTTPS link. You will get this link when you click on your repository on github. Here is mine https://github.com/Jahid1999/GitTesting.git

When you have this link, run the next command in git bash or terminal.

$ git remote add origin “https://github.com/yourGitUsername/repositoryname.git

Note: please put your actual github username and created repository name.

If you want to confirm your command executed successfully or not try this command

$ git remote -v

After adding your repository remotely now you can publish your saved work on github.

$ git push -u origin <branchName>

By default, the branch name will be master unless we create another branch. We will see branch creation within a few minutes.

If you are on Linux and using the terminal you may be asked to enter your github username and password.

You can see, now you have your locally committed files on github.

Git Branch

Branching is a feature available in most modern version control systems. Branching in other VCS can be an expensive operation in both time and disk space. In Git, branches are a part of your everyday development process. Git branches are effectively a pointer to a snapshot of your changes. When you want to add a new feature or fix a bug — no matter how big or how small — you spawn a new branch to encapsulate your changes. This makes it harder for unstable code to get merged into the main codebase, and it gives you the chance to clean up your future’s history before merging it into the main branch. Moreover, If you want to work in a repository with other contributors then branching is a must.

To see the list of branches of your local git repository run the following command $ git branch

As you can see I’ve two branches named demo_branch and master. Here green one or ‘*’ pointed one means current working branch.

Create a branch:

To create a branch run

$ git branch <branchname>

Note: If you follow the previous command to create a new branch you will found your created branch staged as HEAD. (In last committed version of default branch)

Switching Between Branches:

To switch into a branch from another you have to a simple command

$ git checkout <branchName>

Attention!!

Now let’s see the power of the checkout command. Suppose you are building a module/feature of your group project and after doing a lot of coding suddenly you realize that you are working on an old branch that was created to build another module. What will you do now? If you are thinking about applying $ git branch <branchName> and $ git checkout <branchName>. Let me remind you that you will lose your code by doing this. But don’t worry, the very next command will ease your situation.

$ git checkout -b <newBranch>

After creating a new branch git will automatically switch into the new branch with changes. If you already have a branch named <newBranch> git will switch into it with the changes you had made on the current branch.

You can u--track or--no-track option along with the previous command.

To Rename a Branch

Step-1: Switch to the branch you want to rename

$ git checkout <old_name>

Step-2: Rename the branch

 $ git branch -m <new_name>

To Delete A Branch

If the branch named develop is already merged with the master, then run

$ git branch -d <branchName>

If it is not merged with the master but you still want to delete, then run

$ git branch -d <branchName>$ git branch -D <branchName>

Usages of git stash:

Use $ git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEAD commit. You can restore stashed changes to your repository via $ git stash apply

After applying $ git stash apply output will look like:

To see the stashed list run $ git stash list.

Push new Branch to Remote Repository / Github:

To push a new branch:

Step-1:

 $ git add . $ git commit -m “leave commit message here”   'or' $ git commit -a -m “commit message”

Step-2:

$ git push -u origin <branchName> 'or'$ git push origin <branchName>

To push a new branch as a specific name:

$ git push origin <localName>:<newName>

To delete branch from Github (means git origin):

$ git push -d origin <branchName>

Pulling from a Repository:

$git pull origin <branchName>

If you are on branch ‘a’ and want to pull the latest version of code from branch ‘b’. Then run $ git pull origin b. If you are on branch ‘a’ and want to pull the latest version of code from this branch. Then run $ git pull origin a.

After pulling you have to $ git add . and $ git commit -m “message”. After that, you can work with the latest version of code.

Note: While pulling you may face merge conflict. To know how to resolve merge conflict(s) read the next point of this article.

Merging and Merging Conflict:

Merge conflict means, suppose you are working in two branches parallelly and made changes to the same portion of a file, then git won’t understand which changes to keep. This is called merge conflict. If a merge conflict happens, git will make you choose about what changes should be kept.

To merge a branch with another

$ git merge <branchNameYouWantToMerge>

Here, current change means this line(s) of code currently is in your branch and Incoming change means the line(s) of code from the branch which you wanted to merge. Now you have to decide which part of the code you want to have. Keep that part of the code in your file and remove other parts. After that $ git add . and $ git commit -m “message”.

To see your all commits

$ git log

Now you are in the git log file.To exit from here type “q” or “:q”.

To see the last commit of all branch

$ git branch -v

To see which branch is merged and unmerged with me (current working branch)

$ git branch --merged$ git branch --no-merged

I am currently on demo2 and demo1 and master branch is merged with me.

How to Go Back to the Previous Version:

Deleted all the files and pushed them. Lost a bunch of files?? Don’t worry you can easily get them back.

To go back to your previous version

$ git reset HEAD  'or' 
$ git reset --hard HEAD.

If you want to delete the last few commits and go to a specific version

$ git reset HEAD~n .

Here n is the number of commits you want to delete.

Git log

But what if you don’t want to delete your commits and want to get back to a previous version of your code? Don’t worry about that. You can do it as well. For this run

$ git reset --hard <hash-or-ref>

From the above picture, you can see that each commit has a hash value. You will get your commit hash value from the $ git log. Copy the hash value and run the above command to go back.

For a better understanding on this topic read this https://stackabuse.com/git-revert-to-a-previous-commit/

Delete Remote of Git

That means you won’t be able to push your file to github from your local repository.

$ git remote rm origin

Don’t worry. It can be added again

$ git remote add origin “url_link”

Git Clone

You can clone a public repository and work on it separately.

$ git clone “url_here”

(example: git clone “https://github.com/Jahid1999/GitTesting.git”)

After cloning a repository in a folder you don’t need to initialize it as a git repository.

Deleting Git Repo(Locally)

If you want to remove a directory from a git repository, run

$ rm -rf .git

This mainly deletes the .git folder.

If you are on Linux and want to know more about git commands, open the terminal via Crtl+Alt+t and run the command $ man git.

That’s it from my side. Thanks for reading. If you find it helpful please let me know via your responses/comments and hit the like button.

Thanks a lot.

--

--

Abdullah Al Jahid
Abdullah Al Jahid

Written by Abdullah Al Jahid

Institute of Information Technology | University of Dhaka

No responses yet