Common Git Commands
- git config
- git init
- git clone
- git add
- git commit
- git diff
- git reset
- git status
- git log
- git branch
- git checkout
- git merge
- git remote
- git push
It is a git configuration values 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 "email@example.com"
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.
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>
The command adds a file, all files inside a folder or all changes files to the staging area.
git add <file> or git add <directory> or git add .
The command records or snapshots the file permanently into the version history. Of cause, you can revert the commit. But sometimes, you may face the 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”
The command shows the file differences which are not yet staged.
Also, the command below shows the differences between the two branches.
git diff <first branch> <second branch>
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>
The command lets you see which changes have been staged, which haven’t and which files aren’t being tracked by git.
The command shows you any information regarding the committed project history.
git log git log --oneline
--oneline option is useful for getting a high-level overview of the project history.
The command lists all the branches in the current repository.
Create a new branch
git branch <your_branchname>
Delete a branch
git branch -d <your_branchname>
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>
Merge the specified branch’s history into the current branch.
git merge <your_branchname>
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
Send the committed changes of your current branch to the remote repository.
git push origin <your_remote_branch>
Fetches and merges changes from the remote server to your working project folder.
And that is the git commands that I normally use. You can find more git commands and options at this link.