Git commands for Beginners

Don’t worry if you don’t understand this in a single shot, let’s get some hands-on experience and have fun doing this.

First, find below the basic structure one needs to visualize before understanding the concept behind ‘git codes.’

Ref: https://www.youtube.com/watch?v=ofyxPG-uiJI&list=PLVx1qovxj-aksfb_7PpZQQGNjvvcoEJ-b&index=4

Starting from the Remote Repository, it can be either GitHub (or) GitLab; from there we clone it into our current working directory and if we want to make any changes in the corresponding clone files after making necessary corrections, we can revert it back to the remote repository which basically has two more stages: Staging and Local Repository.

Basic Commands and their description in Git:

  1. git config
  2. git init
  3. git clone
  4. git add
  5. git diff
  6. git reset
  7. git status
  8. git commit
  9. git rm
  10. git log
  11. git show
  12. git tag
  13. git branch
  14. git checkout
  15. git merge
  16. git remote
  17. git push
  18. git pull
  19. git stash

These are the basic commands which we use on a daily basis.

Let’s start our journey by picking each and every command and try understanding how it will be implemented.

Let’s make it easy👨‍💻

After creating a github account (company mail id preferred), let’s make a repository in it. Now let’s try using our commands.

git config:

This is how we config our github account details by using the ‘git config’ command

git config user.name “username”git config user.email “[email protected]

git init:

Whenever you want to create a new git repository in your working directory, the first thing you need to do is to go inside the working directory, open terminal and type this command ‘git init’.

In the above image, you can see we have created a directory in the name of ‘ign_workspace’. We open this particular directory in the next step, so now our current working directory is changed to ‘ign_workspace’. Thus,we have an empty  directory i.e. it doesn’t have a repo in it. In order to create a repo, we need to use the command ‘git init’ which basically creates a repo in the current working directory.

You can see the .git folder which is created in the hidden state.

Until now, we have created a directory and initialized our repo. 

git clone:⬅️

As you can see in the picture below, we try to clone the data from github repo to our current working directory.

The https link needs to be copied and used along with our ‘git clone’ command.

This is how we can use the ‘git clone’ along with the https,

So, now all the files present in the corresponding repository have been downloaded into your current working directory.

git add:

With the ‘git add’ command, the necessary changes that you have made to your file can be directly added to your Staging area.  You will find all these staged files in the “D:current working directory/.git” directory which will be in a hidden state in your working directory.

git diff:

To understand how to use this command, let’s first assume that we have a file pgm1.py and let’s make some changes to it. Using this command, ‘git diff file1/pgm1.py’ we are able to check changes in the “pgm1.py” file compared to the previous version.

git status:

Status command displays the state of the working directory and the staging area. It lets you see which changes have been staged, modified, non staged, untracked and those that are to be committed.

So, if the ’git add’ is skipped, we will see the above error, 

git commit:

The commit command is used to add the modified files with a message attached to it. For example, in the below figure you can see that we have used the command line git commit -m ‘Added a folder file1’ where ‘Added a folder file1’ is the message we use to tell others about the changes we made.

So, till now you have committed the changes and stored them in the local repository. Now, you need to push them back to the remote server from where you have cloned.

git push:➡️

By using this command ’git push’ you can successfully push your modified work from the local repository to the remote repository.

Let’s get our hands dirtier by using a few more commands. Don’t miss reading this blog until the very end.

git branch:

‘git branch’ is the command used to create a new branch. 

By default,  you’ll be working under the main branch. So, here in the command to create a new branch having the name ’new_branch1’ is created.

git checkout:

‘git checkout’ is the command used to switch from the current branch to the branch you are interested in.

git merge:

This command merges the specified branch’s history into the current branch.

git reset

This command unstages the file, but it preserves the file contents. So it can be shown as modified in git status.

git rm:

This command deletes the file from your working directory and stages the deletion.

git log

This command is used to list the commit details for the current branch.

git show:

This command shows content changes of the specific commit. ‘git show [commit]’

git pull:

This command fetches and merges changes on the remote server to your working directory.

git stash

The ’git stash’ command removes your uncommitted changes (both staged and unstaged).

Conclusion:

By reading this blog, you not only have a good idea on how to connect the working directory with the remote repository and vice versa, but also have learned most of the basic commands that we use in the git environment. Hope you have enjoyed the first blog in this ‘Getting Started with git’ series. Let’s catch up again in part 2 – ‘Github for beginners’ 👋.

One Reply to “Git commands for Beginners”

Leave a Comment