Basic GIT commands with examples

Breadcrumb

Git is an essential revision control system for programmers because it aids in managing changes as well as enhance collaborative work. For beginner level programmers, learning to using git on the command line could be a necessary first step but memorizing every command isn't quite simple.

Although there are Git GUI software for the Windows and Mac OS such as SmartGit, learning to use the command line is recommended. On the git bash command line, every git command must start with the keyword "git".

Here are some basic Git commands with examples.For full list of git command,click here.

git init: when you create a new project, you have to initialize it on git before you can add and commit it. This will create the ".git" directory in your project folder. To initialize a git repository:

git init

git clone: to replicate a repo from the remote source on your local machine, you use the clone command. When you clone a repository, it allows you to update your local files from it as well as push your changes to the repo if you have the permissions.

git clone git@bitbucket.com:owner/opensource.git

git add: is used to add your new file or changes to your git index so that it can be tracked.

git add .

git rm: removes files from your index and your working directory so they will not be tracked.

git rm filename

git status: compares the status of files in the index and the working directory and display the differences. It gives you the current status of your project files by showing you files that are untracked (only in your working directory), modified (tracked but not yet updated in your index), and staged (added to your index and ready for committing).

git status

git commit: creates a new commit object pointing to changes written in the index and sets the branch to point to that new commit. Git commit requires a short descriptive message of your changes. Unlike the command, the message must be in quote.

git commit -m "php code changes commit"

To add and commit your changes in git with one command, use:

git commit -a -m "adding and committing changes with a single command"

git fetch: fetches and downloads all the files from the remote repository that are not present in the local one.

git fetch origin

git pull: is used to update the local folder with the changes on the remote repository. The command fetches the files from remote server and merges with the local one.

git pull origin

git push: pushes all the changes made on the local working environment to the remote repository and advances its branches.

git push origin master

git remote: shows all the remote versions of your repository.

git remote origin

git branch: lists all existing branches, including remote branches if ‘-a’ is provided. To create a new branch add the name to the command.

git branch -a
git branch newbranch

git checkout: is used to switch between available working branches. You can as well use this command to create a new branch and switch to it with one command.

git checkout
git checkout newbranch

git diff: generates statistical differences between paths or files in your git repository, or your index or your working directory. The details can be saved as a file in your repo.

Richie is a website builder at KodeHauz. He loves Drupal and has launched many websites with it. His hobbies include: swimming, watching movies and TV series, writing, conducting and singing with the choir and, of course, writing codes.