Skip to content

index

  • 合并策略,以及如何显示有冲突的文件?

    1. 如果主分支后有提交,默认走 recursive, 即会主动创建一个 merge commit; 如果主分支并无提交,默认走 fast-forward,即直接指向被合并分支的提交,无 merge commit。
    2. 首先一定 git merge --no-ff --no-commitno-ff 确保每次合并一定会生成一个 merge request( fast-foward 指被合并分支是当前分支的 descendant 那么合并时直接将 branch pointer 移到被合并分支上),no-commit 指不发生冲突的情况下页不会自动合并。因此用户可以通过 git commit 继续合并或者 git merge —abort 退出合并(虽然在 log 里可以看见被 merge 分支的 log, 但是后续 reset 时,还是 reset HEAD~1, 即 reset 掉 merge 的那一个 commit 即可)
    3. 我喜欢 git merge --squash -m "new merge commit"
    4. git diff --diff-filter=U --relative U to only include unmerged files, relative to show paths relative to current working directory. 如果只想要显示文件名,加上 —-name-only
    5. 注意 rebase 时 additional commits 会生成新的 hash,也就相当于重置了新的additional commits
  • 讲 git 原理很好的帖子:Git 内部原理图解——对象、分支以及如何从零开始建仓库: https://chinese.freecodecamp.org/news/git-internals-objects-branches-create-repo/#:~:text=%E5%9C%A8%20git%20%E4%B8%AD%EF%BC%8C%E6%96%87%E4%BB%B6%E7%9A%84,%E2%80%94%E2%80%94%E6%95%B0%E6%8D%AE%E7%9A%84%E4%BA%8C%E8%BF%9B%E5%88%B6%E6%B5%81%E3%80%82

  • blob: 文件内容; Tree与Commit;

  • Head: 默认指向当前 branch 的最新 commit; detached-head: head doesnt belong to a branch

  • Undoing unstaged changes:

    • checkout: 如果+文件名,代表切到某文件的最近提交状态(即抛弃当前的更改);+.代表抛弃当前所有更改
    • newer: git restore
    • newer: git clean // remove newly added files
  • Undoing staged changes:

    • reset + 文件名: means copy the latest commited chanage of a file into the staging area. 需要先把 staging status 变为 commit 的状态,再 checkout
    • newer: git restore --staged 相当于上述 reset
  • git reset

    • Soft: only reset the head to commit, will leaves all changed files "Changes to be committed"

    • mixed: the default action, resets the index but not the working tree.

    • Hard: resets the index and working tree.

      In other words, --soft is discarding last commit, --mix is discarding last commit and add, --hard is discarding last commit
      

commit

学会分区 commit 内容

  • Add files to staging: git add file1 file2
  • Check to see what you are committing: git status
  • Commit: git commit -m "ss" or git commit file1 file2 -m "sss"
  • Accidentally commit the wrong files: git reset --soft HEAD~1
  • Unstage the files: git reset -- filename
  • Stash a certain file[s]: git stash push [file]
  • Merge last several results: git rebase -i HEAD~n 注意,如果如果只有 2 个 commit 的话,需要 rebase 到 root ref

log

  • log with changes overview: git log --raw
  • 显示某文件最近两次的 diff 内容: git log -p -2 -- filepath
  • 显示某文件在某一 commit 下的内容: git show commit:filepath
  • git show 梗概版: git show --stat commit

local and remote

  • git remote add origin xxx: 这里的 origin 实际只是一个后续 url 的 key shorthand, can be any word.
  • git push origin master: 这里的 remotes/origin/master 称为 remote tracking branch
  • git fetch origin(remote branch to remote tracking branch) v.s. git pull origin master // 后者需要指定 tracking branch 才可以 merge
  • 当出现:Your branch have diverged, and have 1 and 2 different commits each, respectively. (use "git pull" to merge the remote branch into yours) 时…说明你的历史和远端分支的历史出现了冲突(比如远端分支被 push --f 了以后),所以
  • merge/rebase 远端的: git merge(or rebase) origin/master
  • 抛弃本地分支,使用远端分支: git reset --hard origin/master
  • git branch --track xxx (remotes/可以省略)origin/xxx
  • local tracking branch -> remote tracking branch -> remote branch
  • git branch -vv
  • git push -u origin xxx // 这里 -u 是指的 upstream