Using Git

Below are some notes on how to use git from day to day.

Basics

 

Command Description

$ git config --global user.name "<your name>"
$ git config --global user.email "<your email>"

Configures git with your name and your email. If you don't do this at a global level or at a repository level, git will complain about this.
$ git init Run this command inside a directory that alread has source code to set up a repository.
$ git clone <url> <dest_dir> Creates a new repository in the specified directory.
$ git status Checks that status of the files in the repository. There are two states for files, one being Tracked, and the other being Untracked. Tracked includes all file that are unmodified, modified, or stages while Untracked is everything else. For example, if you add a file to the repositiory after cloning, then the added file is considered Untracked.
$ git add <file> Adds a file to be tracked and commited next time the commit comand is issued. If you run this command, and then make changes and then commit. The changes that were made after it was staged will not be commited.
$ git commit
$ git commit -m "message"
$ git commit -a -m "message"
First command will open up the default editor so you can create a commit message.
The second command will take the commit message as a parameter.
The third command will stage all files and then take the commit message as a parameter. This means you do not need to do git add <file>
$ git diff
$ git giff --cached
 First command shows what changes that were made and that are unstaged.
The second command shows what will be commited.
$ git rm <file>
$ git rm --cached <file>
$ git rm mv <file> <file>
First command deletes the file from the repository and the hard drive.
The second command will remove the file from being tracked.
The last command will rename the file.

 

Ignoring Files

 

Pattern Description
 dir/  Ending slash means to exclude directory.
 /README  Ignore the README file in root directory.
 *.log  Ignore all files with the extension of log.
!out.log Do not ignore the out.log file, this would be used in conjunction with the above pattern.

 

Git Logs

 

Comand Description
$ git log lists the commits made in the repository in reverse chronological order.
$ git log stat Shows stats on commits.
$ git log -p -2 Shows the differences in each commit. The -2 shows last two commits.
$ git log --pretty=format:"%h - %n, %ar : %s" For mats the log according to user input.
$ git log --pretty=format:"%H - %ar" --graph Displays the commits in a ASCII graph.

 

Filtering and Output Options

 

Option Description
-p Shwo the patch introduced with each commit.
--stat Show statics for files modified in each commit.
--shortstat Display only the changed/insertions/deletions line from the --stat command.
--name-only Show the list of files modified after the commit information.
--name-status Show the list of files affected with added/modified/deleted information as well.
--abbrev-commit Display the date in relative format instead of using the full date format.
--graph Show an ASCII graph of the brand and merge history beside the log output.
--pretty  show commits in an alternat format. Options include oneline, short, full, fuller, and format -- where you specify your format.
-(n) Show the last n commits.
--since, --after Limit the commits to those made after a specified date.
--until, --before Limit the commits to those made before a specified date.
--author Only show commits in which the author entry matches the specified string.
--commiter Show only mitts in which the commiter entry matches the specified string.

 

Log Formatting Options

 

Option Description
%H Commit Hash
%h Abbreviated commit hash
%T Tree hash
%t Abbreviated tree hash
%P Parent hashes
%p Abbreviated parent hashes
%an Author name
%ae Author email
%ad Author date
%ar Author date, relative
%cn Commiter name
%ce Commiter e-mail
%cd commiter date
%cr commiter date, relative
%s subject

Changing Last Commit

$ git commit -m "message"
$ git add stdio.h
$ git commit --ammend

Misc commands

To unstage a file:

$ git reset HEAD stdio.h

To discard changes to a file

$ git checkout -- stdio.h

Working with Remote Repositories

Command Description
$ git remote -v Shows short name and url for repositories
$ git remote add <name> <url> Adds a remote repository.
$ git fetch <remote-name> Doing this will give you access to all branches of the remote repository. No local files will exist for you to work on.To get files to work on, you must check out the branch.
$ git pull gets a branch and merges it.
$ git push <remote-name> <branch> Pushes a branch to a remote repository. If the remote branch has changed since you started working on it, you must run git fetch <remote-name> to synchornize the work.
$ git remote rename <from> <to> Rename
 $ get remote rm <remote-name> Deletes a remote name.