Git Areas and Commands

Help

  1. Display help message for a specific git subcommand:

    Use one of the following commands:

    git <verb> --help
    
    git help <verb>
    
    man git-<verb>
    
  2. Display quick help message for a specific git subcommand

    git <verb> -h
    

Repository Setup

  1. Initialize a new repository.

    git init <directory>
    
    • Omit <directory> to initialize in the current directory.
  2. Initialize a new repository in the current directory and commit all files.

    git init
    git add .
    git commit -m "Initial commit"
    
  3. Clone a repository.

    git clone <repository-url> <directory>
    
    • Omit <directory> to clone into a directory with the same name as the remote repository, inside the current directory.

Configuration

  1. Specify configuration scope:

    git config --local
    git config --global
    git config --system
    
  2. List all configurations:

    git config --list
    
    • --show-origin: Show origin (file path) of each configuration.
    • --show-scope: Show scope of each configuration.
    • --name-only: Show only names of configurations (without values).
  3. Get value of a specific configuration:

    git config --get <key>
    
  4. Set value of a specific configuration:

    git config <key> <value>
    
  5. Remove a specific configuration:

    git config --unset <key>
    
  6. Common global settings.

    Set global user name:

    git config --global user.name <your_name>
    

    Set global user email:

    git config --global user.email <your_email>
    

    Set global editor:

    git config --global core.editor <editor>
    

Branches

  1. List branches.

    git branch
    

    List only local branches by default.

    • -r or --remote: Show only remote branches.
    • -a | --all: Show all branches (local and remote).
    • -v or --verbose: Show more information about branches.
    • -vv or --verbose --verbose: Show more information about branches and their upstream branches.
    • --list <pattern>: Show branches that match the <pattern>.
  2. Create a new branch from the current branch:

    git branch <branch_name>
    
  3. Switch to a branch:

    git checkout <branch_name>
    
    git switch <branch_name> # Git 2.23+ | Experimental
    
  4. Create a new branch and switch to it:
    -b or --branch.

    git checkout -b <branch_name>
    

    Force create a new branch, replacing an existing branch:

    git checkout -B <branch_name>
    
  5. Rename a branch:
    -m or --move.

    git branch -m <old_branch_name> <new_branch_name>
    
  6. Delete a branch:
    -d or --delete. Deleted only if it is merged into its upstream branch, or HEAD if no upstream branch is set.

    git branch -d <branch_name>
    

    Force delete a branch:

    git branch -D <branch_name>
    
  7. Delete a remote branch:

    git push <remote> --delete <branch_name>
    
  8. Set upstream branch:

    -u or --set-upstream-to.

    git branch -u <upstream_branch>
    
  9. Remove upstream branch:

    git branch --unset-upstream
    

Status and Information

  1. Show status of working directory and staging area:

    git status
    
    • -s or --short: Show status in short format.
    • -b or --branch: Show status along with branch information in short format.
    • -u<mode> or --untracked-files=<mode>: Show/hide untracked files.
      • no: Do not show untracked files.
      • normal: Show untracked files and directories.
      • all: Also shows individual files in untracked directories.
    • -v or --verbose: Show diff of staged changes.
    • -vv or --verbose --verbose: Show diff of staged and unstaged changes.
    • <files>: Show status of a specific file.
    • <directories>: Show status of files in a specific directory.
    • <pattern>: Show status of files that match the <pattern>.
  2. Short format characters:

    •  : Unmodified
    • M: Modified
    • T: File type changed (regular file, symbolic link or submodule)
    • A: Added
    • D: Deleted
    • R: Renamed
    • C: Copied
    • U: Updated but unmerged
    • ?: Untracked
    • !: Ignored
  3. Show changes.

    In working directory (unstaged changes):

    git diff
    

    In staging area (staged changes):
    --staged or --cached

    git diff --staged
    

    All changes (both staged and unstaged):

    git diff HEAD
    

    Between two commits:

    git diff <first-commit> <second-commit>
    
  4. Options for git diff:

    • --stat: Show statistics.
    • --name-only: Show only names of changed files.
    • --name-status: Show names and status of changed files.
    • <files>: Show status of a specific file.
    • <directories>: Show status of files in a specific directory.
    • <pattern>: Show status of files that match the <pattern>.
  5. Open a visual diff tool:

    git difftool
    
  6. Show author and date of last change for each line in a file:

    git blame <file>
    
  7. Show changes in a specific file in the last commit:

    git show <file>
    
  8. Show changes in a specific commit and its parent:

    git show <commit>
    
  9. Show content of a file in a specific commit:

    git show <commit>:<file>
    
  10. Show full change history:

    git log
    
  11. Show full change history of a file.

    Show commits that changed a file:

    git log --follow -- <file>
    

    Show commits and changes that changed a file:

    git log -p -- <file>
    
  12. List ignored files:

    git check-ignore <pathname>
    
    • *: List all ignored files at the root level.
    • **: List all ignored files recursively.
    • <pathname>: List ignored files in a specific directory.
    • -v or --verbose: Show ignored files along with the rule that ignores them.
    • -n or --non-matching: Show files that are not ignored. Use with -v to differentiate between ignored and non-ignored files.

