What is Git & Most Useful Git Commands

1 390

In this article, we are discussing “What is Git & Most Useful Git Commands”. Git is known as version control system for tracking computer files and collaborating with multiple people on the same file/project. It is also known as source code management in software or any web application development.

Git is free & open source software under the GNU (General Public Licence). So you can use it in your personal or commercial project. It’s also easy to learn for students or developer who’s still not familiar with the Git. We will discuss more on version control system in our future articles. You can download the latest version of git here.

Some of the common git commands

Git Config

Following command is used to configure the author name and email address, which we used with your commits.

git config --global user.name "Pankaj Sood"

git config --global user.email "info@codebriefly.com"

Git Initialize

Following command is used to create a new local git repository, also we can initialize git in any of our previous projects.

git init

Git Clone

Following command is used to clone a repository from the remote server.

// Create a working copy of a local repository:	
git clone /path/to/repository

// For a remote server, use: 
git clone username@host:/path/to/repository

Git Add

Following command is used to add files to the staging area.

// Add specific file to staging area
git add <filename> 

// Add all files to staging area 
git add *

Git Commit

Following command is used to commit all staging file changes to head.

// Commit changes to head (but not yet to the remote repository)	
git commit -m "Commit message"

// Commit any files you've added with git add 
// and also commit any files you've changed since then
git commit -a

Git Push

Following command is used to push all the files to the remote repository.

// Send changes to the branch of your remote repository.
git push origin master
// Push the branch to your remote repository, so others can use it:	
git push origin <branchname>

// Push all branches to your remote repository:	
git push --all origin

// Delete a branch on your remote repository:	
git push origin :<branchname>

Git Pull

Following command is used to fetch and merge all the changes on the remote server to your working directory.

// Pull changes from specific branch
git pull origin <branchname>

// Pull the current branch from the remote server
git pull

Git Status

Following command is used to show the list of the files you’ve changed and those you still need to add or commit.

git status

Git Remote

Following command is used to connect your local repository to the remote server.

// Add the server to be able to push to it:	
git remote add origin <server>

// List all currently configured remote repositories:
git remote -v

Git Checkout

Following command is used in multiple areas. Such as creating a branch, undo local changes and many more.

// Create a new branch
git checkout -b <branchname>

// Switch from one branch to another branch	
git checkout <branchname>

// You can replace the changes in your working tree with the last content in the head.
git checkout -- <filename>

Git Branch

Following command is used to check the list of branches and delete any of specific branch.

// List all the branches in your repository.
git branch

// Delete the feature branch:	
git branch -d <branchname>

Git Merge

Following command is used to merge a branch into your active branch.

git merge <branchname>

Git Diff

Following command is used to view all the merge conflicts.

git diff

// View the conflict against the base file.
git diff --base <filename>

// Preview changes, before merging
git diff <sourcebranch> <targetbranch>

Git Stash

When we are working on part of your project. And you face a situation you need to pull the changes from a remote server but an issue is your work is not completely done. In this condition, git stash command is used to store all of our modified tracked files and staged changes into a stack of unfinished changes. We can reapply at any time.

// Create a new stash
git stash

// Show the list of available stash
git stash list

// Reapply the last stash files to the working area
git stash apply

// If you want to apply one of the older stashes, you can specify it by naming it, like this
git stash apply stash@{n} // n is a stash index, you can get this value when you use git stash list

// If you want to unapply the applied stash
git stash-unapply

Conclusion

In this article, we are discussing the git and some of the most useful “git commands”. I hope you get the good knowledge about the git. Please feel free to add your comments if any query or you can submit your feedback 🙂


You may like

Leave A Reply

Your email address will not be published.

1 Comment
  1. Josh says

    Hi Pankaj,

    How can i remove the committed files form the git?