Understanding various git commands

If you are working on a project then you have to maintain it properly and that’s why we use git as our VCS(version control system) and github or gitlab as our cloud service where we store all our code remotely. Below there will be some basic git command and their description.

Photo by Yancy Min on Unsplash

Initializing git repository

When you first start a project using git you have to initialize the git in side your project directory.

$ cd my_folder
$ git init
Initialized empty git repository in my_folder/.git/

Cloning repository

Suppose if you want to work on a project that is already on github so in order to contribute to that project first you have to clone that repo to your local machine. Here git clone comes to help you in that.

$ git clone ssh://git@github.com/[username]/[repository-name].git

Status of a repository

If you want to check the status of a repository like which files have added and deleted and modified then you have to execute the below commands. It will also show the tracked and untracked files of the repository.

$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout –– <file>..." to discard changes in working directory)
modified: tasks.txt
no changes added to commit (use "git add" and/or "git commit –a")

Adding files to staging area

If you add a new file to your repository or you have modified some files then before committing you have to move those files to staging area. Here you have certain cases like you want to add files to staging area by one by on or you want to add all files to staging or you want to add all tracked files to staging area except untracked files. Below commands for all cases.

$ #Add specific file to staging area
$ git add [file_name]
$
$ #Add all new and changed file to staging area
$ git add -A
$
$ #Add all tracked files to staging area
$ git add -u

Committing changes

After adding files to staging area you have to commit those changes. Basically you are saving those changes as commits. Every commits has it’s commit message and unique commit ID.

$ git commit -m [commit message]

Removing files or folders

Suppose you have to delete some files from index which is not needed then you have to tell git to remove those and here git rm comes to help you.

$ git rm -r [file-name]

Branching

It is recommended that you should not change directly in the master branch. When you are experimenting something or adding any feature then always you should create a new branch and make changes there. So bellow command is showing you how to create a new branch.

$ git checkout -b <your_branch>
Switched to a new branch "your_branch"

Now type git branch to see all local branches. And if you want to see all the branches include local and remote then you have to type git branch -a. Now if you again want to come back to your master branch then you have to type below command.

$ git checkout master
Switched to branch 'master'

If you want to switch to the last checkout branch then type git checkout -. Now sometimes you have to delete a specific branch so in this case you have two branches one is in your local machine and one is in your server.

$ #delete your local branch
$ git branch -d <your_local_branch>
Deleted branch <your_local_branch> (was 4335a0a).
$
$ #delete your remote branch
$ git push origin --delete <your_remote_branch>

Merging

When you have finished your all features and changes in your branch it is now ok to merge with master branch. In order to merge any branch with master branch there we execute the below command.

$ git checkout master
Switched to branch 'master'
$ git merge <your_branch>

`Basically in merging there is two branches one is source branch and another is target branch and in the above scenario the master branch is target branch and other is source branch. If you want to do the above operation in one line then you can do –

git merge [target branch]

Stashing

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.
And use git stash clear to remove all stashed entries.

Sharing & updating project

After all committing stuff it is the time to push the changed to the remote server or otherwise if your local computer get disturbed you will loose all changes. Use the below command –

$ git push origin <your_branch>

Sometimes you are just working on a specific branch for a long time and it is possible that git will remember the branch name for you. Use git push -u origin <your_branch> to make git remember your branch name. After this you just have to execute the command
git push and it will automatically push the commits in the remembered branch.

Now suppose you have two laptops and from both you are maintaining the project so it is very necessary for you that you keep updated the project in the both laptops. So git pull will update the local repository to the latest commits.

So git has local repository and remote repository so when ever we push something it will go to the remote repository. So it is very important to tell git that what is our remote repository. In order to add the remote repository we execute the bellow commands –

$ git remote add origin ssh://git@github.com/[username]/[repository-name].git

Sometimes we clone the repository in the https format url or we have set the remote url in https format but we are planning to switch to ssh. Then we will execute the below command and everything is changed . Below we are switching to ssh remote url.

$ git remote set-url origin ssh://git@github.com/[username]/[repository-name].git

Inspection & Comparison

If you wand to see commit history then I already published a blog about git log you should checkout. Now if you want to see what you have changed after the last commit then do
git diff. It is good to see the changes you have made in the feature branch before merging to the target branch. Execute the below command for that –

$ git diff [source branch] [target branch]