Git Bash Command Line Reference

Back to A Practical Guide to Git and GitHub for Windows Users

On this page you can find a summary of all Git Bash console commands explained in the book chapter by chapter. The green text are comments that briefly describes each command. The yellow text are variable entries that you have to replace with the appropriate value.

Chapter 2

  2.2 An Introduction to Git Bash

# change to home directory
$ cd ~
# creates a folder
$ mkdir <folder name>
# change to folder
$ cd <folder name>
# prints current directory
$ pwd
# list folder contents
$ ls -a

  2.3 Basic Git Configuration

# tells git who you are
$ git config --global user.name "your name"
# tells git your email
$ git config --global user.email "your.email@domain.com"
# handles end-of-line character differences
$ git config --global core.autocrlf true
# prevents conversion warning messages
$ git config --global core.safecrlf false
# sets Notepad as the default editor
$ git config --global core.editor notepad
# list all current configuration settings
$ git config --list

  2.5 Connecting Git to GitHub

# generate SSH keys
$ ssh-keygen -t rsa -b 4096 -C "your.email@domain.com"
# run the SSH agent
$ eval `ssh-agent -s`
# add private key
$ ssh-add ~/.ssh/id_rsa
# test the connection
$ ssh -T git@github.com

Chapter 3

  Hosting Your Projects on GitHub

# clone a GitHub repository
$ git clone https://github.com/your-name/your-repo
# check working directory changes
$ git status
# add changes to index
$ git add <folder>
$ git add <file>
# commit a new version
$ git commit -m <comment>
# identify remote repository
$ git remote
$ git remote show <repo name>
# push new version to GitHub
$ git push origin master

Chapter 4

  Version History and Tags

# show history of commits
$ git log --oneline --decorate
# apply tag to a version
$ git tag -a <version> <commit> -m <comment>
# tag the last commit
$ git tag <version> -m <comment>

  4.5 Comparing Versions

# view changes not yet staged
$ git diff <file>
# view the changes between the index and the last commit
$ git diff --cached <file>
# view the changes in the working tree since the last commit
$ git diff HEAD <file>
# view changes between two commits
$ git diff <commit1> <commit2> <file>

  4.6 Undoing Changes

# undo unstaged changes
$ git checkout <file>
# undo staged changes
$ git reset HEAD <file>
# undo committed change
$ git revert <commit> --no-edit

Chapter 5

  5.1 Moving, Deleting and Renaming Files

# move or rename a file and stage the change
$ git mv <source> <destination>
# delete a file and stage the change
$ git rm <file>

  Branching and Merging

# create a branch
$ git branch <branch name>
# list local branches
$ git branch
# list remote-tracking branches
$ git branch --remote
# list all branches
$ git branch --all
# switch to another branch
$ git checkout <branch name>
# merge from branch
$ git merge <branch name>
# creates a new branch and switches to it
$ git checkout -b <branch name>
# deletes a branch
$ git branch -D  <branch name>

Chapter 6

  6.6 Keeping your Fork Synchronized

# add a new remote identified by upstream
$ git remote add upstream https://github.com/user-name/repo-name
# list remotes with URL
$ git remote --verbose
# fetch the latest commits from remote identified by upstream
$ git fetch upstream
# update local master from remote identified by upstream
$ git checkout master
$ git merge upstream/master

Chapter 7

  More Git Magic

# initializes a local repo inside current directory
$ git init
# goes back in history to the specified commit
$ git checkout <commit>
# move HEAD pointer to specified commit
$ git reset --hard <commit>
# replay commits on current branch
$ git rebase <branch name>

  7.4 Saving Changes

# saves changes and clear working directory
$ git stash
# list saved changes
$ git stash list
# show saved changes
$ git stash show
# restore saved changes
$ git stash apply

  More Git Bash Magic

# creates an empty file
$ touch <file name>
# open a text file in Notepad
$ notepad <file name>
# inserts a string into a text file
$ echo "some string" >> <file name>
# show contents of text file
$ cat <file name>

Chapter 8

  8.1 The Repository Database

# list repository folder
$ ls .git
# look at HEAD pointer
$ cat .git/HEAD
# list refs directory tree
$ find .git/refs
# last commit on master
$ cat .git/refs/heads/master
# list objects directory tree
$ find .git/objects
# displays type of a Git object
$ git cat-file -t <hash>
# displays the content of a Git object
$ git cat-file -p <hash>

  8.2 A More Sophisticated History View

# convert end-of-line to DOS format
$ unix2dos <file>
# convert end-of-line to Unix format
$ dos2unix <file>
# formatting history log
$ git log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short
# using an alias
$ git <alias>

Chapter 9

  Centralized Workflow

# list all branches
$ git branch -a -v
# list tracking branches
$ git branch -vv
# create a tracking branch
$ git checkout -b <local branch> <remote branch>
# update your local copy of remote branches
$ git fetch <remote name>
# push code directly to remote branch
$ git push <remote name> HEAD:<branch name>
# push feature branch to remote repo
$ git push <remote name> <branch name>

 

Back to A Practical Guide to Git and GitHub for Windows Users

2 thoughts on “Git Bash Command Line Reference

  1. bryan

    I am on Chapter 2: Installation and Configuration. I was able to type in ‘yes’ and enter for the public key finger print but, it did not ask me to type in my private key phrase. Is there something missing? How can I show on command line to ask my private key at this stage of the book?

    Reply
  2. billy

    Hi Roberto,

    I have finished your great book. After reading your great book, the concept of Git/Github is now clear to me. Again, thank you for your wonderful book.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *