Git is a distributed version control system that aid developers working on a project to collaborate seamlessly while also having edge of being distributed so a repository can have multiple remote repository for easy collaboration among multiple teams
Git (/ɡɪt/)[8] is a distributed version control system: tracking changes in any set of files, usually used for coordinating work among programmers collaboratively developing source code during software development. Its goals include speed, data integrity, and support for distributed, non-linear workflows (thousands of parallel branches running on different systems).
- Source: WikiPedia
Linus Torvalds, the developer of the Linux kernel, built Git in 2005 to help control the Linux kernel’s development work.
Git, being distributed version control systems allows a developer to have or collaborate to multiple remote repositories and have a full copy/backup of code locally, as local repository in git also works as repository.
Git has many commands to aid developers in their daily workflow. However here I am listing 50 such commands that one have to use often.
How to initialize a Git repository
If one wish to convert any folder into a git repository on your local machine, this is the command you are looking for. Traverse into folder you wish to convert into Git repository and run
git init .
One can also use this command with absolute of relative path to folder without traversing to that folder; just replace .
with <path_to_folder>
How to add a remote repository to local repository
Now since in step 1, we have initialized a local git repository, now we need to add remote repository to push our changes to (so other developers can also collaborate). For this first run command below to list remote repositories
git remote -v
Command above will list all remote repositories; but in our case it will list none; since there is no remote repositories added as yet. To add a remote repository; run following command
git remote add origin <path_to_remote_repository>
path_to_remote_repository
should a valid git repo path that can be accessed.
NOTE origin
can also have any other name as this would be the name/shorthand for remote repository (i.e aws_repo
)
How to get information abot remote repository in Git
To get to know about a remote branch, one can use following command
git remote show <origin>
Where origin
is replaced with remote repository shorthand as added to get information about it
How to clone an existing Git repository
This step is used only when you already have an existing remote repository and wish to collaborate to that repository. So instead of initializing a blank repository; one can clone an existing repository. To do this run following command
git clone <repository_url> <folder_name>
folder_name: this is optional, it takes remote repository name when not provided
repository_url: this is remote repository url (endpoint)
How to commit add/update files to staging area
Now, since we have a working local git repository; we make some changes and then need to add those changed/new files to git repository. To do this one can run following command
git add <relative_path_to_file>
relative_path_to_file: this is path to new/updated file one wish to add/update in Git tree. This path should be relative to local git repository folder.
Note files added/updated using git add
has not yet been committed in local repository yet.
To add, all files in git tree one can use
git add .
To add files starting with model_
one can use
git add model_*
How to selectively stage changes for a file in Git
There are times, when all changes made in a file are not to be staged and committed (i.e debug code and logging). To only select what needs to be staged in a file one shall add a file using -p
flag like following
git add -p <FILENAME>
How to delete tracked file(s) to staging area
To delete a file from git tree, one can use following command
git rm <relative_path_to_file>
Note to be able to use this command, file(s) concerned should already be part of git tree
How to check status of files in staging area
One may wish to check status of current status of local git repository by running following command
git status
This command will provide statuses as untracked
, staged
, unstaged
files. These statuses denotes following
staged
: Staged files are files that are updated and added to staged area but yet to be committedunstaged
: Unstaged files are files that are updated but not added to staged area.untracked
: Untracked files are files that are newly added or have never been added staged area
How to commit file(s) to local git repository
Once files; that are staged, are ready to be committed to local git repository. To make a commit; one can use following commands
git commit -m "<YOUR_MESSAGE_HERE>"
<YOUR_MESSAGE_HERE>
: Provide a meaningful message that conveys what has been done in this commit in single line
How to commit file(s) to local git repository with multi-line message
To make a commit with multiline meaningful message one can use following command
git commit
This would open a git editor (as chosen in git config) and one can provide multiline commit message
How to change commit message of last commit
You can change commit message to tracked files with a single command by using the -a and -m options.
git commit -a -m "<CHANGED_COMMIT_MESSAGE_HERE>"
How to see log of commit history
To see log of commit history, one can run following command in local git repository
git log
This would list all commit logs; local and remote.
How to see log commit of a particular author
To see log of commit history made by particular author, one shall run following command in local git repository
git log --author=<name_or_email_of_author>
How to see log stats in Git
This command will cause the Git log to show some statistics about the changes made in each commit, including line(s) changed and file names.
git log --stat
How to show commit log as a graph in Git
We can use –graph to get the commit log to show as a graph. Also, –oneline will limit commit messages to a single line.
git log --graph --oneline
How to show commit log as a graph of all branches in Git
Does the same as the command above, but for all branches.
git log --graph --oneline --all
How to check the commits log of a remote repository in Git
Following command only list commit made and pushed to remote repository
git log <remote>/<branch>
Where remote
is remote repository shorthand and branch
is branch name for which logs are sought
How to see a specific commit in Git
To see a specific commit and changes made therein, one shall use following command
git show <GIT_SHA>
GIT_SHA: Git SHA is SHA code for commit. This can be obtained from git log
. One can use just first five letters of SHA code; no need to use entire SHA code
How to see changes made before staging/committing file(s)
This will only work on changes that are yet to be staged. To see changes made in a file, one shall use following command
git diff
This would show chnages in all unstaged file(s), one after another. To see changes made to specific file, one shall provide relative path to that file, like
git diff some_controller.rb
Note: To see diff of staged file one can use --staged
flag
How to pull code from remote repositor(y)(ies)
Before pushing one’s changes to remote; one need to pull from remote. If remote repository has new commits then they will get merged into one’s code. This is a good practice to always pull first before pushing one’s code to remote repository
git pull <ORIGIN> <BRANCH_NAME>
ORIGIN: This is remote repository reference/shorthand we added with git remote add
, by default it is origin
.
BRANCH_NAME: This is branch name we wish to push our commits to. In most cases it is same branch as we are working on.
NOTE: This command is a combination of two commands fetch
and merge
, that we will talk about next sections
How to fetch code from remote repositor(y)(ies)
As mentioned above git pull
is a combination of two commands fetch
and merge
. But git allows us to do it individually.
git fetch
command fetches commits from remote but does not merge it with local branches. For example
git fetch
This command would fetch changes made in remote repository against all branches (new/existing). But this would not merge it. To fetch changes made in particular branch use following command
git fetch <origin> <branch>
Where origin
is remote origin shorthand and branch
is branch name one wish to fetch
How to merge code from remote repositor(y)(ies)
Once we have fetched changes/commits from remote repository. one can simply run following command to merge it
git merge <origin>/<branch>
Where origin
is remote origin and branch
is remote branch one wish to merge into current working branch
How to push changes to remote repository(ies)
Once we have pulled from remote repository; we can now push our changes to remote by running following command
git push <origin> <branch>
Where origin
is remote shorthand (use git remote -v
to know) and branch
is branch one wish to push to
How to undo changes made to an unstaged file(s)
If one have made changes to a file and file has not been added to staged area yet. One can simply checkout
that file to undo changes made
git checkout <filename>
How to unstage staged file(s)
If one has mistakenly added a file to staged area using git add
then it can be again moved to unstaged area by following command
git reset HEAD <filename>
One can also unstage all files staged by following command
git reset HEAD
For more advanced users, one can unstage some chages in particular file using -p
flag
git reset HEAD -p <filename>
How to change to another existing branch in Git
To change to an existing branch from current branch, one can run following command
git checkout <branch>
Where branch
is branch name; one wish to switch to. If this fails because there are conflicting changes then stash
or checkout
changes (read in relevant section(s))
How to change/checkout to new branch in Git
As a standard Git workflow; it is best practice to create a feature branch to work on new thing; instead of directly working on active development branch. To do this one can create a feature_branch to work into. One shall run following command to create a new branch from existing development branch
git checkout -b <branch>
Where branch
is branch name you wish to give to a feature branch.
NOTE Make sure that you are checking out from active development branch to have all commits of that branch
How to list branches in local repository
To list branches available in local Git repository; one shall run following command
git branch
This shall list all local branches available, with current branch marked with *
How to list branches in remote repository
To list branches available in remote Git repository; one shall run following command
git branch -r
This shall list all remote branches available, prefixed with remote shorthand. i.e for remote named origin
one may see origin/master
, origin/development
etc
How to create a new branch in Git
To create a new branch spwaning from current branch, one shall run
git branch <branch_name>
branch_name: branch_name
is name to be given to new branch.
How to delete a local branch in Git
To delete a branch locally, one shall use following command
git branch -d <branch_name>
NOTE: Note that this command shall only delete branch from local repository. To delete this from remote; there are different procedure for server hosting git server. For example on Github, a branch can be deleted by using there UI interface, or pushing a branch prefixed with :
// Only applies on github
git push origin :<branch_name>
How to ignore files in Git
Sometimes developers have files in local machine, that are not to be committed because of several reasons (machine specific config, environment specific config etc). Hence we would like not to commit them.
To avoid them adding and committing accidentally, one shall add those files (with relative path) to .gitignore
file.
NOTE: Files path added to .gitignore
should be untracked files, as it would only ignore untracked files
How to amend the most recent commit in Git
Assume that as a developer you wish to change commit message for recent commit (that is not pushed yet). Git allows us to use --amend
flag to rectify commit messages.
git commit --amend
Note: Do not amend commit message for commit that has already been pushed to any remote.
How to rename files in Git
To change a file’s name in git, one shall use mv
command as in Linux
git mv <OLD_FILENAME> <NEW_FILENAME>
How to revert a recent commit in Git
There are times when we need to revert commits made to git. The most simple solution is to use a revert
command. This commands creates a new commit that undo changes made in specific commit. To revert most recent commit in git, one shall use
git revert HEAD
How to revert a specific commit in Git
To revert a specific commit in git log. One shall get commit SHA id using git log
and then use
git revert <SHA-commit-id>
Note: One just need first five characters of SHA commit id
How to merge two branches in Git
There are times when one need to merge feature branch into active development branch, to do that one shall change branch to one going to merge into
git checkout <branch_to_merge_to>
Where branch_to_merge_to
is branch one need to checkout to. After this one shall run following command
git merge <branch_to_be_merged>
Where branch_to_be_merged
is feature or other branch one wish to merge with
How to abort a merge in case of unresolved conflicts
There maybe times when while merging conflict occurs that can be resolved manually. One shall abort merge by simply using --abort
flag
git merge --abort
How to do a squashed merge in Git
There is a clean way of merging a feature branch into active development branch by bundling all commit made in feature branch into a single commit. To do this one shall use
git merge --squash <branch_to_be_merged>
This would open a commit message editor listing all commit merged together. One can give some meaningful commit message for squashed merge
NOTE: In case of several merge from active development branch to feature branch, squshed merge commit can bundle several other commit messages not belonging to feature branch hence leading to confusion.
How to cherry-pick a specific commit to be merged
There are times, when we only need to merge selective commits from feature branch to active development branch or otherwise. To do this one shall run
git cherry-pick <COMMIT_SHA_ID>
Where COMMIT_SHA_ID
is SHA id for that specific commit.
NOTE: One need to first checkout
to branch one going to cherry-pick
into
How to see commit difference between two branches in Git
There are times when one need to check what commits have been merged
and what commits are still to be merged
. Or simply change of commits between two branches. To do this one shall use
git cherry <BRANCH_NAME>
Where BRANCH_NAME
is name of branch with which one is comparing current branch
How to rebase feature branch with development branch
We have discussed about issue(s) while explaining merge
. When a feature takes more time to develop, there are chances that active development branch goes ways ahead of feature branch. Merging creates unnecessary merge commits and conflicts. To solve this issue one can use rebase
but with CAUTION.
In simplest terms rebase
would remove local feature changes, apply changes from rebase branch and then overlay local feature changes. This way it creates clean commit without overhead merge commits. To do this one shall run following command in feature branch
git rebase <active_development_branch>
CAUTION: To do this one need to make sure that feature branch is not actively worked upon by other developers as it would reset git log history. Once done feature branch can be cleanly merged into other branch and be deleted
CAUTION Before using this, one is advised to read documentation
How to rebase in interactive mode in Git
One can run git rebase
interactively using the -i flag. It will open the editor and present a set of commands you can use for rebasing
git rebase -i <active_development_branch>
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
How to force push in Git
To force push a branch one shall do following
git push <origin> <branch>
This command force pushes a branch to remote (specified).
CAUTION: Before force push, make sure that no other collaborator is working on branch being force pushed as it would change log history.
How to check local Git configuration:
This command below returns a list of information about your git configuration on local machine
git config -l
How to setup Git username in config
With the command below you can configure your user name in local machine
git config --global user.name "Pankaj B"
How to setup Git user email
This command below; lets you setup the user email address to be used in commits made from this machine
git config --global user.email "someuser@example.com"
How to cache login credentials in Git
One can store login credentials in the cache to avoid typing them each time. To do this one shall use
git config --global credential.helper cache
How to setup aliases in local Git
Git allows one to setup aliases for git commands to make shorthand for. One can setup them in local .gitconfig
file. However one can also use mine.
These aliases can be found here
One can add there own and edit existing aliases to make it more confortable to work with
How to use stash effectively in local Git
Git provides stash
commands to organise local changes while hoping from one branch to another. Git stash is a mechanism that would put unstaged local changes into a stash (works in FIFO mode or as a stack). One can read a post here. To stash local unstaged changes one shall use
git stash
To bring those changes back into current branchs, one shall use
git stash apply
apply
will apply back changes but will not remove stash in question. To apply
and remove
latest stash, one shall use
git stash pop
To see list of all stash, one shall use
git stash list
How to use named stash effectively in local Git
Git allows stash to be named. To name a stash one shall use
git stash save "<STASH_NAME_HERE>"
To apply named stash, one shall use
git stash apply stash@{n}
Conclusion
These commands are there to aid and improve productivity of individual developer. With practice these commands can come handy without actually remembering them all. One can refer this page again and again to practice these commands in daily development workflow.
Credits
Image Credits:
About The Author
I am Pankaj Baagwan, a System Design Architect. A Computer Scientist by heart, process enthusiast, and open source author/contributor/writer. Advocates Karma. Love working with cutting edge, fascinating, open source technologies.
To consult Pankaj Bagwan on System Design, Cyber Security and Application Development, SEO and SMO, please reach out at me[at]bagwanpankaj[dot]com
For promotion/advertisement of your services and products on this blog, please reach out at me[at]bagwanpankaj[dot]com
Stay tuned <3. Signing off for RAAM