While there are GUI interfaces available for GIT (some good, some bad), familiarity with at least the basics of git’s command line interface can only enhance your ability to use the tool to maximum effectiveness. Since I am relatively new to git and version control in general, I set out to learn the basics of the git command line. In doing so, I found it handy to keep a list of the commonly-used commands nearby so that I didn’t have to keep Googling.

In this post, I am going to cover the very basic set of commands one might require to effectively navigate and maintain your source repo using only the git Bash command line interface. Probably, in creating this post, I will not need to look at this again, as the sheer fact of composing this list and explaining it all will burn these into my brain forever. On the other hand, if I am ever unsure, I will now have a place to come look!

NOTE: The references here are by no means comprehensive. I have included the basic commands required to get started, and the commonly used options for each command. There is a wealth of additional information available on the internet, and I have included some helpful reference links at the end of this post.

To more easily find what you might be looking for, here are some links to specific sections of this post:

Undoing Changes and Working With Tags

Git Bash: Syntax Notes

Directory Paths

First off, note that Git Bash is a *nix application (Unix/Linux), and expects inputs according to *nix conventions when it comes to file system navigation. This is important when using Git on a windows system, because we need to mentally map the familiar Windows directory notation to Unix format:

Windows Directory Path

<---- Becomes ----->

*nix Directory Path

C:\Users\MyUserFolder\

/c/Users/MyUserFolder/

Strings with Spaces

When we are going to provide an input string with no spaces, we need do nothing. However, strings which contain spaces must be enclosed in quotes. Remember this. Personally, I just use quotes around strings in general.

The “Home” Directory

The file system in *nix systems is set up a little differently than in Windows. Git Bash assumes the existence of a “home” directory for each user. In Windows, the default is your personal user folder. This folder is where Git Bash opens by default. Typing only cd after the command prompt will always return you to the root level of the home directory.

Command Syntax Format:

The basic command syntax for a git Bash Command is:

$ CommandName [options] [directory]

In the above, the square brackets denote optional parts of the command. The square brackets themselves are not typed into the command line. Items following a command which are not enclosed in brackets are required.

Cases and Spaces Count

Also note that git Bash is case-sensitive, and spaces count. For Example, the common command to change to another directory is cd. This is NOT the same as CD or Cd.

Optional Input

When portions of a command are optional, we will note this by enclosing them in square braces:

$ Command [options]

In the above, we do type the square brackets.

User Input

For our purposes here, when we are presenting command syntax examples, we will denote user-provided values between angle brackets:

$ Command [options] <SomeUserInput>

In the above, we do type either the square or angle brackets.

Git Bash: Navigating the File System (cd)

Syntax:

cd [options] [<directory>]

Navigate to the Home Directory (Default folder for the current user):

$ cd

Navigate to a specific folder in the file system:

$ cd /c/SomeFolder/SomeOtherFolder/

Navigate to a specific folder in the file system (if there are spaces in the directory path):

$ cd “/c/Some Folder/Some Other Folder/”

Go back to the previous Location:

$ cd -

Move Up One Directory Level:

$ cd ..

In the above, the cd command is followed by a space, then two period with no space between.

Commits the changes for the specific file(s) and includes the commit message specified:

$ git commit FileName –m “Message Text”

Note that Git requires a commit message. If you do not provide one using the -m option, you will be prompted to do so before the commit is performed.

Commits all files changed since last commit. Does not include new files.

$ git commit –a –m “Message Text”

Adds all changes to the previous commit and overwrites the commit message with the new Message Text. Does not include new files:

$ git commit –a –amend –m “Message Text”

Git Bash: Remote Repositories (git remote)

Syntax:

git remote add <RemoteName> <RemoteURL>

git remote show <RemoteName>

NOTE: As used here, RemoteName represents a local alias (or nickname) for your remote repository. The name of the remote on the server does not necessarily have to be the same as your local alias.

Add the specified remote repo to your git config file. The remote can then be pushed to/fetched from:

$ git remote add RemoteName https://RemoteName/Proj.git

Print information about the specified remote to the console window:

$ git remote show RemoteName

Git Bash: Branching (git branch)

Syntax:

