Common Git Commands
Home » BLOG » Web development » Common Git Commands

Common Git Commands

category:  Web development

Today it was a rainy day. I was cleaning my repositories on GitHub and bitbucket. So it is a good chance that I can share the common git commands here.

git config

It is a git configuration value on a global project level. Executing git config will modify a configuration .gitconfig text file. The command below sets the author name and email address in order to use with your commits.

git config --global user.name "yourusername"
git config --global user.email "youremail@example.com"

git init

The command is used to start a new repository. What I normally do, I navigate to the project folder that I will work on and this folder will be the repository for my project. Then I run the command below. Without git init, you can not use any git commands.

git init

git clone

This command is used to obtain a repository from an existing URL. As a freelancer, often I have to clone the existing repository to my computer. The existing repository can be from the local or remote repository. The remote repository can be SSH or HTTPS.

git clone <repo>

git add

The command adds a file, all files inside a folder, or all changed files to the staging area.

git add <file>
or 
git add <directory>
or
git add .

git commit

The command records or snapshots the file permanently into the version history. Of cause, you can revert the commit. But sometimes, you may face conflict commits with other developers, and to revert your commit may cause time and headache. Normally, I check the changes in the staging area for making sure the changes are correct as I expect. Then I commit the changes. Important, you must add the comment on each commit. This way, you or a team know what did you do on that commit.



git commit -m "yourcomment"

git diff

The command shows the file differences which are not yet staged.

git diff

Also, the command below shows the differences between the two branches.

git diff <first branch> <second branch>

git reset

The command unstaged the file but it preserves the file contents.

git reset <file>

The command undoes all the commits after the specified commit and preserves the changes locally.

git reset <specified commit>

git status

The command lets you see which changes have been staged, which haven’t and which files aren’t being tracked by git.

git status

git log

The command shows you any information regarding the committed project history.

git log
git log --oneline

The --oneline option is useful for getting a high-level overview of the project history.

git branch

The command lists all the branches in the current repository.

git branch

Create a new branch

git branch <your_branchname>

Delete a branch

git branch -d <your_branchname>

git checkout

Switch from a current branch to another one.

git checkout <your_branchname>

Create a new branch and switch to it.

git checkout -b <new-branch>

git merge

Merge the specified branch’s history into the current branch.

git merge <your_branchname>

git remote

Connect your local repository to the remote server

git remote add origin <remote repo>

List the remote connections you have to the repositories including the URL of each connection.

git remote -v

git push

Send the committed changes of your current branch to the remote repository.

git push origin <your_remote_branch>

git pull

Fetches and merges changes from the remote server to your working project folder.

git pull

And that is the git commands that I normally use. You can find more git commands and options at this Git tutorial.

Tip: If you are using Windows or Mac, you can install Sourcetree or Github Desktop which are the git GUI. I use both and they are great tools.