28 Important Git Functionalities Explained

28 Important Git Functionalities Explained

(Updated on 05/31/2017)

 

Git is a powerful tool, but chances are you’ll find yourself lost unless you know what command accomplishes what you intend to do.

To try and avoid this frustration, I’ve decided review some of the most common situations found in version control and pack their command solution together in a simple table for easy access!

 

 

What’s This Post About

Today I’ll simply list and explain commands that you would use in different scenarios.

For a more detailed use of Git, check my last post, Git Overview.

How This Guide Works

The presentation is quite straightforward: below, you’ll find a table with all commands I’ll be writing about in this post. Every command breakdown will feature a link you can click to move back to the table for navigational purposes.

Also, as some commands are presented more than once, check the Description column for a quick explanation of what each variant is used for. The goal here is to have a functionality guideline, not a double for their manual pages.

Index

You can rearrange the table at your convenience — use SHIFT to organize subsequent columns.

Command Group Description
init Initialization Create an empty Git project
clone Initialization Download an entire Git project from a repository
fetch Repository Related Download updates to a Git project
merge Repository Related Join the downloaded update with a local branch
remote -v Repository Related List the Git project’s repositories (remotes)
remote update Repository Related Update the main repository group (check for new versions)
pull Repository Related Fetch from the repository and integrate a local branch
push Repository Related Send a branch up to the repository
status Staging Area Related Inspect the contents of the working directory and staging area
add Staging Area Related Adds files from the working directory to the staging area
diff Staging Area Related Show differences between the working directory and the staging area
diff HEAD file_name Staging Area Related Show what changes were made to a specific file
checkout HEAD file_name Staging Area Related Remove a file from the staging area and undo any modifications
reset file_name Staging Area Related Remove file from the staging area, preserving its modifications
commit Staging Area Related Record changes previously added to the staging area
help General Commands Show any command’s help page
log General Commands Show commit logs (by date in descending order)
show General Commands Show the HEAD commit’s log and all the file changes that were committed
reset ‑‑mode General Commands Remove all files from the staging area. Mode determines how modified files will be handled
diff commit1 commit2 General Commands Show changes between commits
reset commit General Commands Reset current HEAD to the specified commit
branch Branch Related List all branches
branch -d Branch Related Delete branches
branch new_branch Branch Related Create a new branch
fetch origin branch_name Branch Related Download a new branch that was missing or update an existing one
checkout branch_name Branch Related Switch between branches
diff branch_1 .. branch_2 Branch Related Compare two branches
merge branch_name Branch Related Join two or more branches together

For an explanation on how to rename files and preserve a coherent Git history, check this topic from my last post.

General Commands

help

What It Does:

Show any command’s help page.

When To Use:

Useful when you need more information on Git commands.

How To Use:

Arguments:

1. command: The command you want to know more about.

see all commands

log

What It Does:

Show commit logs (by date in descending order).

For each commit, the output will display its name (a SHA-1 checksum), author, date of creation and the message used by the author to describe it.

When To Use:

This command is useful for, but not restricted to, the following cases:

1. You want to browse the branch, folder, or file history;
2. You want to track  when and by whom a change was made;
3. You need to know a commit’s checksum for another command.

How To Use:

Arguments:

1. -n: This argument limits the number of commits to show;
2. file-or-folder: If you want to know what commits modified a specific file or folder, this is the command to issue.

see all commands

show

What It Does:

Show the HEAD commit’s log and all the file changes that were committed.

When To Use:

When you need a quick way to revise what changes were made in the last commit.

How To Use:

see all commands

reset ‑‑mode

What It Does:

Remove all files from the staging area. Mode determines how modified files will be handled.

When To Use:

Four possibilities apply here:

1. When you want to restore all project files to how they were in the HEAD commit;
2. When you want to restore all files that were in the staging area (and only them);
3. When you want to remove all files from the staging area without restoring any;
4. When you want to remove all files from the staging area and update the project files that don’t have local changes.

How To Use:

Arguments:

All options clear the stating area. Besides that,  --mode should be one of the following modes:

1. --hard: Overwrites all modified files in the working directory to reflect the HEAD commit;
2. --merge: Overwrites all files that were added to the staging area, but don’t touch those not staged for commit;
3. --mixed: Keep all modified files intact;
4. --keep: Acts as a merge when an update is needed, but stops before overwriting local modifications.

see all commands

diff commit1 commit2

What It Does:

Show changes between two commits.

When To Use:

When you need to compare versions or see what modifications were made between two commits.

How To Use:

Arguments:

1. commit_1_sha and  commit_2_sha: These are the desired commits SHA-1 checksum (its name). To get a commit’s name, use  git log.

see all commands

reset commit

What It Does:

Reset current HEAD to the specified commit.

When To Use:

In a situation where you need to go back in time to work on a previous version of the current branch.

How To Use:

Arguments:

1. commit_sha: The desired commit SHA-1 checksum. Use  git log to get it.

see all commands

Initialization

init

What It Does:

Create an empty Git project.

You will need to add every file you want to track. For a detailed step-by-step on this process, check Git Overview.

When To Use:

When you are creating a local Git project.

How To Use:

see all commands

clone

What It Does:

Download an entire Git project from a repository.

This command will create a new directory in the current folder unless otherwise noted.

When To Use:

When you don’t have a local copy of a Git project.

How To Use:

Arguments:

1. remote_location: The Git repository URL address;
2. clone_name (optional): The name/location of the folder to download the project to.

