工作区 -> 暂存区
1 2 3 4
| git add <文件名> git add <目录名>/ git add -A git add -u
|
从暂存区移除或到工作区
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| git reset <file> git reset git checkout -- <deleted-file> git reset --hard
git checkout -- <file>
git restore <file>
git stash
git stash pop
|
提交
1 2 3 4
| git commit -m "提交信息" git commit -a -m "提交信息"
|
远程仓库
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 27 28 29 30 31
| git remote add <本地对应名称> <远程仓库url> git remote -v git remote set-url <本地对应名称> <远程仓库url>
git branch -r git branch -vv
git pull <本地对应名称> <远程分支>
git fetch <本地对应名称>
git push <本地对应名称> <本地分支>
git switch -c <local-branch-name> <remote-name>/<remote-branch-name>
git branch --set-upstream-to=<remote-name>/<remote-branch-name> <local-branch-name>
git push -u origin <branch-name>
git checkout -b <local-branch-name> origin/<remote-branch-name>
|
分支操作
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 27 28 29 30
| git checkout --orphan new-empty-branch git checkout -b <branch-name> git branch <branch-name> git checkout <branch-name> git branch git branch -a git checkout <branch-name> git merge <source-branch> git branch -d <branch-name> git branch -D <branch-name> git push <remote-name> --delete <branch-name> git push <remote-name> --delete --force <branch-name>
git branch -m <new-branch-name> git branch -m <old-branch-name> <new-branch-name>
git log git log <branch-name>
git diff <branch-name> git diff <branch1>..<branch2>
git rebase <target-branch>
|
冲突处理
所谓冲突 指成为新的提交时有同一个文件的不同修改
本地分支merge
输出提示
1 2 3
| Auto-merging file.txt CONFLICT (content): Merge conflict in file.txt Automatic merge failed; fix conflicts and then commit the result.
|
此外,可以通过以下命令查看哪些文件存在冲突:
手动解决冲突,打开冲突文件
1 2 3 4 5
| <<<<<<< HEAD 这是主分支的修改 ======= 这是合并分支的修改 >>>>>>> feature
|
解决冲突后,使用以下分支完成合并
解决冲突的过程中遇到问题,或者决定放弃合并,可以运行以下命令:
push远程分支
1.git fetch远程分支到本地
2.合并远程分支到本地分支
3.解决冲突
4.生成新的提交
5.推送
reset
–soft
行为:
将当前分支的提交历史回退到指定的提交,但保留之后的提交所做的更改在暂存区中。
结果:
提交历史被回退。
暂存区包含被回退的提交的更改。
工作区保持不变。
–mixed(默认模式)
行为:
将当前分支的提交历史回退到指定的提交,保留之后的提交所做的更改在工作区中,但取消暂存。
结果:
提交历史被回退。
工作区包含被回退的提交的更改。
暂存区被清空。
–hard
行为:
将当前分支的提交历史、暂存区和工作区都回退到指定的提交的状态。
结果:
提交历史被回退。
暂存区被清空。
工作区被回退到指定提交的状态,所有之后的更改都会被丢弃。
something
将工作区设置为历史某一次提交
方法 1:使用 git checkout
使用 git checkout 可以将工作区设置为历史上的某一次提交的状态。这种方法不会修改当前分支的提交历史,只是将工作区的内容更新为指定提交的状态(使用之前最好提交了修改)
方法 2:使用 git reset
如果你希望将当前分支的提交历史和工作区都回退到历史上的某一次提交的状态,可以使用 git reset 命令。这种方法会修改当前分支的提交历史