The book continues from the above Git common commands - external system interaction commands
In this issue, let's take a look at the file operation instructions in the Git command. Let me explain some of the official terms of git first, otherwise you may have doubts when you read the summary
HEAD: A pointer to the repository, always pointing to the latest commit of the current branch
INDEX: The index of the repository, also known as stage space or cache space. This is the staging area of the Git repository, which can record every file added to Git version control by git add. After commit, the modification will be submitted to the current branch pointed to by HEAD, and HEAD will point to this submission.
working tree: Represents the workspace, which is the file system that the computer can be directly observed by us. HEAD and INDEX are stored in the .git hidden folder.
Order:
git branch: operate on git branches
overview:
usage: git branch [<options>] [-r | -a] [--merged] [--no-merged]
or: git branch [<options>] [-f] [--recurse-submodules] <branch-name> [<start-point>]
or: git branch [<options>] [-l] [<pattern>...]
or: git branch [<options>] [-r] (-d | -D) <branch-name>...
or: git branch [<options>] (-m | -M) [<old-branch>] <new-branch>
or: git branch [<options>] (-c | -C) [<old-branch>] <new-branch>
or: git branch [<options>] [-r | -a] [--points-at]
or: git branch [<options>] [-r | -a] [--format]
Generic options
-v, --verbose show hash and subject, give twice for upstream branch
-q, --quiet suppress informational messages
-t, --track[=(direct|inherit)] set branch tracking configuration
-u, --set-upstream-to <upstream> change the upstream info
--unset-upstream unset the upstream info
--color[=<when>] use colored output
-r, --remotes act on remote-tracking branches
--contains <commit> print only branches that contain the commit
--no-contains <commit> print only branches that don't contain the commit
--abbrev[=<n>] use <n> digits to display object names
Specific git-branch actions:
-a, --all list both remote-tracking and local branches
-d, --delete delete fully merged branch
-D delete branch (even if not merged)
-m, --move move/rename a branch and its reflog
-M move/rename a branch, even if target exists
-c, --copy copy a branch and its reflog
-C copy a branch, even if target exists
-l, --list list branch names
--show-current show current branch name
--create-reflog create the branch's reflog
--edit-description edit the description for the branch
-f, --force force creation, move/rename, deletion
--merged <commit> print only branches that are merged
--no-merged <commit> print only branches that are not merged
--column[=<style>] list branches in columns
--sort <key> field name to sort on
--points-at <object> print only branches of the object
-i, --ignore-case sorting and filtering are case insensitive
--recurse-submodules recurse through submodules
--format <format> format to use for the output
Common commands:
git branch #查看所有分支,当前分支前加*
git branch -r #查看远程分支
git branch -d branch_name/git branch -D branch_name #删除或强制删除分支branch_name
git branch -d -r remote_branch_name #删除远程分支
git branch -a #查看所有分支
git branch branch_name #创建名为branch_name的分支
git branch --set-upstream-to=remote_name/branch_name local_branch_name #创建本地分支与远程分支的关联关系
git branch -m old_branch_name new_branch_name / git branch -M old_branch_name new_branch_name #重命名或者强制重命名分支
git checkout: copy the file from the temporary storage area to the work area, discard the local modification or switch the branch, which will also cause the work area to be modified
overview:
usage: git checkout [<options>] <branch>
or: git checkout [<options>] [<branch>] -- <file>...
-b <branch> create and checkout a new branch
-B <branch> create/reset and checkout a branch
-l create reflog for new branch
--guess second guess 'git checkout <no-such-branch>' (default)
--overlay use overlay mode (default)
-q, --quiet suppress progress reporting
--recurse-submodules[=<checkout>] control recursive updating of submodules
--progress force progress reporting
-m, --merge perform a 3-way merge with the new branch
--conflict <style> conflict style (merge, diff3, or zdiff3)
-d, --detach detach HEAD at named commit
-t, --track[=(direct|inherit)] set branch tracking configuration
-f, --force force checkout (throw away local modifications)
--orphan <new-branch> new unparented branch
--overwrite-ignore update ignored files (default)
--ignore-other-worktrees do not check if another worktree is holding the given ref
-2, --ours checkout our version for unmerged files
-3, --theirs checkout their version for unmerged files
-p, --patch select hunks interactively
--ignore-skip-worktree-bits do not limit pathspecs to sparse entries only
--pathspec-from-file <file> read pathspec from file
--pathspec-file-nul with --pathspec-from-file, pathspec elements are separated with NUL character
Common commands:
git checkout branch_name #将本地当前分支切换至branch_name分支
git checkout -b branch_name remote_name/branch_name #迁出远端remote_name的branch_name分支到本地命名为branch_name,并且切换至branch_name
git checkout . #丢弃当前工作区所有未commit的修改
git checkout -- filename #丢弃当前工作区filename文件的修改
git reset, git revert: rollback operation of the repository
overview:
usage: git reset [--mixed | --soft | --hard | --merge | --keep] [-q] [<commit>]
or: git reset [-q] [<tree-ish>] [--] <pathspec>...
or: git reset [-q] [--pathspec-from-file [--pathspec-file-nul]] [<tree-ish>]
or: git reset --patch [<tree-ish>] [--] [<pathspec>...]
-q, --quiet be quiet, only report errors
--no-refresh skip refreshing the index after reset
--mixed reset HEAD and index
--soft reset only HEAD
--hard reset HEAD, index and working tree
--merge reset HEAD, index and working tree
--keep reset HEAD but keep local changes
--recurse-submodules[=<reset>] control recursive updating of submodules
-p, --patch select hunks interactively
-N, --intent-to-add record only the fact that removed paths will be added later
--pathspec-from-file <file> read pathspec from file
--pathspec-file-nul with --pathspec-from-file, pathspec elements are separated with NUL character
usage: git revert [<options>] <commit-ish>...
or: git revert <subcommand>
--quit end revert or cherry-pick sequence
--continue resume revert or cherry-pick sequence
--abort cancel revert or cherry-pick sequence
--skip skip current commit and continue
--cleanup <mode> how to strip spaces and #comments from message
-n, --no-commit don't automatically commit
-e, --edit edit the commit message
-s, --signoff add a Signed-off-by trailer
-m, --mainline <parent-number> select mainline parent
--rerere-autoupdate update the index with reused conflict resolution if possible
--strategy <strategy> merge strategy
-X, --strategy-option <option>·option for merge strategy
-S, --gpg-sign[=<key-id>] GPG sign commit
--reference use the 'reference' format to refer to commits
Common commands:
git reset --hard HEAD #将本地HEAD、INDEX、working tree回退至版本库最新版本
git reset --hard HEAD^ #将本地HEAD、INDEX、working tree回退至版本库最新版本的上个版本
git reset --hard HEAD~num #将本地HEAD、INDEX、working tree回退至版本库最新版本的前num个版本
git reset --hard remote_name #将本地HEAD、INDEX、working tree回退至远端版本库版本
#git revert用于撤销commit,然后重新提交一个操作commit。本身不会对其他的人提交commit产生影响。本质是删除某次提交之后的快照
git revert <commit> #撤销某个commit-id
git revert -n <commit> #撤销某个commit-id,但不进行自动commit
git revert -n <commit1>..<commitn> #撤销多个commit-id,但不进行自动commit(不包含commit1,但是包含commitn)
git revert -m parent-commit merge-commit #撤销一次merge commit parent-commit代表是以哪个分支为主。比如存在master、dev两个分支,当master-a(123456)的commit将dev-b(112233)的commit合并进来后,形成master-c。此时如果需要revert节点masert-c,我们需要确定parent-commit。通过指令git show master-c可以看到一个Merge行告知我们合并的节点,parent-commit则是节点从做往后顺序的数字。比如show之后显示merge:123456 112233,我们将master作为保留分支那么parent-commit为1,我们将dev作为保留分支那么parent-commit为2,如果有多个节点也是这样类推。
git diff: display differences between files, branches, and repository
overview:
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>
options:
-p -u --patch Generate patch (see section on generating patches). This is the default
-s --no-patch Suppress diff output. Useful for commands like git show that show the patch by default, or to cancel the effect of --patch.
-U<n> --unified=<n> Generate diffs with <n> lines of context instead of the usual three. Implies --patch.
--raw Generate the diff in raw format
Common commands:
git diff/git diff file #比较本地工作区和暂存区的区别
git diff --cached #比较暂存区和版本库的区别
git diff commit—id1 commit-id2/git diff branchname1 branchname2 #比较提交与分支之间的区别
git diff branch_name #比较当前工作区、暂存区与分支的区别
git merge: merge branches, resolve conflicts
overview:
usage: git merge [<options>] [<commit>...]
or: git merge --abort
or: git merge --continue
-n do not show a diffstat at the end of the merge
--stat show a diffstat at the end of the merge
--summary (synonym to --stat)
--log[=<n>] add (at most <n>) entries from shortlog to merge commit message
--squash create a single commit instead of doing a merge
--commit perform a commit if the merge succeeds (default)
-e, --edit edit message before committing
--cleanup <mode> how to strip spaces and #comments from message
--ff allow fast-forward (default)
--ff-only abort if fast-forward is not possible
--rerere-autoupdate update the index with reused conflict resolution if possible
--verify-signatures verify that the named commit has a valid GPG signature
-s, --strategy <strategy> merge strategy to use
-X, --strategy-option <option=value> option for selected merge strategy
-m, --message <message> merge commit message (for a non-fast-forward merge)
-F, --file <path> read message from file
--into-name <name> use <name> instead of the real target
-v, --verbose be more verbose
-q, --quiet be more quiet
--abort abort the current in-progress merge
--quit --abort but leave index and working tree alone
--continue continue the current in-progress merge
--allow-unrelated-histories allow merging unrelated histories
--progress force progress reporting
-S, --gpg-sign[=<key-id>] GPG sign commit
--autostash automatically stash/stash pop before and after
--overwrite-ignore update ignored files (default)
--signoff add a Signed-off-by trailer
--no-verify bypass pre-merge-commit and commit-msg hooks
Common commands:
git merge <commit> #将指定提交合并到当前分支
git merge <branchname> #将指定分支合并到当前分支
git merge <branchname1> <branchname2> #将指定分支合并到当前分支顶部
git rebase: rebase, that is, modify the base of a certain branch to the latest submission record of the current branch (it is recommended to use it less, rebase will cause the submission record to be lost, it is better to record the whole submission record of the branch than merge)
overview:
usage: git rebase [-i] [options] [--exec <cmd>] [--onto <newbase> | --keep-base] [<upstream> [<branch>]]
or: git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] --root [<branch>]
or: git rebase --continue | --abort | --skip | --edit-todo
--onto <revision> rebase onto given branch instead of upstream
--keep-base use the merge-base of upstream and branch as the current base
--no-verify allow pre-rebase hook to run
-q, --quiet be quiet. implies --no-stat
-v, --verbose display a diffstat of what changed upstream
-n, --no-stat do not show diffstat of what changed upstream
--signoff add a Signed-off-by trailer to each commit
--committer-date-is-author-date make committer date match author date
--reset-author-date ignore author date and use current date
-C <n> passed to 'git apply'
--ignore-whitespace ignore changes in whitespace
--whitespace <action> passed to 'git apply'
-f, --force-rebase cherry-pick all commits, even if unchanged
--no-ff cherry-pick all commits, even if unchanged
--continue continue
--skip skip current patch and continue
--abort abort and check out the original branch
--quit abort but keep HEAD where it is
--edit-todo edit the todo list during an interactive rebase
--show-current-patch show the patch file being applied or merged
--apply use apply strategies to rebase
-m, --merge use merging strategies to rebase
-i, --interactive let the user edit the list of commits to rebase
--rerere-autoupdate update the index with reused conflict resolution if possible
--empty <{drop,keep,ask}> how to handle commits that become empty
--autosquash move commits that begin with squash!/fixup! under -i
-S, --gpg-sign[=<key-id>] GPG-sign commits
--autostash automatically stash/stash pop before and after
-x, --exec <exec> add exec lines after each commit of the editable list
-r, --rebase-merges[=<mode>] try to rebase merges instead of skipping them
--fork-point use 'merge-base --fork-point' to refine upstream
-s, --strategy <strategy> use the given merge strategy
-X, --strategy-option <option> pass the argument through to the merge strategy
--root rebase all reachable commits up to the root(s)
--reschedule-failed-exec automatically re-schedule any `exec` that fails
--reapply-cherry-picks apply all changes, even those already present upstream
Common commands:
git rebase <branchname> #将目标分支变基到当前分支
git rebase -i HEAD~N #合并当前分支的多个提交记录到一个提交记录
git cherry-pick: select the commit to merge into the current branch (such as bug fix commit for urgent development)
overview:
usage: git cherry-pick [<options>] <commit-ish>...
or: git cherry-pick <subcommand>
--quit end revert or cherry-pick sequence
--continue resume revert or cherry-pick sequence
--abort cancel revert or cherry-pick sequence
--skip skip current commit and continue
--cleanup <mode> how to strip spaces and #comments from message
-n, --no-commit don't automatically commit
-e, --edit edit the commit message
-s, --signoff add a Signed-off-by trailer
-m, --mainline <parent-number>select mainline parent
--rerere-autoupdate update the index with reused conflict resolution if possible
--strategy <strategy> merge strategy
-X, --strategy-option <option> option for merge strategy
-S, --gpg-sign[=<key-id>] GPG sign commit
-x append commit name
--ff allow fast-forward
--allow-empty preserve initially empty commits
--allow-empty-message allow commits with empty messages
--keep-redundant-commits keep redundant, empty commits
Common commands:
git cherry-pick <commit-id> #选择特定commit合并到当前分支
git cherry_pick -x <commit-id1>...<commit-idn> #选择从commit-id1~commit-idn的提交合并到当前分支(不包含commit-id1)
4.git cherry-pick -x <commit-id1>^...<commit-idn> ##选择从commit-id1~commit-idn的提交合并到当前分支(包含commit-id1)
git stash: store the current workspace and temporary storage area in the git stack, and restore to the state of the latest submission
overview:
usage: git stash list [<options>]
or: git stash show [<options>] [<stash>]
or: git stash drop [-q|--quiet] [<stash>]
or: git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]
or: git stash branch <branchname> [<stash>]
or: git stash clear
or: git stash [push [-p|--patch] [-S|--staged] [-k|--[no-]keep-index] [-q|--quiet]
[-u|--include-untracked] [-a|--all] [-m|--message <message>]
[--pathspec-from-file=<file> [--pathspec-file-nul]]
[--] [<pathspec>...]]
or: git stash save [-p|--patch] [-S|--staged] [-k|--[no-]keep-index] [-q|--quiet]
[-u|--include-untracked] [-a|--all] [<message>]
Common commands:
git stash/git stash save "tag" #将当前的工作区和暂存区存放,缓存名称为最新一次提交的commit的id或者加了save参数后自定义的注释
git stash list #返回缓存的列表
git stash pop/git stash pop stash@{n} #将git栈中的最新缓存拿出来或者选定的stash@{n}缓存,然后删除本条缓存
git stash apply/git stash apply stash@{n} #将git栈中的最新缓存拿出来或者选定的stash@{n}缓存,不进行删除操作
git stash drop stash@{n} #删除某条缓存
git stash clear #删除所有缓存,慎用!!!
git stash -p show stash@{n} #显示某缓存与当前分支的差异
git stash branch <branch_name stash_id> #根据创建暂存时的提交创建一个新分支,并将暂存中的修改删除
To be continued~