see all commands

Repository Related

fetch

What It Does:

Download updates to a Git project.

This command doesn’t automatically incorporate changes, though: this is done with git merge.

When To Use:

When you want to update a project’s history and branches.

How To Use:

see all commands

merge

What It Does:

Join the downloaded update with a local branch.

When To Use:

It’s usually issued after git fetch to incorporate changes that were made to the repository into the local project.

How To Use:

see all commands

remote -v

What It Does:

List the Git project’s repositories (remotes).

When To Use:

Whenever you need to check the addresses Git uses for fetch and push purposes.

How To Use:

see all commands

remote-update

What It Does:

Update the main repository group (check for new versions).

This is a two-part process:

1. Update the remote references;
2. Check for the current branch status / Check for new branches.

When To Use:

In case you want to check the repository for changes that need to be applied, or to check if new branches were added.

How To Use:

see all commands

pull

What It Does:

Fetch from the repository and integrate a local branch.

When To Use:

When you want to automatically fetch and merge any changes made to the repository.

How To Use:

see all commands

push

What It Does:

Send a branch up to the repository.

It’s a good practice to always merge recent changes to your local project before issuing this command.

Also, this is the command used when you need to send a new branch to the remote.

When To Use:

Whenever you want to send a branch up to the remote (referenced by origin) so it can be reviewed and merged into the remote’s master branch.

How To Use:

Arguments:

1. local_branch_name: The branch you will update in the repository.

see all commands

Staging Area Related

status

What It Does:

Inspect the contents of the working directory and staging area.

When To Use:

Whenever you want to:

1. Check which files are staged for commit;
2. Check which files have local changes and aren’t currently inside the staging area;
3. Check where your local project is ahead, behind or diverges from the remote.

How To Use:

see all commands

add

What It Does:

Adds files from the working directory to the staging area.

When To Use:

This command includes the files that will be included in the next commit.

How To Use:

Arguments:

1. files/folders: A list of files and/or folders to add to the staging area.

see all commands

diff

What It Does:

Show differences between the working directory and the staging area.

This command does not show differences between files and the HEAD commit after they have been added to the staging area.

When To Use:

When you need to see the actual differences in files not listed in the staging area.

How To Use:

see all commands

diff HEAD file_name

What It Does:

Show what changes were made to a specific file.

This command works regardless of the fact that the file is staged for commit or not.

When To Use:

Every time you need to revise what modifications were made to a file.

How To Use:

Arguments:

1. file_name: The file you want to examine.

see all commands

checkout HEAD file_name

What It Does:

Remove a file from the staging area and undo any modifications.

When To Use:

When you want to restore files and remove them from the staging area.

How To Use:

Arguments:

1. file_name: The file that will be overwritten to its previous state.

see all commands

reset file_name

What It Does:

Remove file from the staging area, preserving its modifications.

When To Use:

When you want to keep modifications while unstaging files from the staging area.

How To Use:

Arguments:

1. file_name: The file to unstage.

see all commands

commit

What It Does:

Record changes previously added to the staging area.

When To Use:

When you want to permanently store all modifications previously added to the staging area.

It’s a good practice to always comment your commits so maintainers can easily browse the branch’s history.

How To Use:

see all commands

Branch Related

branch

What It Does:

List all branches.

The current branch will be marked with the * character.

When To Use:

When you need to check if new branches were added, which ones exists and what is your current branch.

How To Use:

see all commands

branch -d

What It Does:

Delete branches.

Two different commands are used to accomplish this task: one for deleting local branches and one for deleting branches directly in the repository.

When To Use:

When a branch has served its purpose and is no longer required.

How To Use:

Arguments:

1. branch_name: The branch to be deleted.

see all commands

branch new_branch

What It Does:

Create a new branch.

It’s a good practice to give it a meaningful name.

When To Use:

When you want to separate your work from another branch (usually master) and work on a new feature without compromising a stable version.

How To Use:

Arguments:

1. new_branch: The name of the new branch.

see all commands

fetch origin branch_name

What It Does:

Download a new branch that was missing or update an existing one.

When To Use:

When you want to download a new branch, but don’t want to change to it immediately, or when you want to fetch just one branch.

How To Use:

Arguments:

1. branch_name: The branch to fetch.

see all commands

checkout branch_name

What It Does:

Switch between branches.

When To Use:

When you want to switch between branches.

How To Use:

Arguments:

1. branch_name: The branch to switch to.

see all commands

diff branch_1 .. branch_2

What It Does:

Compare two branches.

You can compare the branches last commit or their difference from their common ancestor.

When To Use:

Whenever you need to compare and revise the differences between branches.

This is especially useful as the step before merging two branches together.

How To Use:

Arguments:

1. branch_1 and  branch_2: The branches to be compared.

see all commands

merge branch_name

What It Does:

Join two or more branches together.

This command works locally. To send the merged branch to the repository, use git push.

When To Use:

When the changes made to one branch will be added to another.

The simplest example is to merge features developed in a separate branch into the stable version (master branch).

How To Use:

Arguments:

1. base_branch: The branch that receives the modifications;
2. branch_name: The branch to be merged. In this example, origin/ directly references the repository to avoid using the local version of branch_name.

see all commands

Final Words

I hope these commands can be useful for you. They don’t cover all possible situations, but as a reference guide they work quite well.

Don’t forget to leave any questions in the comments, in case you need some help, and good luck!