In the last post, we discussed git internals to understand Git. In this post we will discuss Git commands, to use Git. I recommend reading the last post, to easily understand the terminologies used here.

I’m going to skip the commands to install git as it varies based on the operating system and y. Once you have our machine architecture. After installation following are the commonly used commands.
1. Check Git Version
git —-version
check and confirm the installation
2. Configure Git
You can use the below command to set global configurations for Git.
git config --global key “value”
.
By default, Git doesn’t allow you to commit until it knows who you are. So the first thing to do after installation is, to set the below properties. Preferably globally
git config --global user.name “My Name”
git config --global user.email “myemail@email.com”
NOTE. If your
present working directory
is inside a local git repository, you can omit the—-global
option from the above command and set up a project-specific configuration.
3. Create a local repo
To create a git repo on your machine, you can either “clone” an already existing remote repository or you can create a fresh local repository.
git init
— Converts the current folder into a local git repo.git clone git@github:user1/repo1.git
— Brings a repo from the remote server to the local machine.
4. Work with branches
The branch is just a pointer to a commit, usually at the head of the tree. Any commit you make is made on the branch you’re currently “checked out” to.
Here are a few commands to work with branches
git status
— Show which branch you are on, (and some more info)git branch
— Show local branchesgit branch -a
— Show both local and remote branches
git branch branch-name
— create a local branchgit checkout -b branch-name
— create a new branch and switch to it.
git checkout branch-name
—Switch to an existing branch git switch branch-name
—similar (not same) as above
git branch -d branch-name
— delete a local branchgit push origin --delete branch-name
— delete a remote branch. Might fail in the absence of required privileges.
git merge source-branch-name
— merge the commits from the source branch into the current branch.
4: Add new content
Following are the commands to manage and add content to the repository
git status
Display the status of the working tree, staging area, and untracked files.
git add <pathspec>
Move the files matching the given pathspec, from the working tree to Staging Area.
git commit -m "<commit message>”
Move the code from the staging area to the repo-tree
Some examples for git add
are
git add . # all content of the current directory
git add ui/ # content from ui/ directory
git add MyScript.js # Only MyScript.js file
NOTE: pathspec is a lot more powerful than the above 3 simple examples. For the sake of the size of this post, I’ll cover it in detail in a separate post.
5. Remove / Undo content
git rm <pathspec>
Remove the file from the staging area & working tree.
git revert <commit-hash>
It doesn’t actually remove anything. When applied on a clean working tree, The revert
creates a new commit in the repo tree. This commit has the code to invert changes from the given commit-hash
git commit --amend
Adds the currently staged changes to the previous commit
git reset --<option> <pointer>
common option values - soft | hard | mixed(default)
pointer values - commit-hash | HEAD
Commands update all 3 trees as follows
- Repo tree- The HEAD pointer of the current branch is moved to the given commit-hash. No change If there is no commit hash.
- staging area- effects of
reset
depend upon the used optionhard
— the changes are lost.mixed
— the current content is moved to the working tree.soft
— no change. - working tree- effects of
reset
depends upon the used optionhard
— the changes are lost.mixed
— the changes from staging are added back, and any file which was created after a given commit-hash stays in a tree but becomes untrackedsoft
— no change.
5. Observe the content & repo
Following are the commands which do not affect data in any tree.
They are used only for observation or analysis
git status
Display the changes in the working tree, staging area, and untracked files.
git log
Show details from commit logs, there are many options to use this command the simplest is as above
git show <commit-hash>
Shows the changes done in a commit
git blame <file name>
Shows the author and time information for each line.
git diff
There are many variations for diff
also. Here are some commonly used ones
git diff
— Difference between the working tree & repo-tree.
for the file present in both working-tree & staged areas, shows the difference between working and staginggit diff --cached
— Difference between the staging area & repo-tree.git diff HEAD
— Difference between (staging area+working-tree) & repo-tree. for the file present in both working-tree & staged areas, shows the difference between working and repo-treegit diff <commit-hash-1> <commit-hash-2>
Difference between the two given commits
6. Working with remote
The repository usually stays on the local machine as well as remote servers. Here are a few commands to manage them
git remote -v
— shows the remote repo associated with this local repo.git remote add origin <URL>
— set a remote repo for fetch
& push
git fetch
— download objects and refs from the remote repo. Does not merge them in the local repo tree git pull
— run git fetch
followed by a git merge
from origin to local branch.
git push
— push object and refs from local to the remote repo.git push origin HEAD
push the current HEAD
, which is the currently checked out branch, to the origin
, which is the remote/ref
. git push -u origin HEAD
This is good (but not limited) to newly created local-only branches. With the -u
option git set the tracking information for the current local branch.
Before I explain the last command in the “working with remote” section, let's look in detail at some commands we already talked about above.
git checkout <remote-branch-name>
When we checkout first time, a local branch with the name same as remote-branch-name
is created and its upstream is set to origin/remote-branch-name
.
git push -u origin HEAD
When we push
with -u
, a remote branch with a name same as local-branch-name is created and upstream is set as origin/local-branch-name
In all other cases, our current branch does not have tracking information, which can be set with the below command
git branch --set-upstream-to=origin/<remote-branch> <local-branch>
—sets a remote counterpart to run fetch
& push
on a given local-branch.
7. Stash
Git stash doesn’t fit anywhere in our 3 working trees, it’s a handy tool provided by git to stash away the changes that can be used later.
git stash
Stash changes available in the working tree into a directory away from 3 trees.
git stash pop
Apply stashed changes to the working tree and drop the stash
git stash apply
Apply the stashed change to the working tree.
git stash list
Show all the stashes
git stash drop
Drop the last stash
git stash clear
Drop all stashes
git stash show
show the changes in the last taken stash
NOTE — Once you stash, changes get added to the stash data structure at the top. so
stash@{0}
always points to the last stash. You can rungit stash
apply, drop, and show
commands withstash@{x}
parameter. where x is the index starting from 0 for the latest stash.
I intentionally did not cover rebase
because I’ve had many experiences of developers using it incorrectly with force push and causing trouble for everyone else while merging. I would suggest avoiding rebase
until you know what you are doing.
This is all as far as a quick cookbook is concerned. I barely scratched the surface here. but whatever is discussed above, is more than enough for everyday use.
However, every command has a plethora of options, Let me know in the comments if you want me to cover any of the above commands in detail. Thanks for reading.
Comments