>githelp# Access help (list of commands)>githelp<command># Access help for 'command'
>gitconfig--globaluser.name"John Doe"# set name of user>gitconfig--globaluser.email"[email protected]"# user email>gitconfig--globalcolor.uitrue# color ui>gitconfig--globalcore.editoremacs# editor>gitconfig--globalmerge.tool<difftool># diff tool
>gitconfiguser.email"[email protected]"# user email for local repo>gitconfig--list# shows global and local settings
Start a repo
Start a Repository
>gitinit# create local repository in the current folder>touchREADME.md# create README file>gitci-am"Created repository."
>gitadd<filename># add filename to staging area>gitadd<listoffiles># adds files to staging area>gitadd--all# add all new, modified or deleted files to the staging area>gitadd*.txt# add all txt files in the current folder>gitadd"*.txt"# add all txt files in the whole project>gitadd.# add all files in this folder and subfolders>gitcommit-m"message"# commit staged files with message>gitcommit-am"message"# Add changes from all tracked files and commit>gitstatus# show current status of the repository>gitlog# shows history of commits>gitlog--oneline# show all commits in one single line
Git uses vi if no default editor is set to edit commit messages
ESC: leave mode
i: insert mode
:wq: save and quit (write + quit)
:q!: cancel and quit
>gitdiff# show unstaged differences since last commit>gitdiffHEAD# like git diff>gitdiffHEAD^# git diff parent of last commit against current>gitdiffHEAD^^# git diff grandparent of last commit against current>gitdiffHEAD^5# git diff 5 commits ago against current>gitdiffHEAD^..HEAD# show diff of second most recent commit against the most recent>gitdiff--staged# show staged differences since last commit>gitdiff<SHA1><SHA2># show diff between commits identified by SHA's or abbreviated SHA's>gitdiff<branch1><branch2># do a diff of two branches>gitdiff--since=1.week.ago--until=1.minute.ago# You can use time based ranges to do diffs
Unstage, reset or cancel
Unstage, reset or cancel
>gitresetHEAD<file># Unstage file>gitcheckout--<file># Reset file to state in last commit>gitreset--softHEAD^# Undo last commit and put changes in the staging area>gitreset--hardHEAD^# Undo last commit completely>gitreset--hardHEAD^^# Undo last two commits completely
HEAD refers to the last commit on the current branch
HEAD^ refers to the second last commit on the current branch
HEAD^^ refers to the third last commit on the current branch
It is recommended not to undo commits once you have pushed changes to the canonical repository
Ammend a commit
>gitcommit-ammend-m"message"# Adds staged files to last commit. The commit message will override the previous one
Remote repositories. Pushing and pulling
>gitremoteaddorigin<remoterepo># by convention use origin for canonical remote>gitremote-v# show remote repositories (verbose)>gitremoterm<name># remove remote repositories>gitpush-u<remote><branch># push local repository to remote one>gitpull<remote><branch># pull changes from remote to local repository
The -u option sets the default upstream repo so you can use “git push” and “git pull” without specifying a remote
Cloning a repository
Cloning a repository
>gitclone<address># clone repository into local repo named as the repo>gitclone<address><name># clone repository into local folder <name>
>gitbranch# list local branches>gitbranch<branchname># Create a new branch>gitbranch-d<branchname># Delete a branch>gitbranch-D<branchname># Force delete a branch (the normal -d will result in a warning if there are changes in the branch that haven't been merged anywhere)>gitcheckout<branchname># Switch to a branch>gitcheckout-b<branchname># Create a new branch and switch to it>gitpushorigin<branchname># Push branch to remote branch>gitpushorigin<localbranchname>:<remotebranchname># Push local branch to the remote branch that we select >gitmerge<frombranch># merge a branch to the current branch
>gitbranch-r# list all remote branches>gitpushorigin--delete<branchname># Delete a branch in the remote "origin" repository>gitpushorigin:<branchname># Delete a branch in the remote repository >gitpushorigin<branchname># Push branch to remote branch (this also works with newly created local branches)>gitremoteshoworigin# shows remote branches and how they map to local branches>gitremotepruneorigin# clean up references to stale remote branches (remote branches that have been deleted)
A tag is a reference to a specific commit.
>gittag# List tags>gitcheckoutv0.0.0.1# Check out code at commit tagged v0.0.0.1>gittag-av0.0.0.1-m"version 0.0.0.1"# Add a new tack>gitpush--tags# Push tags to a remote
Rebasing allows you to have a better commit history by removing the necessity of merging branches via the merge command and thus the existence of merge commits.
A rebase consists on two steps:
Fetch changes for remote repository
Move all changes to master which are not in origin/master to a temporary area
Run all origin/master commits
Run all commits in the temporary area, one at a time
>gitfetch# fetch >gitrebase<branchname># rebases from branch
If you want to, for instance merge a local branch to master via rebase you would:
Go to feature branch: git checkout feature-101
Rebase from master: git rebase master
Go to master and merge git checkout mastergit merge feature-101
When there are conflicts during rebase, git will prompt us to resolve the conflict and run the rebase command with different arguments based on if we want to continue, skip the conflicting commit, or abort the rebase.
>gitrebase--continue# Use to continue rebasing after resolving conflict>gitrebase--skip# Use to skip the conflicting commit>gitrebase--abort# Use to checkout the original branch and stop rebasing
>gitlog# show branch history>gitconfig--globalcolor.uitrue# colorize ui and thus log as well>gitlog--pretty=oneline# show history as one line commits>gitlog--oneline>gitlog--pretty=format:"%h %ad- %s [%an]"# this allows you to customize the format of the log >gitlog--oneline-p# Show what changed in each commit (files and file contents) (patch output)>gitlog--oneline--stat# Show how many inserts and deletions per file and commit>gitlog--oneline--graph# Visual representation of branches and commits>gitlog--until=1.minute.ago# use dates to limit log command>gitlog--since=1.day.ago>gitlog--since=1.hour.ago>gitlog--since=1.month.ago--until=2.weeks.ago>gitlog--since=2010.10.10--until=2012.12.12
Note: Some of the options you can use in git log --pretty:format are:
%h SHA hash
%ad author date
%an author name
%d ref names
run git help log for more options.
You can use the blame command to see all the changes (line by line) on a given file throughout the whole file history and who made those changes.
>gitblameindex.html--dateshort# show all changes in index.html and who made them
You can exclude files in your local repository by putting them in the .git/info/exclude file, either specifically or using patterns.
You can ignore files completely and in all repositories by adding files and file patterns to the .gitignore file in your repository.
>gitrm<filename># remove file
To untrack a file (remove it from being tracked by git but not from your actual file system) use: