To communicate with another developer we use code sharing platform like (GitHub, Bitbucket or GitLab). Git commands help us to communicate with those sharing platform easily. But communication needs to be universal. To solve this problem Git used universal command for every developer or sharing platform. So that we developer don’t get panic or learn dozens of command for our daily development work.
In this article, I’m going to show you those basic git command that every developer need to know. This is an essential skill that require every developer.
Table of Contents
- Basic git commands at a glance
- Command: git –version
- Command: git –help
- Command: git config
- Command: git config –global user.name
- Command: git config –global user.email
- Command: git init
- Command: git clone
- Command: git status
- Command: git add
- Command: git commit
- Command: git push
- Command: git pull
- Command: git remote
- Command: git remote add
- Command: git remote rm
- Command: git fetch
- Command: git branch
- Command: git checkout
- Command: git merge
- Command: git diff
- Command: git log
- Command: git reset
- Command: git clean
- Command: git rm
- Command: git show
- Command: git tag
- Command: git cat-file
- Command: git grep
- Command: gitk
- Command: git gc
- Command: git fsck
- Command: git rebase
- Congrats! 🎉🎉🎊You just learn the basic git commands which are used in daily life.
Basic git commands at a glance
|git –version||Print the git suit version|
|git –help||Prints the synopsis and a list of the commonly used commands|
|git config||Prints the synopsis and a list of global options|
|git config –global user.name||Set a username as globally|
|git config –global user.email||Set a email as globally|
|git init||Create a new local repository|
|git clone||Create a local working copy of the repository|
|git add||Add one or more file to staging area|
|git commit||Take staged snapshot and commits it to the project history|
|git push||Send changes to the master branch of remote repository|
|git status||Print the list of changes files|
|git remote -v||Print list of all remote repository|
|git remote add||Add new remote connection|
|git remote rm||Remove a remote connection|
|git remote add origin||Connect local repository to the remote repository|
|git branch||Print the list of all branches in your repo|
|git pull||Update from the remote repository|
|git merge||Merging the branch to the master repo|
|git log||Print the list of all commit history|
|git fetch||Fetching download from a branch of a repo|
|git checkout||Checking out old commits or change the branches|
|git clean||Remove untracked files from the working directory|
|git reset||Reset the local commit|
|git diff||Shows the file differences which are not yet staged.|
|git rm||Remove a file from git|
|git show||Show the metadata of commits|
|git tag||Identify the release version|
|git cat-file||view the type and the size information|
|git grep||search through committed trees, working directory, and staging area for specific phrases and words.|
|gitk||shows the graphical interface for a local repository|
|git gc||clean unnecessary files and optimize the local repository.|
|git fsck||integrity check of the git file system and identifies any corrupted objects|
|git rebase||apply certain changes from one branch to another|
Let’s test it out all those git commands with examples. First, open the git bash/terminal from your pc. You can use any terminal you want. Here I’m using my new windows terminal.
Command: git –version
git --version command allow you to print the current git version. Basically it show the git bash version.
Command: git –help
git --help command allow you to print all the commonly use commands and synopsis.
If you want to know about specific command, you can do that too by using following command.
git --help clone
It will open the help page in your default bowers like this.
Command: git config
git config command allows you to prints the synopsis and a list of local and global options. It can be used for user-specific configuration values like username, email, file format etc.
Command: git config –global user.name
git config --global user.name command set the username globally for commits. This username will be used for all local to remote repo commits. Use your GitHub/Bitbucket/GitLab username for better readability.
git config --global user.name "username" example: git config --global user.name "jhondoe"
Command: git config –global user.email
git config --global user.email command set the email globally for commits. This email will be used for all local to remote repo commits. Use your GitHub/Bitbucket/GitLab email address for better readability.
git config --global user.email "[email protected]" example: git config --global user.email "[email protected]"
Note: If you want to use a username and email for a specific local repo then change --global to --local. This is only applied for that repo only.
Command: git init
git init command creates an empty Git repository – basically a .git directory with subdirectories for objects, refs/heads, refs/tags, and template files. An initial HEAD file that references the HEAD of the master branch is also created. To see this .git folder make user show hidden file option is open. This folder contains the local change of the repo.
#initialize the git git init
Inside the .git folder there are some files which is save the local changes.
Command: git clone
git clone command create a copy of an existing Git repository. Cloning is the most common way for developers to obtain a working copy of a central repository.
git clone [url] #example: git clone [email protected]:kmhmubin/Face-Recognition-Attendance-System.git # use SSH or git clone https://github.com/kmhmubin/Face-Recognition-Attendance-System.git #use HTTPS
There is two option to clone for a git URL, one is using HTTPS options which are commonly used, and the other is SSH method which is password protected and more secure.
Cloning an empty repo from GitHub just copy the URL
Or you can clone an existing repo.
Command: git status
git status command displays the state of the working directory and the staging area. It lets you see which changes have been staged, which haven’t, and which files aren’t being tracked by Git. The status output does not show you any information regarding the committed project history.
Command: git add
git add command allow to add files to staging area. All the change are save in locally in the .git folder.
# add single file git add hello.txt # add multiple files git add .
Command: git commit
git commit command captures a snapshot of the project’s currently staged changes. Committed snapshots can be thought of as “safe” versions of a project. There are a couple of option for commit in git.
git commit -m "type your message"
Always add a meaningful message to your commit. The message will help you to track your work.
This command commits any files you’ve added with the git add command and also commits any files you’ve changed since then.
git commit -a
There is a shortcut command which is allow you to do both of those above command.
git commit -am "type your message" example: git commit -am "Initial Commit"
Command: git push
git push command upload the committed changes of master branch from local repo to your remote repository.
git push [remote] [branch name] example: git push origin master
Here I’m upload all my local file to GitHub master repository.
You can also commit to a specific branch like this.
git commit origin branch2
Command: git pull
git pull command fetches and merges changes on the remote server to your working directory.
git pull or git pull [remote] [branch] or git pull [repo link]
Command: git remote
git remote command lets you create, view, and delete connections to other repositories. Remote connections are more like bookmarks rather than direct links into other repositories.
List the remote connections you have to other repositories.
git remote -v
Same as the above, but include the URL of each connection.
Command: git remote add
To add new remote to an empty repo or existing repo use add command.
git remote add [name] [URL] example: git remote add origin https://github.com/kmhmubin/github-test.git
Command: git remote rm
To remove a remote source from the repo use rm command.
git remote rm [remote name]
$ git remote -v # View current remotes > origin https://github.com/OWNER/REPOSITORY.git (fetch) > origin https://github.com/OWNER/REPOSITORY.git (push) > destination https://github.com/FORKER/REPOSITORY.git (fetch) > destination https://github.com/FORKER/REPOSITORY.git (push) $ git remote rm destination # Remove remote $ git remote -v # Verify it's gone > origin https://github.com/OWNER/REPOSITORY.git (fetch) > origin https://github.com/OWNER/REPOSITORY.git (push)
Command: git fetch
Fetching downloads a branch from another repository, along with all of its associated commits and files. But, it doesn’t try to integrate anything into your local repository. This gives you a chance to inspect changes before merging them with your project.
Fetch all of the branches from the repository. This also downloads all of the required commits and files from the other repository.
git fetch [remote] example: git fetch origin
Same as the above command, but only fetch the specified branch.
git fetch [remote] [branch] example: git fetch origin master
Or you can use a power move which fetches all registered remotes and their branches.
git fetch --all
Command: git branch
A branch represents an independent line of development. Branches serve as an abstraction for the edit/stage/commit process. This command lets you create, list, rename, and delete branches.
git branch or git branch --list
Create a new branch in git.
# creating new branch git branch [branch name] example: git branch new_branch
Delete a branch from repo.
# delete a branch git branch -d [branch name] example: git branch -d new_branch
Command: git checkout
git checkout command lets you navigate between the branches created by
git branch. Checking out a branch updates the files in the working directory to match the version stored in that branch, and it tells Git to record all new commits on that branch.
Switching to new branch
git checkout [branch name] example: git checkout new_beta
Again switching new_beta branch to master branch.
Creating new branch and auto switch to new branch
git checkout -b [branch name] example: git checkout -b checkout_branch
Command: git merge
This command allow to combine the develop branch to master branch.
git merge [branch name] example: git merge new_branch
Here is an example code to crate new feature branch and merge to master branch.
# Start a new feature branch git checkout -b new-feature master # Edit some files git add <file> git commit -m "Finish a new feature" # Go back to master branch git checkout master # Merge in the new-feature branch to master branch git merge new-feature # Delete the feature branch git branch -d new-feature
Command: git diff
This command shows the file differences which are not yet staged. It will show the file in the terminal and show the changes lines.
Command: git log
The Git Log command allows you to view information about previous commits that have occurred in a project.
Display all the git commit history using
git log --all
git log or git log --all
git log --follow [filename] lists version history for a file, including the renaming of files also.
git log --follow [filename] example: git log --follow hello.txt
git log --author [name] command allow you to filter the commit by the specific author.
git log --author [name] example: git log --author kmhmubin
Command: git reset
This command will reset the index and the working directory to the last git commit’s state.
git reset [file name] command upstages the file, but it preserves the file contents.
git reset [file name] example: git reset hello.txt
git reset [commit] command undoes all the commits after the specified commit and preserves the changes locally.
git reset [commit] example: git reset be019e998144e513ce4f1e0fdb083481f1dbf74b
git reset -hard [commit] command discards all history and goes back to the specified commit.
git reset -hard [commit] example: git reset -hard be019e998144e513ce4f1e0fdb083481f1dbf74b
Command: git clean
git clean command remove untracked files from the working tree.
If you want to see which files will be deleted you can use the
-n option before you run the actual command:
git clean -n
Then when you are comfortable because it will delete the files then use the -f option:
git clean -f
To remove directories use
git clean -fd or git clean -f -d
To remove ignored files use
git clean -fX or git clean -f -X
To remove ignored and non-ignored files use
git clean -fx or git clean -f -x
Command: git rm
git rm command deletes the file from your working directory and stages the deletion.
git rm [file name]
Command: git show
git show command shows the metadata and content changes of the specified commit.
git show [commit] example: git show be019e998144e513ce4f1e0fdb083481f1dbf74b
Command: git tag
Tags are ref’s that point to specific points in Git history. They allow you to identify specific release versions of your code. There are two type of tags
- Annotated Tags
- Lightweight Tags
Both of them will allow you to refer to a specific commit in a repository, but they differ in the amount of metadata they can store.
Annotated tags store extra metadata such as author name, release notes, tag-message, and date as full objects in the Git database. All this data is important for a public release of your project.
git tag -a v1.0.0 or # add tag message git tag -a v1.0.0 -m "Releasing version v1.0.0"
Lightweight tags are the simplest way to add a tag to your git repository because they store only the hash of the commit they refer to.
git tag v1.0.0
Command: git cat-file
git cat-file is used to view the type and the size information of a repository object. Use the -p option along with the object’s SHA-1 value to view the information of a specific object.
git cat-file -p [commit SHA-1 value] example: git cat-file -p be019e998144e513ce4f1e0fdb083481f1dbf74b
Command: git grep
git grep lets users search through committed trees, working directory, and staging area for specific phrases and words.
git grep "world"
gitk shows the graphical interface for a local repository.
Command: git gc
git gc will clean unnecessary files and optimize the local repository.
Command: git fsck
git fsck performs an integrity check of the git file system and identifies any corrupted objects.
Command: git rebase
git rebase is used to apply certain changes from one branch to another.
git rebase [branch name] or git rebase master
Congrats! 🎉🎉🎊You just learn the basic git commands which are used in daily life.
Don’t know the basic shell command. Don’t worry. I have a solution for your. Check this out.
If you want to learn more about git command and how it works then read this book. I hope you like it. Please share this article with follow developer.