This tutorial compiles list of frequent Git commands for reference purposes.

Content

  1. Git Connecting to Repo
  2. Branch Operations
  3. Git add, commit Command
  4. Git fetch, merge, and pull
  5. Git Reset
  6. Git Clean
  7. 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 [server]
				
			

Branch Operations

Create a new branch

Following command will create a new branch with [branch-name]. Read More

				
					git branch [branch-name]
				
			

Switch to another branch

Following command will to a branch with [branch-name]. Read More

				
					git checkout [branch-name]
				
			

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 [branch-name]
				
			

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 [local-branch-name]
				
			

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/[existing-upstream-branch] [local-branch-name]
				
			

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

				
					git status
				
			

Delete a branch

Following command will delete a [branch-name] branch locally. Read More

				
					git branch -d [branch-name]
				
			

Following command will delete a [branch-name] branch locally and remotely.

				
					git branch -d -r [branch-name]
				
			

View all branches

Following command will show all local branches. Read More

				
					git branch
				
			

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
				
			

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 [file-name] 	 # This will add [file-name] to staging area
					git add [directory-name] # 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 [commit-message] # 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 [commit-message] # 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 push
				
			

Git fetch, merge, and pull Command

Fetch latest file changes from upstream branch

Following command will download changes from upstream branch. Read More

				
					git fetch
				
			

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

				
					git merge
				
			

Merge specific branch to current working branch

Following command will merge changes of [branch-name-to-merge] to current working branch.

				
					git merge [branch-name-to-merge]
				
			

Visualize Merge Conflicts

Following command will open appropriate mergetool to visualize merge conflicts

				
					git mergetool
				
			

Fetch and Merge from upstream branch

git pull is a combination of git fetch and git merge. git pull downloads changes from remote repository and merge it into local repository. Read More

				
					git pull
				
			

See difference

Following command will show the differences between source branch and target branch.Read More

				
					git diff [source-branch] [target-branch]
				
			

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/[branch-name]   # Move HEAD to origin/[branch-name] 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/master   # Move HEAD to origin/master and overwrite staged files with copy of origin/master
				
			

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/master   # Move HEAD to origin/master, overwrite staged and unstaged files with copy of origin/master
				
			

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.

-d option cleans all directories.

				
					git clean -f -d
				
			

Use -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.

				
					git clean -n -f -d
				
			

Git Logging

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
				
			

Conclusion

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.

References

Git-SCM
Git-Guide