Git Wiki

Help

Help
1
2
> git help # Access help (list of commands)
> git help <command> # Access help for 'command'

Configuration

Global Config
1
2
3
4
5
> git config --global user.name "John Doe" # set name of user
> git config --global user.email "[email protected]" # user email
> git config --global color.ui true # color ui
> git config --global core.editor emacs # editor
> git config --global merge.tool <difftool> # diff tool
Local Config
1
2
> git config user.email "[email protected]" # user email for local repo
> git config --list # shows global and local settings

Start a repo

Start a Repository
1
2
3
> git init # create local repository in the current folder
> touch README.md # create README file
> git ci -am "Created repository."

Basic commands

Basic commands
1
2
3
4
5
6
7
8
9
10
11
> git add <filename> # add filename to staging area
> git add <list of files> # adds files to staging area
> git add --all # add all new, modified or deleted files to the staging area
> git add *.txt # add all txt files in the current folder
> git add "*.txt" # add all txt files in the whole project
> git add . # add all files in this folder and subfolders
> git commit -m "message" # commit staged files with message
> git commit -am "message" # Add changes from all tracked files and commit
> git status # show current status of the repository
> git log # shows history of commits
> git log --oneline # show all commits in one single line

Note:

  • Git uses vi if no default editor is set to edit commit messages
    • h: left
    • k: up
    • l: right
    • j: down
    • ESC: leave mode
    • i: insert mode
    • :wq: save and quit (write + quit)
    • :q!: cancel and quit

Diffs

Diffs
1
2
3
4
5
6
7
8
9
10
> git diff # show unstaged differences since last commit
> git diff HEAD # like git diff
> git diff HEAD^ # git diff parent of last commit against current
> git diff HEAD^^ # git diff grandparent of last commit against current
> git diff HEAD^5 # git diff 5 commits ago against current
> git diff HEAD^..HEAD # show diff of second most recent commit against the most recent
> git diff --staged # show staged differences since last commit
> git diff <SHA1> <SHA2> # show diff between commits identified by SHA's or abbreviated SHA's
> git diff <branch1> <branch2> # do a diff of two branches
> git diff --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
1
2
3
4
5
> git reset HEAD <file> # Unstage file
> git checkout -- <file> # Reset file to state in last commit
> git reset --soft HEAD^ # Undo last commit and put changes in the staging area
> git reset --hard HEAD^ # Undo last commit completely
> git reset --hard HEAD^^  # Undo last two commits completely

Notes:

  • 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

Ammend
1
> git commit -ammend -m "message" # Adds staged files to last commit. The commit message will override the previous one

Remote repositories. Pushing and pulling

Remote repositories
1
2
3
4
5
> git remote add origin <remote repo> # by convention use origin for canonical remote
> git remote -v # show remote repositories (verbose)
> git remote rm <name> # remove remote repositories
> git push -u <remote> <branch> # push local repository to remote one
> git pull <remote> <branch> # pull changes from remote to local repository

Note:

  • 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
1
2
> git clone <address> # clone repository into local repo named as the repo
> git clone <address> <name> # clone repository into local folder <name>

Branches

Branches
1
2
3
4
5
6
7
8
9
> git branch # list local branches
> git branch <branch name> # Create a new branch
> git branch -d <branch name> # Delete a branch
> git branch -D <branch name> # 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)
> git checkout <branch name> # Switch to a branch
> git checkout -b <branch name> # Create a new branch and switch to it
> git push origin <branch name> # Push branch to remote branch
> git push origin <local branch name>:<remote branch name> # Push local branch to the remote branch that we select 
> git merge <from branch> # merge a branch to the current branch
Remote Branches
1
2
3
4
5
6
> git branch -r # list all remote branches
> git push origin --delete <branch name> # Delete a branch in the remote "origin" repository
> git push origin :<branch name> # Delete a branch in the remote repository 
> git push origin <branch name> # Push branch to remote branch (this also works with newly created local branches)
> git remote show origin # shows remote branches and how they map to local branches
> git remote prune origin # clean up references to stale remote branches (remote branches that have been deleted)

Tagging

A tag is a reference to a specific commit.

1
2
3
4
> git tag # List tags
> git checkout v0.0.0.1 # Check out code at commit tagged  v0.0.0.1
> git tag -a v0.0.0.1 -m "version 0.0.0.1" # Add a new tack
> git push --tags # Push tags to a remote 

Rebase

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:

  1. Fetch changes for remote repository
  2. Rebase

    1. Move all changes to master which are not in origin/master to a temporary area
    2. Run all origin/master commits
    3. Run all commits in the temporary area, one at a time
1
2
> git fetch # fetch 
> git rebase <branch name> # rebases from branch 

If you want to, for instance merge a local branch to master via rebase you would:

  1. Go to feature branch: git checkout feature-101
  2. Rebase from master: git rebase master
  3. Go to master and merge git checkout master git 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.

1
2
3
> git rebase --continue # Use to continue rebasing after resolving conflict
> git rebase --skip # Use to skip the conflicting commit
> git rebase --abort # Use to checkout the original branch and stop rebasing

History

1
2
3
4
5
6
7
8
9
10
11
12
13
> git log # show branch history
> git config --global color.ui true # colorize ui and thus log as well
> git log --pretty=oneline # show history as one line commits
> git log --oneline
> git log --pretty=format:"%h %ad- %s [%an]" # this allows you to customize the format of the log 
> git log --oneline -p # Show what changed in each commit (files and file contents) (patch output)
> git log --oneline --stat # Show how many inserts and deletions per file and commit
> git log --oneline --graph # Visual representation of branches and commits
> git log --until=1.minute.ago # use dates to limit log command
> git log --since=1.day.ago
> git log --since=1.hour.ago
> git log --since=1.month.ago --until=2.weeks.ago
> git log --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
  • %s subject
  • %an author name
  • %d ref names

run git help log for more options.

Blame

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.

1
> git blame index.html --date short # show all changes in index.html and who made them

Excluding Files

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.

Removing files

1
> git rm <file name> # remove file

Untracking file

To untrack a file (remove it from being tracked by git but not from your actual file system) use:

1
> git rm --cached <file name> # untrack file

Useful Aliases

These are some useful git aliases:

1
2
3
4
5
6
> git config --global alias.co checkout
> git config --global alias.br branch
> git config --global alias.ci commit
> git config --global alias.st status
> git config --global alias.mylog "log --pretty:format..."
> git config --global alias.lol "log --graph --decorate --pretty=oneline --abbrev-commit --all"

Resources

Comments