Changes

  1. Add changes from working directory to staging area:

    git add <files>
    
    • .: Add new and modified files in the current directory and its subdirectories. Does not add deleted files.
    • -A or --all: Add all changes, including untracked files and deleted files, in the working directory.
    • -u or --update: Add modified and deleted files, but not untracked files.
    • <files>: Add specific files.
    • <directories>: Add all files in the specified directories.
    • <pattern>: Add files that match the <pattern>.
  2. Reset changes in the staging area and keep them in the working directory:

    git reset
    
    • <files>: Reset changes in specific files.
    • <directories>: Reset changes in all files in the specified directories.
    • <pattern>: Reset changes in files that match the <pattern>.
  3. Reset all changes in the staging area and working directory to the last commit:

    git reset --hard
    

Commits

  1. Commit changes in the staging area:

    git commit
    
    • -m <message> or --message=<message>: Add a message to the commit. If not provided, opens the default editor to add a message.
    • -a or --all: Add all changes to the commit.
    • -v or --verbose: Show changes in the commit message.
    • -q or --quiet: Do not show commit summary after commit.
    • --amend: Amend the last commit.
  2. Delete last commit:

    git reset HEAD~1
    

    Moves the current branch HEAD to the previous commit.

    • --soft: Do not alter uncommitted changes in staging area and working directory. Add changes from previous commit to the working directory.
    • --mixed: Move all staged changes to working directory. Add changes from previous commit to the working directory.
    • --hard: Discard all changes including uncommitted changes in staging area and working directory.

Push

  1. Push changes to a remote repository:

    git push <remote> <branch>
    
    • Omit <branch> to push changes to the current branch.
    • Omit <remote> to push changes to the default remote repository.
    • -n or --dry-run: Show what would be pushed, without pushing.
    • --force or -f: Force push.
    • -u or --set-upstream: Set upstream branch.

Fetch

  1. Fetch changes from a remote repository:

    git fetch <remote>
    
    • If <remote> is omitted, uses upstream of the current branch, else uses origin.
    • --all: Fetch changes from all remotes.
    • --dry-run: Show what would be fetched.
    • --prune: Remove remote tracking branches that no longer exist on the remote.
    • -t or --tags: Fetch all tags.

Pull

  1. Pull changes from a remote repository:

    git pull <remote> <branch>
    
    • Omit <branch> to pull changes from the upstream branch.
    • Omit <remote> to pull changes from the default remote repository.
    • --rebase: Rebase changes instead of merging.
    • --ff-only: Allow only fast-forward merges.
    • --no-rebase: Merge changes instead of rebasing.

Merge

  1. Merge changes from a <feature_A> into the <development> branch:

    git checkout <development>
    git merge <feature_A>
    
    
    git push origin <development>
    
    # Delete the feature branch
    
    • --no-ff: Create a merge commit even if fast-forward is possible.
    • --ff-only: Allow only fast-forward merges.
    • --squash: Merge changes into a single commit.

Rebase

  1. Rebase changes from a <feature_A> onto the <development> branch:

    git checkout <feature_A>
    git rebase <development>
    
    git checkout <development>
    git merge <feature_A>
    
    git branch -d <feature_A>
    
    git push origin <development>
    
  2. Interactive rebase:
    -i or --interactive.

    git rebase -i <commit>
    
    • <commit>: Start rebase from a specific commit.
    • r or reword: Change commit message.
    • e or edit: Change commit.
    • s or squash: Combine commit with previous commit.
    • f or fixup: Combine commit with previous commit without keeping the commit message.
    • d or drop: Remove commit.
    • p or pick: Keep commit as is.
  3. Continue a rebase after resolving conflicts:

    git rebase --continue
    
  4. Skip a commit during rebase:

    git rebase --skip
    
  5. Abort a rebase:

    git rebase --abort
    

