美文网首页
git 命令

git 命令

作者: 武曌思 | 来源:发表于2018-07-19 00:21 被阅读0次

    特别感谢 @阮一峰 老师 的博客、@易百 Git 教程 

    推荐一个特别有趣的学习 git 的网站 LearnGit

    一、初始化和克隆

    git init:使用当前目录作为一个仓库

    git init [dir]:在当前目录下新建 dir 目录作为一个仓库

    git remote add [remotename] [address]:为本地仓库添加一个远端仓库

    git remote -v:列表显示当前仓库所有远端仓库及地址,可不加 -v 参数

    git remote rm [remotename]:删除远端仓库的追踪

    git remote show [remotename]:查看远端仓库详细信息,包括本地、远端分支及其追踪情况

    git clone [url] [dir]:克隆一个仓库到当前目录,使用仓库名作为文件夹名。如果有dir参数,就会将项目克隆到指定目录。

    二、暂存、取消暂存和提交

    git add [file1] [file2] ...:添加指定文件到暂存区

    git add [dir]:添加指定目录到暂存区,包括子目录

    git add .:添加当前目录及其子目录的变更文件到缓存区

    git reset [file1] [file2]:取消暂存的文件,恢复到工作区。如果省略文件名,则全部恢复到工作区

    git commit -m [message]:提交暂存区到仓库,提交信息为message

    git commit --amend -m [message]:替换(更新)上次提交

    git commit -a -m [message]:将工作区和缓存区的文件全部提交,省去了 git add命令,提交的文件仅包括追踪的文件。

    三、分支的新建、查看、切换、删除和追踪

    git branch [branchname] [commitid]:以 commitid 的提交作为原型,新建一个分支,但仍处于当前分支

    git branch [branchname] [branchname1]:以某一分支为原型,新建分支,仍处于当前分支。如果是远端分支,则自动建立追踪关系。如果省略 branchname1,默认为当前分支

    git branch -m [old] [new]:重命名分支

    git branch:查看本地所有分支

    git branch -r:查看远端所有分支

    git branch -a:查看所有(本地和远端)分支

    git branch -v:查看本地分支与其追踪关系

    git checkout [branchname]:切换分支

    git checkout -b [branchname] [branchname1]:以某分支为原型,新建一个分支,并切换到新分支。如果是远端分支,则自动建立追踪关系。如果省略 branchname1,默认为当前分支。

    git checkout -t [remote]/[branchname]:以远端分支为原型,新建一个与远端分支名相同的分支,并切换到新分支,同时建立追踪关系

    git branch -d [branchname]:删除本地分支

    git branch -dr [remotename]/[branchname]:删除远端分支

    git branch -u [remote]/[branchename] [localbranchname]:设置本地分支追踪到远端分支,如果省略 localbranchname,默认为当前分支;-u 可以使用 --track 替换

    git branch --unset-upstream:取消追踪远端分支

    四、撤销提交

    git revert [branch/commitid/HEAD]~<回退次数>:新建一个提交来撤销一个提交

    git reset <option> HEAD~<回退次数>:将当前分支撤销几个提交,如果一次,可省略数字1

    git reset <option> <commitid>:重置提交到某次提交,可以向前,也可以向后

    option 详解:

    --soft:将提交内容恢复到暂存区,工作区不变

    --mixed(默认):将提交内容恢复到工作区,暂存区清空

    --hard:重置到指定提交,工作区和暂存区同时清空

    五、合并

    git cherry-pick [commit] ...:选择一个或多个 commit,合并进当前分支,合并的是变化

    git cherry-pick [start-commit-id]..[end-commit-id]:合并两个 commit 之间的提交,左开右闭,即不包括 [start-commit]

    git cherry-pick [start-commit-id]^..[end-commit-id]:合并两个 commit 之间的提交,闭区间,包括 [start-commit]

    git rebase [basebranch] [feturebranch]:将 feturebranch 的提交应用到 basebranch 分支。如果 feturebranch 缺省,默认为当前分支;如果两者都缺省,则与当前分支追踪的远端分支进行变基;如果当前分支未追踪远端分支,则变基失败

    注意:rebase 可能会在某一个提交处产生冲突,此时会停在这一状态下,让你去解决冲突。

    (1)解决完,使用 git rebase --continue,继续变基

    (2)如果 git rebase --continue 无效,可以使用 git rebase --skip

    (3)如果不想继续变基,可以使用 git rebase --abort,终止变基

    git merge [branchname]:将当前分支与 branchname 分支进行合并。如果缺省,则与其追踪的远端分支进行合并。如果未追踪远端分支,则合并失败

    如果发生冲突,可使用 git merge --abort,终止合并

    rebase 和 merge 区别:如图

    rebase 和 merge 区别

    六、远程仓库操作

    git fetch [remote] [branchname]:[branchname1]:取回远端仓库 remote 的 branchname 分支上的更新,放到本地数据库,更新 branchname1,但不会与本地分支进行合并。

    (1)如果缺省 branchname1,则更新本地 remote/branchname;

    (2)如果缺省 branchname,则取回所有分支的更新;

    (3)如果全部缺省,则取回本地仓库 .git/config 中指定的远端仓库所有分支的更新。

    .git/config 文件对远程仓库的配置

    git pull [remote] [remotebranch]:[localbranch]

    (1)该命令是 git fetch 和 git merge 的合并操作,参数解析同 git fetch;

    (2)merge 是 HEAD 和 localbranch 的合并;

    (3)如不想采用 merge 合并,可使用 git pull --rebase

    git push [remote] [localbranch]:[remotebranch]:将本地分支 localbranch 推送到 remote 仓库的 remotebranch 分支上。

    (1)如果缺省 remotebranch,则默认与 localbranch 同名分支;若远端没有该分支,则会新建远端分支;

    (2)如果缺省两个分支,则推送当前分支到其追踪的 remote 仓库中的远端分支;若没有则新建;

    (3)如果缺省 remote,则根据 config 中配置进行 push;

    (4)如果本地分支与远端分支有冲突,且不要解决冲突,以本地分支为准,则加 --force 参数,强推至远端。

    git push [remote] --delete [branchname]:删除远端分支。

    git push [remote] :[branchname]:删除远端分支。


    七、打补丁

    https://blog.csdn.net/liuhaomatou/article/details/54410361

    https://blog.csdn.net/Qidi_Huang/article/details/61920472

    八、日志

    https://juejin.im/post/5a65ac67f265da3e330473f7

    九、文件操作

    git checkout -- [filename]:丢弃文件修改

    git reset --hard HEAD [filename]:丢弃文件修改

    git rm --cached [file]:停止追踪指定文件,但该文件会保留在工作区

    相关文章

      网友评论

          本文标题:git 命令

          本文链接:https://www.haomeiwen.com/subject/iczfpftx.html