Git

Git Notes

BY Guanqiao Huang

Posted by HUANG on December 17, 2020
  • git init 一开始初始化repo
  • git clone (remote repository only) 一开始从某地址克隆repo
  • .git Git自动生成的文件夹,用来保存history
  • .gitignore 一个文件用来忽略某一类文件或文件夹,比如一类文件*.mp4或者[Db]ebug文件夹
  • git add . 添加所有文件到stage状态
  • git add filename 添加某文件到stage状态,叫staged
  • git status 查看当前的状态
  • git restore --staged filename 取消某文件的stage状态,叫unstage
  • git commit -m "Message" 将staged的文件保存,并附上message,可以理解为保存为一个版本
  • git stash 临时保存当前所有的文件的修改状态到抽屉,将修改恢复到最新保存(有记录/Commit过的)的版本。可以理解为临时保存当前游戏记录,并且恢复到上一个记录点。
  • git stash -m "Message 类似于commit的用法,在stash基础上加message
  • git stash list 查看抽屉的列表
  • git stash apply 恢复到最新的临时保存的状态,可以将最新的stash记录取出来。可以理解为恢复最新的临时记录。
  • git stash pop stash@{0} 恢复某临时记录
  • code . 在CLI上输入后,可以调用VS Code
  • VS Code里文件有U的标识,意思是Untracked
  • git push 用作将local repo更新到remote
  • git fetch 从remote往本地拉取最新信息,同步信息
  • git pull 将remote拉取到本地
  • sync 就是pull push结合
  • Remote repository 和 Local repository是彼此独立,两者是同步的关系。local不能直接commit到remote。通常是用commit提交到到local,再把本地的repository的版本提交到remote。
  • origin/xx 是一个remote的标识
  • git rm filename 意思是将某文件从git中移除,rm后,查看状态git status会发现在rm的文件会在staged状态,需要commit后,文件和记录才会真正被移除。(某文件不想再被version control了)
  • git restore --staged delected-filename 这个可以将rm的文件变回unstaged状态,再使用checkout恢复出来,前提是rm后没有commit。
  • git checkout . 获取某branch的最新历史版本,rm后立刻用的话可以可以立马恢复到最新的历史版本。
  • git clean -f 假如需要将所有新创建的文件清除,可以用clean。这个操作会删除文件的,所有要加-f意思是force强制的意思。这个不影响modified的文件,只针对新增的文件。
  • git revert hashvalue commit一个新版本,恢复到某版本,message和恢复的版本一样。
  • git revert --abort 终止revert
  • git reset hashvalue --soft soft reset到之前某版本,修改了的差异是放在staged状态。这个还会保存之前的记录,只不过隐藏起来了。
  • git reset hashvalue --hard hard reset到之前某版本,之前的记录会被真正的删除。
  • git diff 对比两个版本的不一样
  • git branch 查看branch有哪些,目前在哪branch
  • git branch <branch name> 创建新的branch
  • git checkout master 切换到master,或者某branch
  • git checkout -b <branch name> 创建新的branch并且切换到新的banch
  • branch命名规则,<type>/<ticket-number>-<title>, 一般用在feature、fix/bugfix/hotfix,chore.
  • git merge feat1 在当前的branch,选择将feat1合并到当前branch,feat1历史记录也会进来
  • git merge <branch name> --abort
  • git merge <branch name> --overwrite-ignore
  • git merge <branch name> --no-overwrite-ignore
  • git log --all --decorate --oneline --graph 图形化显示所有banch和log
  • git rebase <branch name> rebase合并记录,并且在一条线上
  • git push <name> <local_branch>:<remote_branch>
  • 正常工作情况,每天工作都要pull一下最新的remote master
  • git remote add origin https://github.com/xx/xx.git
  • git push -u origin master
  • A branch is a divergence from the main repository and essentially an independent line of code development.

The general process of reviewing a pull request

  1. Read the pull request description
  2. Comment on lines of code that need feedback
  3. Submit review
  4. Take a look again, repeat if needed

The steps (in order) to successfully create and merge a pull request

  1. Submit pull request with description
  2. Make changes from feedback
  3. Merge code

The major steps in the Github flow

  1. Work on a specific branch
  2. Commit changes and push code to remote repository
  3. Create pull request
  4. Discuss pull request with reviewers
  5. Merge branch once pull request accepted

About Pull Request

Examples

  • A short pull request to remove trailing spaces on the Codecademy docs repository which is immediately approved.
  • A long pull request to add a new component on the Codecademy gamut repository which includes screenshots, comments, and a lively discussion before being approved.

CLI 一些用法

  • echo > filename 可以创建文件,并且输入多行文字,回车保存, 会覆盖记录
  • echo "message" > filename 将一行文字保存到某文件,会覆盖记录
  • echo >> filename 可以创建文件,并且输入多行文字,回车保存, 不会覆盖,而是继续添加在文本后
  • echo "message" >> filename 将一行文字保存到某文件,不会覆盖,而是继续添加