Remote Repositories

  1. List remote repositories:

    git remote
    
    • -v or --verbose: Show URLs of remote repositories.
  2. Add a remote repository:

    git remote add <name> <url>
    
  3. Remove a remote repository:

    git remote remove <name>
    
  4. Rename a remote repository:

    git remote rename <old_name> <new_name>
    
  5. Show information about a remote repository:

    git remote show <name>
    
  6. Get URLs of a remote repository.

    git remote get-url <name>
    
    • --push: Get push URL.
  7. Set URLs of a remote repository.

    git remote set-url <name> <url>
    
    • --push: Set push URL.

Tags

  1. List tags:

    git tag
    
    • -l <pattern> or --list=<pattern>: Show tags that match the <pattern>.
    • --sort=<key>: Sort tags by <key>.
      • refname: Sort by tag name.
      • version:refname or v:refname: Sort by version number, when tag name is a version number.
      • taggerdate: Sort by tagger date. (annotated tags only)
      • committerdate: Sort by tag commit date of the referenced commit.
      • creatordate: Sort by tag creation date of the tag object. (annotated tags only)
      • Prefix with - for reverse order.
      • Use tab key for autocompletion.
    • --merged: Show tags that are merged into the current branch.
    • --no-merged: Show tags that are not merged into the current branch.
    • --contains <commit>: Show tags that contain a specific commit.
    • -a or --annotate: Show annotated tags.
  2. Create a lightweight tag:

    git tag <tag_name>
    
    • <commit>: Tag a specific commit.
  3. Create an annotated tag:
    -a or --annotate.

    git tag -a <tag_name>
    
    • -m <message> or --message=<message>: Add a message to the tag. If not provided, opens the default editor to add a message.
    • <commit>: Tag a specific commit.
  4. Push tags to a remote repository:

    git push <remote> <tag>
    
    • --tags: Push all tags. Omit <tag> to push all tags.
  5. Delete a tag:

    Local tag:

    git tag -d <tag_name>
    

    Remote tag:

    git push <remote> --delete <tag_name>
    
  6. Show information about a tag:

    git show <tag_name>
    

Stash

  1. List stashes:

    git stash list
    
  2. Stash changes:

    Staged and unstaged changes:

    git stash push
    
    • -u or --include-untracked: Stash staged changes, unstaged changes and untracked files.
    • -a or --all: Stash staged changes, unstaged changes, untracked files and ignored files.
    • -S or --staged: Stash only staged changes.
    • -k or --keep-index: Keep staged changes after stashing.
    • -m <message> or --message=<message>: Add a message to the stash.
  3. Apply stash and keep it in the stash list.

    git stash apply
    
    • stash@{n}: Apply a specific stash.
  4. Apply stash and remove it from the stash list.

    git stash pop
    
    • stash@{n}: Apply a specific stash.
  5. Show changes in a stash:

    git stash show
    
    • stash@{n}: Show changes in a specific stash.
  6. Delete stash:

    git stash drop
    
    • stash@{n}: Delete a specific stash.
  7. Clear all stashes:

    git stash clear
    

Ignore Files

  1. Create a .gitignore file:

    touch .gitignore
    
  2. Add files and directories to ignore:

    echo <pattern> >> .gitignore
    
    • # <comment>: Add comments. Use \# for patterns that start with #.
    • : Include a space at the end of a line.
    • !: Do not ignore a file or directory, ignored by a previous pattern. Use \! for patterns that start with !. Files inside ignored directories are always ignored.
    • Patterns with / at beginning or in middle are relative to the directory level of the particular .gitignore file. Otherwise, they can match at any level below the .gitignore file.
    • Pattern ending with / matches directories only.
    • *: Matches anything except /.
    • ?: Matches any single character, except /.
    • [a-zA-Z]: Matches any character in the range.
    • Leading **/: Matches in all directories.
    • Trailing /**: Matches everything inside a directory.
    • /**/: Zero or more directories in a path.