CodeProject

Basic Git Command Line Reference for Windows Users


gitWhile 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:

Working with the file system

Configuring Git and Creating a Repository:

Staging and Committing Changes

Working with Remote Repositories (like Github)

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.

Git Bash: Show Directory Contents (ls)

Syntax:

ls [options]

Options:

-1 = List 1 item per line
-r = Reverse the sort order
-a  = Show Everything, including hidden items
-d = list only directories
-l = (letter L, lowercase) = Use a long listing format (more info per item, arranged in columns, vertical listing)

List the contents of the current directory (folder):
$ ls

The above will display the contents of the current directory as a horizontal list. Not real convenient.

List the contents of the current directory, one item per line:
$ ls -1

That’s better. Note, however, that we can only differentiate files from subdirectories based upon the file extension.

List only the subdirectories (folders) within the current directory:
$ ls –d */
List everything in long form, vertically:
$ ls –al

The above gives a swath of information. Also, subdirectories are differentiated by the first column (begin with drwxr instead of -rw)

List all contents, including subdirectory contents, single item per line:
$ ls -1 *

Git Bash: Create a New Directory (mkdir)

Syntax:

mkdir [options] <folderName>

Options:

-p = Create parent directories as needed
--verbose = Show a message for each new directory created (note the double dash)

Create a folder in the current directory (without spaces in the folder name):
$ mkdir NewFolderName
Create a folder in the current directory (with spaces in the folder name):
$ mkdir “New Folder Name”
Create a folder at the specific directory path:
$ mkdir /c/ExistingParentFolder/NewFolderName
Create a folder at the specific directory path, and create parent directories as needed:
$ mkdir -p /c/NewParentFolder/NewFolderName
Create a folder at the specific directory path, create parent directories as needed, and print a description of what was done in the console window:
$ mkdir -p --verbose /c/NewParentFolder/NewFolderName

Git Bash: Create Files (touch, echo)

Syntax:

touch [options] <FileName>

echo [options] TextString > FileName

(NOTE: FileName can include directory. Default is the current directory).

Create a single (empty) text file in the current directory:
$ touch newFile.txt
Create a single (empty) text file in the specified directory:
$ touch /c/SomeFolder/newFile.txt
Create multiple (empty) text files in the current directory:
$ touch newFile_1.txt newFile_2 . . . newFile_n
Append text to a file. If the file does not exist, one is created:
$ echo “This text is added to the end of the file” >> newFile.txt
Overwrites text in a file. If the file does not exist, one is created:
$ echo “This text replaces existing text in the file” > newFile.txt
Overwrites text in a file at the specified location. If the file does not exist, one is created:
$ echo “This text replaces existing text in the file” > /c/SomeFolder/newFile.txt

Git Bash: Remove Files (rm)

Syntax:

rm [options] -<FileName>

Options:

-I (or –interactive) = Prompt before removal

-v (or –verbose) = Explain what is being done

Remove the specified file from the current directory (no spaces):
$ rm -DeleteFileName
Remove the specified file from the current directory (with spaces):
$ rm -“Delete File Name”
Prompt for confirmation before remove the specified file from the current directory (no spaces):
$ rm -i -DeleteFileName
Removes the specified file and reports what was done in the console window:
$ rm -v -DeleteFileName

Git Bash: Remove Directories (rmdir, rm -rf)

Syntax:

rmdir [options] <FolderName>

rm -rf

Removes the specified folder if empty. Operation fails if folder is  not empty:
$ rmdir -DeleteFolderName
Removes the specified folder and all contents:
$ rm -rf -DeleteFileName

Git Bash: Configure Git (git config)

Syntax:

git config --global user.name <“User Name”>

git config --global user.email <UserEmailAddress>

Set the global User.Name value for the current user on the system:
$ git config --global user.name “FirstName LastName”
Set the global User.Email value for the current user on the system:
$ git config --global user.email UserEmailAddress

Show me the current values:

The following return the current values for the user.name and user.email properties respectively and output the values to the console window:

Print the current global User.Name value to the console window:
$ git config --global user.name
Print the current global User.Email value to the console window:
$ git config --global user.email

Git Bash: Initialize a New Git Repo (git init)

Syntax:

git init

Create files required in the current directory to perform version control:
$ git init

Git Bash: Add/Stage for Commit (git add)

Syntax:

git add [options] [<File_1>] [<File_2>] . . . [<File_n>]

Options:

-A (or --all) = Add all new or changed files to the staged changes, including removed items (deletions)

-u = Add changes to currently tracked files and removals to the next commit. Does not add new files.

. = Adds new or changed files to the staged changes for the next commit, but does not add removals.

Note that git add -A is semantically equivalent to git add . followed bygit add –u

-p = Interactive add. Walks through changes in the working directory and prompts for add

Add all changes in the working directory to the next commit, including new files and deletions:
$ git add -A
Add all changes to tracked files and all new files to the next commit, but do not add file deletions:
$ git add .
adds all changes to tracked files and all file removals to the next commit, but does not add new files:
$ git add -u
Walks through changed files and prompts user for add option. Does not include new files:
$ git add -p

Git Bash: Unstage from Commit (git reset)

Syntax:

git reset HEAD <File_1>

Remove the specified file from the next commit:
$ git reset HEAD FileName

Git Bash: Committing Changes (git commit)

Syntax:

git commit [options] [<File_1>] [<File_2>] . . . [<File_n>] [-m <“Commit Message”>]

Options:

-a = Commit all changes to tracked files since last commit

-v = Verbose: include the diffs of committed items in the commit message screen

--amend = Edit the commit message associated with the most recent commit

--amend <File_1> <File_2> . . . <File_n> = redo the previous commit and include changes to specified files

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.

Additional Resources

ASP.Net
ASP.NET MVC–Azure Websites and EF Model First Connection Strings when Deploying from Github
ASP.Net
ASP.NET MVC: Keep Private Settings Out of Source Control
ASP.Net
Customizing Routes in ASP.NET MVC
  • Sunil

    SunilSunil

    Author Reply

    Thanks for the article :)


  • adyoi

    adyoiadyoi

    Author Reply

    How to run command ‘su’ or ‘sudo’ in git bash


    • John Atten

      John AttenJohn Atten

      Author Reply

      Far as I know, there is no corresponding “sudo” command. You can right click on the .exe and select “Run as Administrator” I think though…