Basic Git Commands

Akshay Waingankar
4 min readJul 29, 2020

Git is one of the best open-source version controlling tool and widely used in web development companies. It consists of many features along with their respective commands which help the developer to maintain their code clean and separated from others work.

Let's see some basic command which is frequently used.

  1. Pull request

When we need the updated files from the server to our local machine the PULL request is used. It always a best practice to check updated files from the server. PULL to have the updated code and on sync with the team

//Initialize git to the folder location
- git init
//Get updated files from master
- git pull origin master

2. Adding files

//Initialize git to the folder location
- git init
//Check status like number of files present in the folder or number of modified files written in red color (optional).
- git status
//Adding all files inside the folde
- git add .
OR
//Adding specific file only
- git add "filename.extension"
//Now add comment which describes the purpose of your file pushing to git repository
- git commit -m "Specific comment"
//Syncy with your GitHub repository via link for pushing the file(s) to server
- git remote add origin https://your_git_repository_link.git
//Checking git status(optional). And now it will shows file names with green color.
- git status
//Pushing files to GitHub repository
- git push -u origin master

3. Pushing Updated/Modified files

//This will give you all the modified files from the folder(optional)
- git status
//Adding all updated files
- git add -A
OR
//Adding specific file only
- git add "filename.extension"
//Now add comment which describe the purpose of your file pushing to git repository
- git commit -m "comment what changes you made in breif"
//Checking git status(optional). And now it will appear file names with green color.
- git status
//Pushing files to GitHub repository
- git push -u origin master

4. Branching and Merging

This feature is used when you are not sure about pushing files on the mainstream repository as your developing new feature and code is not yet completed or tested at your local machine.

//Create new branch
- git branch branch_name
//Checking all branches in which green name suggest your present branch(Optional)
- git branch
//Moving from one branch to your working branch
- git checkout your_branch_name
//Now add your files
- git add .
OR
- git add "file name"
OR
- git add -A
//Add commit
- git commit -m "comment what changes you made in breif"
//Pushing files to GitHub repository
- git push origin your_branch_name
//When your code is fine and now you want to merge it to master branch
- git checkout master
- git merge your_branch_name
- git push origin master

Just now you merge your branch code to the main source. If you are done with branch and no further requirements of it, then delete it from local as well as from remote machine.

//Delete from local
- git branch -d branch_name
//Delte from remote
- git push origin --delete branch_name

5. Rewrite commit message

If you want to correct previously committed message then follow below steps

//Changing commit statement
- git commit --amend

one editor will open

type i, now this will allow you to insert a new commit statement.

Type new comment, press ‘Esc’ and type ‘:wq’ and press enter.

//Push to server
- git push origin master

6. Revert up to specific version

This comes into the picture when something wrong happens to your code and you want to get back to its last stable version.

Warning: Use this command wisely. As it will change your code and remove dependencies from project

//Back to previos commit using their id
- git checkout commit_id .
//Check your current branch. If you are not in master branch then go back to master branch
- git branch
- git checkout master
//Add modified file
- git add -A
//Add new commit and mention you reason to go back to this version
- git commit -m "commit message"
//Push to remote server
- git push

I hope you guys got a basic understanding of git and you will implement it to your day to day coding practice.

Let me know in the comment section if this blog is relevant or need more description.

You can visit my git account where I normally put my daily work. Your contribution to my public repository is always welcomed.

Other articles

--

--