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.’
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:
- git config
- git init
- git clone
- git add
- git diff
- git reset
- git status
- git commit
- git rm
- git log
- git show
- git tag
- git branch
- git checkout
- git merge
- git remote
- git push
- git pull
- 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.
This is how we config our github account details by using the ‘git config’ command
git config user.name “username”git config user.email “firstname.lastname@example.org”
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.
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.
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.
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.
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,
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.
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’ 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’ is the command used to switch from the current branch to the branch you are interested in.
This command merges the specified branch’s history into the current branch.
This command unstages the file, but it preserves the file contents. So it can be shown as modified in git status.
This command deletes the file from your working directory and stages the deletion.
This command is used to list the commit details for the current branch.
This command shows content changes of the specific commit. ‘git show [commit]’
This command fetches and merges changes on the remote server to your working directory.
The ’git stash’ command removes your uncommitted changes (both staged and unstaged).
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’ 👋.