git commands manual

  • let git manage a code repository:
    git init

  • stage modified files or add untracked files to git repo:
    git add <files>

  • stage all tracked files(this is very useful especially if you have multiple files to be staged):
    git add -u

  • commit staged files:
    git commit -m '<commit explanation>'

  • skip staging step, automatically add tracked files to staging area then commit them:
    git commit -am '<commit explanation>'

  • check current status of current branch without showing untracked files:
    git status -uno

  • show changes you have made but not yet staged:
    git diff

  • show staged changes compared to last commit:
    git diff --cached (or git diff --staged)

  • remove a file both from git and from disk:
    git rm <files>

  • remove a file from git only(they will become untracked files, kept on your disk):
    git rm --cached <files>

  • move a file in working directory:
    git mv <file_from> <file_to>

  • show commit history:
    git log
    use -p option to show commit changes, use -num option to limit the number of commits to be displayed, examples:
    git log -p -5
    git log options

    option explanation
    -p Show the patch introduced with each commit.
    --stat Show statistics 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 Show only the first few characters of the SHA-1 checksum instead of all 40.
    --relative-date Display the date in a relative format (for example, weeks ago」) instead of using the full date format.
    --graph Display an ASCII graph of the branch and merge history beside the log output.
    --pretty Show commits in an alternate format. Options include oneline, short, full, fuller, and format (where you specify your own format).
  • redo last commit(if you don't make any changes before running this command, then only the commit message is changed):
    git commit --amend

  • unstaging staged files:
    git reset HEAD <files>

  • unmodifying modified files:
    git checkout -- <files>

  • show remote repo:
    git remote -v

  • add remote repo:
    git remote add <shortname> <url>

  • fetch commits from remote repo:
    git fetch <repository-name>

  • push local branch to remote repo with same name.
    git push <remote-name> <branch-name>
    you can specify a different remote name with: git push origin local-name:remote-name

  • show remote repo info:
    git remote show <remote-name>

  • rename remote repo name referenced in local(this command only changes the remote repo name):
    git remote rename <remote-from> <remote-to>

  • remove a remote repo referenced in local(this command won't delete remote repo):
    git remote rm <remote-name>

  • create a new branch based on current commit:
    git branch <branch-name>

  • switch to an existing branch:
    git checkout <branch-name>(or git switch <branch-name>)

  • combine the two above commands in one(create a new branch then switch to it):
    git checkout -b <branch-name>(or git switch -c <branch-name>)

  • merge another branch to current branch:
    git merge <branch-name>

  • delete a branch(use -D to force delete)
    git branch -d <branch-name>

  • call external GUI tool to resolve conflicts during merging(or just open VS Code, it has built-in support for git):
    git mergetool

  • show last commit of each branch:
    git branch -v

  • show branches that have been merged to current branch:
    git branch --merged

  • show branches that have not been merged to current branch:
    git branch --no-merged

  • create a branch and switch to it, and set it to track a remote branch:
    git checkout -b <branch-name> <remote-name>/<branch-name>
    if you would like the local branch name to be the same with remote branch name, just use this command:
    git checkout --track <remote-name>/<branch-name>

  • setup local branch to track a remote branch:
    git branch -u <remote-name>/<branch-name>
    (git branch --set-upstream-to <remote-name>/<branch-name>)

  • show all branches with its upstream info:
    git branch -vv

  • delete a remote branch:
    git push <remote-name> --delete <branch-name>
    (or git push <remote-name> :<branch-name>)

  • update others' changes to your current branch:
    git rebase <base-branch> <topic-branch>(e.g.update local code: git rebase upstream/master master)
    advanced usage:
    git rebase --onto <base-branch> <another-branch> <topic-branch>(here branch can also be commit ID)
    It means "Take topic branch, figure out the patches since it diverged from another branch, and replay these patches in the topic branch as if it was based directly off the base branch instead."
    Notes: this command has the same effect with cherry-pick when you want to cherry-pick multiple commits but takes less typing and seems pretty cool.

  • merge one commit to your current branch(this will generate a new commit in your current branch):
    git cherry-pick <SHA-1>

  • cherry-pick without commit(just patch the changes, don't commit nor stage):
    git cherry-pick -n <SHA-1>

  • cherry-pick a range of commits:
    git cherry-pick A..B
    Including important comments (credits to respective authors)

    • Note 1: In the "cherry-pick A..B" form, A should be older than B. If they're the wrong order the command will silently fail. – damian
    • Note 2: Also, this will not cherry-pick A, but rather everything after A up to and including B. – J. B. Rainsberger
    • Note 3: To include A just type git cherry-pick A^..B – sschaef
  • revert one file's change in a commit:
    git show <SHA-1> -- <file-name> | git apply -R

  • create a patch based on a specific commit:
    git format-patch -1 <SHA-1> (-1 is number 1, means one commit)

  • create a patch that is svn compatible:
    git diff --no-prefix old-commit new-commit > your.diff

  • git reset 3 options:

    • git reset --soft <SHA-1>, only HEAD points to <SHA-1>, Index and working directory not changed.
    • git reset --mixed <SHA-1>, HEAD points to <SHA-1>, and Index is the same with HEAD, working directory not changed. This is the default option.
    • git reset --hard <SHA-1>, HEAD points to <SHA-1>, Index and working directory is the same with HEAD.

    Check Reset Demystified section in <<Pro Git>> for more details.

  • revert one commit(this will generate a new commit in your current branch):
    git revert <SHA-1>


郵箱地址不會被公開。 必填項已用*標註