Source
Common Git Command Line Operation | Chanvin's Blog (chanvinxiao.com)
This article record the specific usage method of some common git command line operation
本文记录了一些常用 git 命令行操作的具体使用方法
git clone
拉取git项目到本地。
git clone REPOSITORY_URLClone repository, and use the name of repository as local folder name 克隆版本库,并使用版本库名称作为本地文件夹名称git clone REPOSITORY_URL FOLDERClone repository, and use FOLDER as local folder name 克隆存储库,并使用 FOLDER 作为本地文件夹名称
git fetch
git fetch originUpdate all the remote branch 更新所有远程分支git fetch origin BRACHUpdate designated remote branch 更新指定的远程分支
git pull
git pull originEquivalent tofetchmergecoresponding upstream branch 把分支推到远端对应的上游分支git pull origin BRACHPull designated branch to current branch 等同于fetchmerge对应上游分支git pull origin --rebase masterMake local branch rebase remote master branch 让本地分支重定向远程主分支
git push
git push originPush branch to coresponding remote upstream branch 将分支推送到对应的远程上游分支git push origin BRANCHPush branch to remote designated branch 将分支推送到远程指定分支git push --set-upstream origin BRANCHPush branch to remote designated branch, and make it as upstream branch (generally need to be used for pushing branch of your own for the first time) 将分支推送到远程指定分支,并使其成为上游分支(一般用于首次推送自己的分支)git push -f originForce push branch to corresponding remote upstream branch (will override remote branch, need to be used carefully) 将分支推送到远程指定分支,并使其成为上游分支(一般用于首次推送自己的分支)git push origin -d BRANCHDelete remote branch 删除远程分支
git branch
git branchList all local branch 删除远程分支git branch -aList all local and remote branch 列出本地和远程分支git branch -m NEW_BRANCHRename current branch 重命名当前分支git branch -d BRANCHDelete merged branch 删除已合并的分支git branch -D BRANCHForce delete branch (even if not be merged yet) 强制删除分支(即使未合并)
git checkout
git checkout BRANCHSwitch to designated branch 切到对应分支git checkout -b NEW_BRANCHCreate new branch 创建新分支git checkout -b NEW_BRANCH BRANCHCreate new branch based on BRANCH 基于 BRANCH 创建新分支git checkout SHA-1Switch to a commit, or use HEAD~N (N as 1, 2, 3…) to switch to previous Nth commit 切换到某个提交,也可以用 HEAD~N(N 为 1, 2, 3…)切到上 N 个提交git checkout SHA-1 /PATH/TO/FILERestore file to designated commit version 把文件还原到相应的提交版本git checkout --theirs /PATH/TO/FILEUse theirs’ file version in case of conflict 有冲突时使用对方的文件版本git checkout --ours /PATH/TO/FILEUse ours’ file version in case of conflict 有冲突时使用自己的文件版本git checkout -Switch to previous branch, suitable for switching frequently between two branches 切换到之前的分支,适合在两个分支频繁切换时使用
git add
git add .Mark all added / modified / deleted files as to-be-committed 把所有增加/修改/删除的文件标识为要提交git add /PATH/TO/FILEMark a single file as to-be-committed, suitable for situation when there’re other modified files which don’t need to be committed 只把单一文件标识为要提交,当有其他不需要提交的文件被修改时可使用
git commit
git commitCommit files marked bygit add把git add标识的文件进行提交git commit -aCommit modified / deleted files (if there’s newly added file, need to usegit addto mark firstly) 把修改/删除的文件进行提交(如果有新增的文件,需要使用git add添加)git commit -am "MESSAGE"Commit modified / deleted files and assign comment (suitable for temporary or simple comment content) 把修改/删除的文件进行提交并指定注释(适用于临时或简单注释内容)git commit --amendUpdate last commit, can add-aor rungit addfirstly to append updated files 更新上一次提交,可以加上-a或在之前运行git add追加更新文件git commit --amend --reset-authorDefault updating commit won’t change author, can explicitly config if necessary 默认的更新提交是不改变作者的,如果需要改变可以明确配置
git cherry-pick
git cherry-pick SHA-1Apply a commit to current branch 把某个提交应用到当前分支
git status
git statusCheck current status 查看目前状态
git diff
git diffUpdating contents of current modified files which has not been marked as to-be-committed 当前所有修改到的,没被标识为要提交的文件的更新内容git diff --cacheUpdating contents of current modified files which has been marked as to-be-committed 当前所有修改到的,并被标识为要提交的文件的更新内容git diff /PATH/TO/FILEUpdating contents of designated file, and can also be distinguished with--cache指定文件的更新内容,同样可以用--cache区分
git log
git logShow all logs in detail 详细显示所有记录git log -n 10Show latest 10 logs 显示最近 10 条记录git log --onelineShow all logs briefly 简要显示所有记录git log --oneline master ^BRANCH | wc -lCompute how much commit differences between BRANCH and master 可以计算 BRANCH 和 master 分支相差多少个提交
git stash
git stashStash modified / deleted files, and the added files marked as to-be-committed 暂存修改/删除,或已标识为要 commit 的新增的文件git stash -uStash modified / deleted / added files, which means the added files is included without usinggit add暂存修改/删除/新增的文件,即新增文件可以不用git addgit stash popPop the stashed files 把暂存的文件重新放出来
git revert
git revert SHA-1Cancel a commit by forming a new commit 通过形成一个新提交取消某个提交git revert SHA-1 -m 1If the to-be-reverted commit is a merged one, need to designate which parent commit to be reverted to For example, if the merged commit is merging from BRANCH_2 to BRANCH_1, and you want to revert to BRANCH_1, then m should be 1 (it’s the most cases) 如果是合并节点,需要指定要取消提交对应的父节点,例如合并是把 BRANCH_2 合并到 BRANCH_1,那么要在 BRANCH_1 取消这次合并,就应该指定 m 为 1(大多数情况都是这样)
git reset
git resetCancel marking for to-be-committed files (equivalent to withdrawinggit add) 取消对要 commit 的文件的标识(相当于git add的撤销)git reset --hardCancel updating for modified / deleted files and added files marked as to-be-committed 取消修改/删除或已标识为要 commit 的新增的文件的更新git reset SHA-1Cancel all the commits after SHA-1, but retain updates of the committed files If want to just cancel last commit, SHA-1 can be set asHEAD^取消从 SHA-1 之后的所有提交,但是保留提交文件的更新,如果只想取消上一次提交,SHA-1 可以设为HEAD^git reset --hard SHA-1Cancel all the commits after SHA-1, and don’t retain updates of the committed files 更新 SHA-1 以后的提交,可以pick/p,edit/e,drop/d,squash/s相应提交,如果第一个提交使用p,后面的提交使用s,可以把多个提交合并成一个提交
git rebase
git rebase BRANCHMake current branch rebase BRANCH 让当前分支重新基于 BRANCHgit rebase -i SHA-1Update commits after SHA-1, canpick/p,edit/e,drop/d,squash/scorresponding commits If the first commit used withp, and the following commit used withs, then multiple commits will join into a single commit 更新 SHA-1 以后的提交,可以pick/p,edit/e,drop/d,squash/s相应提交,如果第一个提交使用p,后面的提交使用s,可以把多个提交合并成一个提交
git merge
git merge BRANCHMerge BRANCH into current branch, try not to form merged commit 把 BRANCH 合并到当前分支,尽量不形成合并节点git merge --no-ff BRANCHMerge BRANCH into current branch, and make sure to form merged commit 把 BRANCH 合并到当前分支,并确保形成合并节点git merge --squash BRANCHMake the differences between BRANCH and the current branch as to-be-committed contents, need to rungit committo complete merging with only one commit 把 BRANCH 和当前分支的变更作为标识为要提交的内容,需要运行git commit完成只有一个提交的合并
git update-index
git update-index --assume-unchanged /PATH/TO/FILEWhen a file is modified temporary, but don’t want to be committed, and not suitable to be added to.gitignore, can use this command to makegitdon’t recognize it as modified Cannot use this command if the file is newly added, but can add the file path to.git/info/exclude当某个文件被临时修改,但不想提交,也不适合放到.gitignore,可以用此命令让git不将其识别为已修改 如果这个文件是新增的,就不能用这个命令了,不过可以把文件路径加到.git/info/excludegit update-index --no-assume-unchanged /PATH/TO/FILERecover modified recognition for the designated file 恢复以上文件的修改识别


