This tutorial compiles list of frequent Git commands for reference purposes.
- Git Connecting to Repo
- Branch Operations
- Git add, commit Command
- Git fetch, merge, and pull
- Git Reset
- Git Clean
- Git Logging
Git Connecting to Repo
Connect Your Local Repo to Server
Following command will connect your local git repo to server. Read More
git remote add origin
Create a new branch
Following command will create a new branch with [branch-name]. Read More
Switch to another branch
Following command will to a branch with [branch-name]. Read More
Create a new branch and switch to a new branch
Following command will create a new branch with [branch-name] and switch to newly created branch as well. Read More
git checkout -b 
Connect a new local branch to upstream branch
Use following command to push a new local branch to its remote counterparts. This will create a new branch on remote and set it as a upstream branch for a local branch.
git push --set-upstream origin
Checkout an existing branch from remote and track it
Use following command to checkout an existing branch from a remote and track it.
git checkout -b --track /
Connect local branch to an existing upstream branch
Use following command to set up upstream branch for a local branch.
git branch --set-upstream-to=origin/
See all branches and its upstream branches
Use branch command in a verbose mode to see the local branch, its current SHA, and upstream branch information.
git branch -vv
Get status of changes in a branch
Following command will show list of changes in current branch. Read More
Delete a branch
Following command will delete a branch locally. Read More
git branch -d
Following command will delete a branch locally and remotely.
git branch -d -r
View all branches
Following command will show all local branches. Read More
Following command will show all remote branches. Read More
git branch -r
Following command will show all local and remote branches. Read More
git branch -a
Following command will show verbose output of branches. Read More
git branch -vv
Git add, commit, push Command
Add Changes to Staging Area
Following command will add files from working directory to staging area. Read More
git add # This will add [file-name] to staging area git add # This will add [directory-name] and all files inside it to staging area git add * # This will add all current changes to staging area
Commit staged changes
Following command will commit all staged changes. Read More
git commit # This will prompt for commit message and commit all files. git commit -m # This will commit all staged changes with given [commit-message]
Stage and Commit Changes
Following command will Stage and Commit all changes.
git commit -a -m # This will stage all changes and commit with given [commit-message]
Push Changes to Remote Branch
Following command will push all commited local branch changes to remote branch. Read More
Git fetch, merge, and pull Command
Fetch latest file changes from upstream branch
Following command will download changes from upstream branch. Read More
Merge changes from upstream branch to local branch
Following command will merge changes on local branch and remote branch together. Run
git fetch command before using
git merge to download latest changes to merge. Read More
Merge specific branch to current working branch
Following command will merge changes of to current working branch.
Visualize Merge Conflicts
Following command will open appropriate mergetool to visualize merge conflicts
Fetch and Merge from upstream branch
git pull is a combination of
git fetch and
git pull downloads changes from remote repository and merge it into local repository. Read More
Following command will show the differences between source branch and target branch.Read More
Git Reset Command
Git reset is very powerful command. If used unappropriately, you can lose all your local changes permanently without being saved. Please read about it in detail at Here. This post explains about git reset in detail.
Soft Reset to Change only HEAD Position
Soft reset will move only HEAD position. Staged file changes (Index area) and Unstaged file changes (Working Directory area) will not be affected.
git reset --soft origin/ # Move HEAD to origin/ reference git reset --soft origin/master # Move HEAD to origin/master reference git reset --soft origin/develop # Move HEAD to origin/develop reference git reset --soft HEAD~ # Move HEAD to one commit backward
Mixed Reset to Update Staged Files as Well
Mixed reset will move HEAD position and overwrite staged file changes (Index area). Unstaged file changes (Working Directory area) will not be affected. Mixed reset is the default option for
git reset command.
git reset --mixed origin/ # Move HEAD to origin/ and overwrite staged files with copy of origin/
Hard Reset to Update Working Directory as Well
Hard reset will move HEAD position, overwrite staged file changes (Index area), and overwrite unstaged file changes (Working Directory area) as well. Hard reset will not save unsaved changes and you will lose any unsaved work.
git reset --hard origin/ # Move HEAD to origin/, overwrite staged and unstaged files with copy of origin/
Git Clean Command
Delete all Untracked files and directories
Git Clean will delete all untracked files and directories from the current working branch.
-f option cleans all files. </br />
-d option cleans all directories.
git clean -f -d
-n if you want to do a dry run and see which files and directories will be deleted. It will not delete them actually.
-n option shows all files/directories to be deleted, but it will not delete it. </br />
git clean -n -f -d
See the Log
See the chane log of files. Read More
git log # see the log of files git log --author=[author-name] # see logs only from specific author git log --pretty=oneline # see compressed log. (Just oneline) git log --graph --oneline --decorate --all # Oneline graph of all commit git log --help # Help on log command
In my day to day development activities, these are the most common git operations involved. Please feel free to comment any other git commands which you are using most frequently in your development.