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项目到本地。
git clone REPOSITORY_URL
Clone repository, and use the name of repository as local folder name
克隆版本库,并使用版本库名称作为本地文件夹名称git clone REPOSITORY_URL FOLDER
Clone repository, and use FOLDER as local folder name
克隆存储库,并使用 FOLDER 作为本地文件夹名称git fetch origin
Update all the remote branch
更新所有远程分支git fetch origin BRACH
Update designated remote branch
更新指定的远程分支git pull origin
Equivalent to fetch
+ merge
coresponding upstream branch
把分支推到远端对应的上游分支git pull origin BRACH
Pull designated branch to current branch
等同于 fetch
+ merge
对应上游分支git pull origin --rebase master
Make local branch rebase remote master branch
让本地分支重定向远程主分支git push origin
Push branch to coresponding remote upstream branch
将分支推送到对应的远程上游分支git push origin BRANCH
Push branch to remote designated branch
将分支推送到远程指定分支git push --set-upstream origin BRANCH
Push 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 origin
Force push branch to corresponding remote upstream branch (will override remote branch, need to be used carefully)
将分支推送到远程指定分支,并使其成为上游分支(一般用于首次推送自己的分支)git push origin -d BRANCH
Delete remote branch
删除远程分支git branch
List all local branch
删除远程分支git branch -a
List all local and remote branch
列出本地和远程分支git branch -m NEW_BRANCH
Rename current branch
重命名当前分支git branch -d BRANCH
Delete merged branch
删除已合并的分支git branch -D BRANCH
Force delete branch (even if not be merged yet)
强制删除分支(即使未合并)git checkout BRANCH
Switch to designated branch
切到对应分支git checkout -b NEW_BRANCH
Create new branch
创建新分支git checkout -b NEW_BRANCH BRANCH
Create new branch based on BRANCH
基于 BRANCH 创建新分支git checkout SHA-1
Switch 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/FILE
Restore file to designated commit version
把文件还原到相应的提交版本git checkout --theirs /PATH/TO/FILE
Use theirs’ file version in case of conflict
有冲突时使用对方的文件版本git checkout --ours /PATH/TO/FILE
Use ours’ file version in case of conflict
有冲突时使用自己的文件版本git checkout -
Switch to previous branch, suitable for switching frequently between two branches
切换到之前的分支,适合在两个分支频繁切换时使用git add .
Mark all added / modified / deleted files as to-be-committed
把所有增加/修改/删除的文件标识为要提交git add /PATH/TO/FILE
Mark 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
Commit files marked by git add
把 git add
标识的文件进行提交git commit -a
Commit modified / deleted files (if there’s newly added file, need to use git add
to mark firstly)
把修改/删除的文件进行提交(如果有新增的文件,需要使用 git add
添加)git commit -am "MESSAGE"
Commit modified / deleted files and assign comment (suitable for temporary or simple comment content)
把修改/删除的文件进行提交并指定注释(适用于临时或简单注释内容)git commit --amend
Update last commit, can add -a
or run git add
firstly to append updated files
更新上一次提交,可以加上 -a
或在之前运行 git add
追加更新文件git commit --amend --reset-author
Default updating commit won’t change author, can explicitly config if necessary
默认的更新提交是不改变作者的,如果需要改变可以明确配置git cherry-pick SHA-1
Apply a commit to current branch
把某个提交应用到当前分支git status
Check current status
查看目前状态git diff
Updating contents of current modified files which has not been marked as to-be-committed
当前所有修改到的,没被标识为要提交的文件的更新内容git diff --cache
Updating contents of current modified files which has been marked as to-be-committed
当前所有修改到的,并被标识为要提交的文件的更新内容git diff /PATH/TO/FILE
Updating contents of designated file, and can also be distinguished with --cache
指定文件的更新内容,同样可以用 --cache
区分git log
Show all logs in detail
详细显示所有记录git log -n 10
Show latest 10 logs
显示最近 10 条记录git log --oneline
Show all logs briefly
简要显示所有记录git log --oneline master ^BRANCH | wc -l
Compute how much commit differences between BRANCH and master
可以计算 BRANCH 和 master 分支相差多少个提交git stash
Stash modified / deleted files, and the added files marked as to-be-committed
暂存修改/删除,或已标识为要 commit 的新增的文件git stash -u
Stash modified / deleted / added files, which means the added files is included without using git add
暂存修改/删除/新增的文件,即新增文件可以不用 git add
git stash pop
Pop the stashed files
把暂存的文件重新放出来git revert SHA-1
Cancel a commit by forming a new commit
通过形成一个新提交取消某个提交git revert SHA-1 -m 1
If 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
Cancel marking for to-be-committed files (equivalent to withdrawing git add
)
取消对要 commit 的文件的标识(相当于 git add
的撤销)git reset --hard
Cancel updating for modified / deleted files and added files marked as to-be-committed
取消修改/删除或已标识为要 commit 的新增的文件的更新git reset SHA-1
Cancel 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 as HEAD^
取消从 SHA-1 之后的所有提交,但是保留提交文件的更新,如果只想取消上一次提交,SHA-1 可以设为 HEAD^
git reset --hard SHA-1
Cancel 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 BRANCH
Make current branch rebase BRANCH
让当前分支重新基于 BRANCHgit rebase -i SHA-1
Update commits after SHA-1, can pick/p
, edit/e
, drop/d
, squash/s
corresponding commits
If the first commit used with p
, and the following commit used with s
, then multiple commits will join into a single commit
更新 SHA-1 以后的提交,可以 pick/p
,edit/e
,drop/d
,squash/s
相应提交,如果第一个提交使用 p
,后面的提交使用 s
,可以把多个提交合并成一个提交git merge BRANCH
Merge BRANCH into current branch, try not to form merged commit
把 BRANCH 合并到当前分支,尽量不形成合并节点git merge --no-ff BRANCH
Merge BRANCH into current branch, and make sure to form merged commit
把 BRANCH 合并到当前分支,并确保形成合并节点git merge --squash BRANCH
Make the differences between BRANCH and the current branch as to-be-committed contents, need to run git commit
to complete merging with only one commit
把 BRANCH 和当前分支的变更作为标识为要提交的内容,需要运行 git commit
完成只有一个提交的合并git update-index --assume-unchanged /PATH/TO/FILE
When 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 make git
don’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/exclude
git update-index --no-assume-unchanged /PATH/TO/FILE
Recover modified recognition for the designated file
恢复以上文件的修改识别