Git is currently the most popular version control system. This is, among others, because it is fast, distributed, branched, and free. The very idea of working with Git is fairly easy, but the number of commands is quite large, and quite a few of them are rarely used.
Git commands can be divided into several types, depending on what they are for. These can be configuration commands (e.g. config), to make changes to the repository (e.g. commit, push), branching and merging (e.g. checkout, merge) or inspection and comparison (e.g. log, diff).
Git commands types and examples
|command||what it does||syntax & examples|
|init||Creates a new local (empty) repository with the default branch, but without any git commit.||git init|
|clone||Creates a local copy of the remote repository.||git clone <repo_address> <local_directory>|
|bundle||Create a single archive file with all refs needed to restore the repository.||git bundle create <archive_name> –all|
|add||Add changes to the Staging Area.||git add <file_name>|
|commit||Commit changes from the Staging Area.||git commit -m “Message”|
|push||Sends changes to a remote repository (origin) to a given branch.||git push origin <branch_name>|
|fetch||Take changes from the external repo but do not include them in local branches.||git fetch origin|
|pull||It downloads and immediately integrates changes to the local branch.|
pull = fetch + merge
|git pull origin <branch_name>|
Above call is equal to this two below:
git fetch origin <branch_name>git merge origin/<branch_name>
|reset||Reset current HEAD to the specified state (e.g. commit):|
–softdifferences will be preserved in the Staging Area,
–mixedthe differences will be kept in the Working Directory, this is the default scope,
–hardthe: differences will be completely removed.
|git reset –soft <file_name>|
git reset –hard
|revert||Rolls back the changes by creating a new commit that is the exact opposite of the one being reverted. Safe operation – will not spoil the commit history|
reverts the last commit.
|git revert <commit_SHA>|
git revert HEAD~1
|branch||Basic operations on branches.|
– showing the list of all branches,
– creating a new branch,
– deleting the specific branch.
git branch <branch_name>
git branch -d <branch_name>
|checkout||Switches to the given branch.|
Creates a new branch and switches to it immediately.
|git checkout <branch_name>|
git checkout -b <branch_name>
|remote||Manage a set of tracked repositories.|
Set remote server address (e.g. after init operation).
Displays a list of currently set connections.
git remote add origin <repository_address>
git remote -v
|merge||Concatenates changes from the given branch to the currently active branch. Possible merge conflicts.||git merge <branch_name>|
|rebase||Reapply commits on top of the current and updated branch. The current branch is re-based.|
Flatten the history, avoiding merge-commits.
|git rebase <branch_name>|
|status||Shows the current status of the Working Directory and Staging Area.||git status|
|log||It shows the commit list with some details depending on the parameters provided.|
Prints log for the last 3 commits.
Prints log for author’s commits, not older than 1 week.
git log -3
git log –author=”John Doe” –after=”1 week ago”
|diff||Show changes between two items:|
– commits difference
– branches difference
git diff <SHA_commit1> <SHA_commit2>
git diff <branch1_name> <branch2_name>
|config||Allows for basic configuration, such as username, email address or default text editor, etc.|
Deletion of a given config.
Be aware that there are 3 levels of configuration: local, global, system.
|git config user.name “John Doe”|
git config –local user.email “firstname.lastname@example.org”
git config –unset <option_name>
A very important feature of git commands is the fact that virtually every command can be parameterized, i.e. give it an appropriate option (or flag) that will change the operation of a given function, e.g.:
default call with default information
git status –short
will show the output in short format (–long is the opposite parameter)
git status -uno
this will show no untracked files
Detailed descriptions of all options and how to use them are available in the official Git documentation.