git branch [options][<BranchName>][<StartPoint>]

Options:

-a = List all local and remote branches

-r=List all remote branches

List all local branches:

$ git branch

List all remote branches:

$ git branch -r

List all local and remote branches:

$ git branch -a

Create a new branch starting at the some point in history as the current branch:

$ git branch BranchName

Note that this creates the new branch, but does not “check it out” (make it the current working branch).

Switch from the current branch to the indicated branch:

$ git checkout BranchName

Create a new branch and switch to it from the current branch:

$ git checkout –b NewBranchName StartPoint

Note that StartPoint refers to a revision number (or the first 6 characters of such) or an appropriate tag.

Git Bash: Merging Branches

Syntax:

git merge [<BranchName>][--no-commit]

Merge the specified branch into the current branch and auto-commit the results:

$ git merge BranchName

Merge the specified branch into the current branch and do not commit the results:

$ git merge BranchName --no-commit

Git Bash: Pushing to Remote Repositories (git push)

Syntax:

git push [<RemoteName> <BranchName>]

Update the remote server with commits for all existing branches common to both the local system and the server. Branches on the local system which have never been pushed to the server are not shared.

$ git push

Updates the remote server with commits for the specific branch named. This command is required to push a new branch from the local repo to the server if the new branch does not exist on the server.

$ git push RemoteName BranchName

Git Bash: Fetching from Remote Repositories (git fetch)

Syntax:

git fetch <RemoteName>

Retrieve any commits from the server that do not already exist locally:

$ git fetch RemoteName

NOTE: git fetch retrieves information from the remote and records it locally as a branch in your current repository. In order to merge the new changes into your local branch, you need to run git fetch followed by git merge. Since there may be more than one branch on the remote repository, it is necessary to specify the branch you wish to merge into your current branch:

Merge syntax for post-fetch merge:

git merge <RemoteName/BranchName>

Merge the newly fetched branch from the remote into your current working branch:

$ git merge RemoteName/BranchName

Using fetch before merging allows you to pull changesets in from the remote, but examine them and/or resolve conflicts before attempting to merge.

Git Bash: Pulling from Remote Repositories (git pull)

Syntax:

git pull <RemoteName/BranchName>

Fetch changes from the specified branch in the remote, and merge them into the current local branch:

$ git pull RemoteName/BranchName

NOTE: git pull is essentially the same as running git fetch immediately followed by git merge.

Git Bash: Undo (git reset)

Syntax:

git reset [options]

Options:

--hard = undo everything since the last commit

--hard ORIG_HEAD = Undo most recent merge and any changes after.

--soft HEAD^ = undo last commit, keep changes staged

Undo everything since the last commit:

$ git reset --hard

Undo most recent successful merge and all changes after:

$ git reset --hard ORIG_HEAD

Undo most recent commit but retain changes in staging area:

$ git reset --soft HEAD^

Git Bash: Tags (git tag)

Syntax:

git tag [options] [<TagName>] [<CommitChecksum>] [<TagMessage?]

Options:

-a = Annotated Tag

-m = Annotated Tag Message

List all tags in the current repository:

$ git tag

Create a tag at the current revision:

$ git tag TagName

Create a tag at the commit specified by the partial checksum (six characters is usually plenty):

$ git tag TagName CommitChecksum

Create an annotated tag:

$ git tag -a TagName -m TagMessage

Create an annotated tag at the commit specified by the partial checksum:

$ git tag -a TagName CommitChecksum

Push tags to a remote repository:

$ git push --tags

Print information about a specific tag to the console window:

$ git show TagName

Almost all of the information presented above represents a “toe in the water” sampling designed to get started. There are plenty more commands for use both within Git itself, and from the more general Bash command line. Additionally, most of the commands listed here have more options than I have included. I tried to keep this simple, as a reference for myself, and for whoever else may find it useful to get started with the basics.

About the author

My name is John Atten, and my "handle" on many of my online accounts is xivSolutions. I am Fascinated by all things technology and software development. I work mostly with C#, JavaScript/Node, and databases of many flavors. Actively learning always. I dig web development. I am always looking for new information, and value your feedback (especially where I got something wrong!). You can email me at: