Home git commands
Post
Cancel

git commands

git commands

process

flowchart LR
    A[pull] --> B[coding] --> C[commit] --> D[pull] --> E[push]

config

git-config - Get and set repository or global options

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
git --version
git help --all
git config --unset 123@456.com

# local user
git config user.name "name"
git config user.email "mail"

# global user
git config --global user.name "name"
git config --global user.email "mail"

# commit editor
git config --global core.editor PathOfEditor

# set default init branch name
git config --global init.defaultBranch main

init

git-init - Create an empty Git repository or reinitialize an existing one

1
2
3
4
5
6
7
8
git init [-q | --quiet]
        [--bare]
        [--template=<template-directory>]
        [--separate-git-dir <git-dir>]
        [--object-format=<format>]
        [-b <branch-name> | --initial-branch=<branch-name>]
        [--shared[=<permissions>]]
        [<directory>]
  • –bare

Create a bare repository. If GIT_DIR environment is not set, it is set to the current working directory.

  • -b <branch-name> | –initial-branch=<branch-name>

Use the specified name for the initial branch in the newly created repository. If not specified, fall back to the default name (currently master, but this is subject to change in the future; the name can be customized via the init.defaultBranch configuration variable).

Examples

Start a new Git repository for an existing code base

1
2
3
4
$ cd /path/to/my/codebase
$ git init      #(1)Create a /path/to/my/codebase/.git directory.
$ git add .     #(2)Add all existing files to the index.
$ git commit    #(3)Record the pristine state as the first commit in the history.

status

git-status - Show the working tree status

Displays paths that have differences between the index file and the current HEAD commit, paths that have differences between the working tree and the index file, and paths in the working tree that are not tracked by Git (and are not ignored by gitignore(5)). The first are what you would commit by running git commit; the second and third are what you could commit by running git add before running git commit.

1
git status [<options>…​] [--] [<pathspec>…​]

clone

git-clone - Clone a repository into a new directory

1
2
3
4
5
6
7
8
9
git clone [--template=<template-directory>]
          [-l] [-s] [--no-hardlinks] [-q] [-n] [--bare] [--mirror]
          [-o <name>] [-b <name>] [-u <upload-pack>] [--reference <repository>]
          [--dissociate] [--separate-git-dir <git-dir>]
          [--depth <depth>] [--[no-]single-branch] [--no-tags]
          [--recurse-submodules[=<pathspec>]] [--[no-]shallow-submodules]
          [--[no-]remote-submodules] [--jobs <n>] [--sparse] [--[no-]reject-shallow]
          [--filter=<filter> [--also-filter-submodules]] [--] <repository>
          [<directory>]

add

