Dive into Git with command line — Daily need git commands

Deepika Srinivasan
Nerd For Tech
Published in
7 min readMar 2, 2021

The word Git is a slang used to insult people which denoting an unpleasant, childish, annoying silly person. Well, the git we are going to talk about today does not have that meaning.

In my opinion learning Git is essential for programmers and mostly for Developers. I started using Git from 2019 and that saved me from loosing a lot of time.

Before using Git I used to copy the code I wrote into USB stick and merged it with my team mates code. It was a disaster. There will be a lot of conflicts while merging everyone’s project and you will be frustrated.

So lets dive into Git.

What is git? — An introduction

Git is a free and opensource distributed version controlling system. It is designed to handle small to very large projects with speed and efficiency. Git’s purpose is to keep track of projects and files as they change over time with manipulations happening from different users.

Git stores information about our project in a Repository and it has the commits we made.

All these information are stored in the .git file which is in your project. Most of the time .git file will be hidden by default in most systems.

While you are working as a term everyone can have their own branch and then can merge as a one single project.

Why command line for git?

I have seen most of the people use GitHub or BitBucket and do all the necessary things using the GUI. It will be easier to do using them, but if you get and bigger error or conflicts it will be easier to solve using command line. So it is best to use command line for day today git uses.

Getting Started

Check if you have already installed Git on your machine by typing git version on command line/ terminal.

If you haven’t installed git, go to this like to check the documentation of installing Git and download for your OS.https://git-scm.com/downloads

Configure

This command sets the author name and email address respectively to be used with your commits. (Your GitHub Username and Email address)

git config — — global user.name “YourUserName”

git config — — global user.emali “Your@Email”

Initializing a git repository

Go to your project folder and open git bash.

Go to the project folder, right click and select “Git Bash Here”

Then type git init in git bash to create .git file. This file will store and track all of your work.

Type “git init” and this will create an empty .git file

I have already created a project called index.html. To check which files/folders are untracked (These exists locally, but isn’t a part of the Git repository) by using “git status” command.

Then add the files you need by using add command. There are 3 ways to do that.

git add <file name> — Add only one specific file

git add . —(with dot) Add all the files in the folder

git add -A — Same as “git add .”

Now the index.html file is trackable. If we want to make it un trackable we can use “git reset <file name>” command or “git reset” to make all the files untracked.

File changed as untracked.

Commit the files

To have a complete idea about different ways of committing please refer this page. https://chris.beams.io/posts/git-commit/

Normally we use one line message to commit. For that we use git commit -m “Your message” command. But Sometimes you need to commit with more than one line. In order know how to do that please refer the above link.

We can use “git log” to check our commits.

Also wecan use “git log — — oneline” to see the commit details and commit numbers (SHA 1) in one line.

Push to GitHub

After committing the message we can push the commits to GitHub. Create a repository and copy the link of that repository.

Copy this link ( of your repository)

(To know how to create a repository in GitHub refer the documentation. https://docs.github.com/en/github/getting-started-with-github/create-a-repo )

Then in the bash type “git branch -M master” to create master branch. Then add the git repository as remote repository by typing “git remote add origin <your repo link>”. Then push the files to GitHub using “git push -u origin master” command.

Successfully added to GitHub

If we check the GitHub all of the files will be added to there.

Work with Branches

We can create branches to make our work easier and prevent from more error. Use “git branch <branch name>” to create a branch.

Creating branch sample1

Then checkout to that new branch. “git checkout <branch name>

Now create another file/edit current file in your project and check using “git status”. Then push the commits to sample1 branch.

Now if we go to you GitHub repository we can see there will be another branch. We can see the branches you created by typing “git branch” and the current branch

Now sample1 branch have extra one file which is not in master branch. So now we need to merge sample1 branch to master. So in order to do that we need to switch to master branch.

Then use “git merge <branch name>” to merge branches. Then push the changes to GitHub master branch.

Now if you check the GitHub repository both files will be merged. The sample.html file will be added to master branch.

Merge Conflicts

Sometimes when we are trying to merge branches we will get some conflicts .

If something like this happens we need go to IDE. It will show what are the conflicts. You can edit in the IDE and able to merge easily.

You can select from the top and change or you can edit by your self.

After editing in the IDE you can simply add the files and make a commit. Then it will be solved automatically. Do the same to the other branch too. Then check GitHub.

Deleting a Branch

So now the both branches are merged successfully. Think that we don’t nee the sample1 branch anymore. So we are going to delete it.

The command “git branch -d <branch name>” will delete the branch in local repository. In order to delete from remote repository you need to use, “git push origin — — delete <branch name>” command. Then if we check GitHub repository there will be master branch only.

Creating Tag

Git tags are used as reference points in your development workflow. You might want to create new Git tags in order to have a reference to a given release of your software.

You can see, near the branch there is a tag. You can add as many as tags you like.

More than these there are some other basic commands:

  • git pull :- pull changes from remote repository
  • git clone <link> :- clone a project
  • git remote -v :- check where the origin of the cloned repository
  • git branch -a :- The branch we are working
  • git diff :- shows the file differences which are not yet staged
  • git rm :- deletes the file from your working directory and stages the deletion

Conclusion

It is always better to use command line for git. It doesn’t scary as it look. If you start doing anything in command line it will make your work easier and interesting.

--

--

Deepika Srinivasan
Nerd For Tech

Undergraduate | BSc(Hons). Software Engineering | MBA | Software Engineer @Well Tech Solutions | Writer @ Nerd For Tech