美文网首页
git 的使用场景举例

git 的使用场景举例

作者: Reiser实验室 | 来源:发表于2019-01-18 10:28 被阅读45次

    使用 git 很长一段时间了,觉得干巴巴的指令集合用处真心不大,结合场景使用才能理解的更深入(虽然也可能更片面)。下面是我整理的常见的场景:

    撤销

    1. 没有add,撤销工作区的更改

    git checkout fileName(从暂缓区恢复到工作区)
    

    2. 已经add,撤销缓存区的更改

    git reset fileName
    

    3. 已经commit,覆盖提交

     git commit —amend(修改文件为正确的之后,add之后)
    

    4. 已经commit,撤销提交

    git reset --hard fileName(—hard 使得工作区也会被撤销到以前版本)
    

    5. 已经push且在远程分支上,本地撤销提交后强制提交

    git push origin branch1 -f
    

    线上提交代码

    1. Clone自己的项目

    git clone git@github.com:leonzone/test.git
    git push origin master
    

    2. 关联本地已有项目

    git remote add origin 
    git@github.com:leonzone/test.git
    git push -u origin master(-u 以后默认push到origin)
    

    远程分支

    1. 新建远程分支

    git check -b develop
    git push origin develop: develop
    

    2. 同步代码到远程分支

    git push origin develop
    

    3. 同步远程分支到本地

    git checkout -b develop origin/develop
    

    添加忽略

    .gitignore只能忽略那些原来没有被track的文件,如果某些文件已经被纳入了版本管理中,则修改.gitignore是无效的。那么解决方法就是先把本地缓存删除(改变成未track状态),然后再提交:

    git rm -r –-cached filePath 
    //添加 filePath 到 .gitignore
    git commit -m “remove xx” 
    

    查看变动

    1. 查看历史中的多个 commit:log
      1. 查看详细改动: git log -p
      2. 查看大致改动:git log --stat
    2. 查看具体某个 commit:show
      1. 要看最新 commit ,直接输入 git show ;要看指定 commit ,输入 git show commit的引用或SHA-1
      2. 如果还要指定文件,在 git show 的最后加上文件名
    3. 查看未提交的内容:diff
      1. 查看暂存区和上一条 commit 的区别:git diff --staged(或 --cached)
      2. 查看工作目录和暂存区的区别:git diff 不加选项参数
      3. 查看工作目录和上一条 commit 的区别:git diff HEAD

    相关文章

      网友评论

          本文标题:git 的使用场景举例

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