git-add - Add file contents to the index (more)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
git add [--verbose | -v] 
        [--dry-run | -n]
        [--force | -f]
        [--interactive | -i]
        [--patch | -p]
        [--edit | -e] 
        [--[no-]all | --[no-]ignore-removal | [--update | -u]] [--sparse]
        [--intent-to-add | -N] 
        [--refresh] [--ignore-errors] 
        [--ignore-missing]
        [--renormalize]
        [--chmod=(+|-)x] 
        [--pathspec-from-file=<file> [--pathspec-file-nul]]
        [--] [<pathspec>…​]
  • git add filename
  • git add . (currunt directory)`

rm

git-rm - Remove files from the working tree and from the index (more)

1
2
3
4
5
6
7
8
git rm  [-f | --force] 
        [-n] 
        [-r] 
        [--cached] 
        [--ignore-unmatch]
        [--quiet] 
        [--pathspec-from-file=<file> [--pathspec-file-nul]]
        [--] [<pathspec>…​]

In the first form, it renames <source>, which must exist and be either a file, symlink or directory, to <destination>. In the second form, the last argument has to be an existing directory; the given sources will be moved into this directory. The index is updated after successful completion, but the change must still be committed.

  • git rm –cached filename

reset

git-reset - Reset current HEAD to the specified state

1
2
3
4
5
git reset [-q] [<tree-ish>] [--] <pathspec>…​
git reset [-q] [--pathspec-from-file=<file> [--pathspec-file-nul]] [<tree-ish>]
git reset (--patch | -p) [<tree-ish>] [--] [<pathspec>…​]
git reset [--soft | --mixed [-N] | --hard | --merge | --keep] [-q] [<commit>]
DEPRECATED: git reset [-q] [--stdin [-z]] [<tree-ish>]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
git reset [<mode>] [<commit>]

    This form resets the current branch head to <commit> and possibly updates the index (resetting it to the tree of <commit>) and the working tree depending on <mode>. If <mode> is omitted, defaults to --mixed. The <mode> must be one of the following:

    --soft
        Does not touch the index file or the working tree at all (but resets the head to <commit>, just like all modes do). This leaves all your changed files "Changes to be committed", as git status would put it.

    --mixed
        Resets the index but not the working tree (i.e., the changed files are preserved but not marked for commit) and reports what has not been updated. This is the default action.

        If -N is specified, removed paths are marked as intent-to-add (see git-add(1)).

    --hard
        Resets the index and working tree. Any changes to tracked files in the working tree since <commit> are discarded. Any untracked files or directories in the way of writing any tracked files are simply deleted.

    --merge
        Resets the index and updates the files in the working tree that are different between <commit> and HEAD, but keeps those which are different between the index and working tree (i.e. which have changes which have not been added). If a file that is different between <commit> and the index has unstaged changes, reset is aborted.

        In other words, --merge does something like a git read-tree -u -m <commit>, but carries forward unmerged index entries.

    --keep
        Resets index entries and updates files in the working tree that are different between <commit> and HEAD. If a file that is different between <commit> and HEAD has local changes, reset is aborted.

    --[no-]recurse-submodules
        When the working tree is updated, using --recurse-submodules will also recursively reset the working tree of all active submodules according to the commit recorded in the superproject, also setting the submodules' HEAD to be detached at that commit.


mv

git-mv - Move or rename a file, a directory, or a symlink(more)

1
2
git mv [-v] [-f] [-n] [-k] <source> <destination>
git mv [-v] [-f] [-n] [-k] <source> ... <destination directory>
  • git mv oldfile newfile

commit

git-commit - Record changes to the repository (more)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
git commit  [-a | --interactive | --patch] 
            [-s] 
            [-v] 
            [-u<mode>] 
            [--amend]
            [--dry-run] 
            [(-c | -C | --squash) <commit> | --fixup [(amend|reword):]<commit>)]
            [-F <file> | -m <msg>] 
            [--reset-author] 
            [--allow-empty]
            [--allow-empty-message] 
            [--no-verify] 
            [-e] 
            [--author=<author>]
            [--date=<date>] 
            [--cleanup=<mode>] 
            [--[no-]status]
            [-i | -o] 
            [--pathspec-from-file=<file> [--pathspec-file-nul]]
            [(--trailer <token>[(=|:)<value>])…​] 
            [-S[<keyid>]]
            [--] [<pathspec>…​]
  • git commit
  • git commit -a
  • git commit -am “commit message”
  • git commit -amend
  • git commit -v

log

git-log - Show commit logs (more)

1
git log [<options>] [<revision-range>] [[--] <path>…​]
  • git log origin..HEAD
  • git log HEAD ^origin
  • git log A B –not $(git merge-base –all A B)
  • git log A…B
  • git log –pretty=short

diff

git-diff - Show changes between commits, commit and working tree, etc (more)

1
2
3
4
5
6
git diff [<options>] [<commit>] [--] [<path>…​]
git diff [<options>] --cached [--merge-base] [<commit>] [--] [<path>…​]
git diff [<options>] [--merge-base] <commit> [<commit>…​] <commit> [--] [<path>…​]
git diff [<options>] <commit>…​<commit> [--] [<path>…​]
git diff [<options>] <blob> <blob>
git diff [<options>] --no-index [--] <path> <path>
  • git diff
  • git diff head

show

git-show - Show various types of objects (more)

1
git show [<options>] [<object>…​]
  • git show <commit id>

checkout

git-checkout - Switch branches or restore working tree files (more)

1
2
3
4
5
6
7
8
git checkout --help
git checkout [-q] [-f] [-m] [<branch>]
git checkout [-q] [-f] [-m] --detach [<branch>]
git checkout [-q] [-f] [-m] [--detach] <commit>
git checkout [-q] [-f] [-m] [[-b|-B|--orphan] <new-branch>] [<start-point>]
git checkout [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] [--] <pathspec>…​
git checkout [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] --pathspec-from-file=<file> [--pathspec-file-nul]
git checkout (-p|--patch) [<tree-ish>] [--] [<pathspec>…​]

remote

git-remote - Manage set of tracked repositories

1
2
3
4
5
6
7
8
9
10
11
12
13
git remote [-v | --verbose]
git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=(fetch|push)] <name> <URL>
git remote rename [--[no-]progress] <old> <new>
git remote remove <name>
git remote set-head <name> (-a | --auto | -d | --delete | <branch>)
git remote set-branches [--add] <name> <branch>…​
git remote get-url [--push] [--all] <name>
git remote set-url [--push] <name> <newurl> [<oldurl>]
git remote set-url --add [--push] <name> <newurl>
git remote set-url --delete [--push] <name> <URL>
git remote [-v | --verbose] show [-n] <name>…​
git remote prune [-n | --dry-run] <name>…​
git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)…​]

push

git-push - Update remote refs along with associated objects

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
git push [--all | --mirror | --tags] 
        [--follow-tags] 
        [--atomic] 
        [-n | --dry-run] [--receive-pack=<git-receive-pack>]
        [--repo=<repository>] 
        [-f | --force] 
        [-d | --delete] 
        [--prune] 
        [-v | --verbose]
        [-u | --set-upstream] 
        [-o <string> | --push-option=<string>]
        [--[no-]signed|--signed=(true|false|if-asked)]
        [--force-with-lease[=<refname>[:<expect>]]
        [--force-if-includes]]
        [--no-verify]
        [<repository> [<refspec>…​]]

pull

git-pull - Fetch from and integrate with another repository or a local branch

1
git pull [<options>] [<repository> [<refspec>…​]]

fetch

git-fetch - Download objects and refs from another repository

Fetch branches and/or tags (collectively, “refs”) from one or more other repositories, along with the objects necessary to complete their histories. Remote-tracking branches are updated (see the description of <refspec> below for ways to control this behavior).

By default, any tag that points into the histories being fetched is also fetched; the effect is to fetch tags that point at branches that you are interested in. This default behavior can be changed by using the –tags or –no-tags options or by configuring remote.<name>.tagOpt. By using a refspec that fetches tags explicitly, you can fetch tags that do not point into branches you are interested in as well.

1
2
3
4
git fetch [<options>] [<repository> [<refspec>…​]]
git fetch [<options>] <group>
git fetch --multiple [<options>] [(<repository> | <group>)…​]
git fetch --all [<options>]

merge

git-merge - Join two or more development histories together

1
2
3
4
5
6
git merge [-n] [--stat] [--no-commit] [--squash] [--[no-]edit]
        [--no-verify] [-s <strategy>] [-X <strategy-option>] [-S[<keyid>]]
        [--[no-]allow-unrelated-histories]
        [--[no-]rerere-autoupdate] [-m <msg>] [-F <file>]
        [--into-name <branch>] [<commit>…​]
git merge (--continue | --abort | --quit)

References

This post is licensed under CC BY 4.0 by the author.