ubuntu~git学习

推荐:廖雪峰~git教程

  https://git-scm.com/book/en/v2

1. 新建知识库

  git init

2. git 添加

  • git add file 
  • git add 文件夹   ~git添加整个文件夹及内容
  • git add -A 提交所有变化
  • git add -u 提交被修改(modified)和被删除(deleted)文件,不包括新文件(new)
  • git add . 提交新文件(new)和被修改(modified)文件,不包括被删除(deleted)文件

3. kill git进程:ctrl + c,而不是command + c

4. git版本回退reset和版本合并rebase(可以用于修改git commit记录)

  (1) 如果只是单纯地修改commit information备注信息,不涉及版本问题,我们可以使用git commit --amend命令

  (2) 如果我们按时间顺序将最近的几个git commit repo版本合并,可以使用git reset版本回退命令。

    • https://www.jianshu.com/p/6c822790b81d
    • 软回退:git reset --soft commit-id,这会将repo回退到某个版本,只回退commit的信息,不会回退工作区、缓存区cache的代码状态。如果还要提交,直接commit即可。 
    • 硬回退:git reset --hard:彻底回退到某个版本、并按时间顺序抛弃改版本后面的版本,本地、工作区、缓存区的源码也会完全变为上一个版本的内容。
    • 错误:push失败,因为本地repo commit版本比remote repo少几个版本。    

      error: failed to push some refs to 'git@github.com:yuyongsheng1990/yuyongsheng1990.github.io.git'

      hint: Updates were rejected because the tip of your current branch is behind

      解决:git push -u origin master -f(这条push命令会强制覆盖远程仓库之前的所有命令

   (3) 如果我们按时间顺序将最近的几个commit版本合并到之前的某个版本中,则使用rebase版本合并。

    • https://www.jianshu.com/p/12803dba313e

    git rebase -i [branch|<commit>]

      你可以直接进入某个分支的 rebase 也可以进入某次 commit 的 rebase,如果你是项将某些 commit 合并,那么建议使用 $ git rebase -i <commit>

    git rebase的vim界面有许多命令:

      ubuntu~git学习

    在git rebase的vim界面中整合完commit记录之后,进行后续三个操作:

    • git rebase --continue,表示rebase操作顺利,继续以保存rebase结果,退出rebase程序。
    • git rebase --absort,表示rebase操作出现错误,中途退出,已恢复原有的commit状态。
    • git rebase --skip,表示rebase操作过程中,某些warning信息是否需要忽略。