Git and GitHub workflow

Git and GitHub workflow

  1. git clone // 到本地
  2. git checkout -b xxx 切换至新分支xxx
    (相当于复制了remote的仓库到本地的xxx分支上
  3. 修改或者添加本地代码(部署在硬盘的源文件上)
  4. git diff 查看自己对代码做出的改变
  5. git add 上传更新后的代码至暂存区
  6. git commit 可以将暂存区里更新后的代码更新到本地git
  7. git push origin xxx 将本地的xxxgit分支上传至github上的git

(如果在写自己的代码过程中发现远端GitHub上代码出现改变)

  1. git checkout main 切换回main分支
  2. git pull origin master(main) 将远端修改过的代码再更新到本地
  3. git checkout xxx 回到xxx分支
  4. git rebase main 我在xxx分支上,先把main移过来,然后根据我的commit来修改成新的内容
    (中途可能会出现,rebase conflict -----》手动选择保留哪段代码)
  5. git push -f origin xxx 把rebase后并且更新过的代码再push到远端github上
    (-f ---》强行)
  6. 原项目主人采用pull request 中的 squash and merge 合并所有不同的commit

远端完成更新后

  1. git branch -d xxx 删除本地的git分支
  2. git pull origin master 再把远端的最新代码拉至本地

Disk, Staging,Local,Remote
git init

git diff

撤销还未缓存的修改

git status -> Changes not staged for commit(红色)

git checkout <changed_file>

(git restore <changed_file>)

撤销缓存区中的修改

git status -> Changes to be commited(绿色)

git reset <changed_file>

(git restore --staged <changed_file>)

撤销D和S中的修改

git checkout HEAD <chaned_file>

撤销提交的版本

git reset --soft HEAD~1

非软撤销

git reset HEAD~1

撤销 commit 版本同时清除 add 添加的缓存

撤销 L+S+D

git reset --hard HEAD~1

完全回滚(撤销已经提交的版本、放弃已经添加的缓存、甚至本地的修改)

reset vs rever

和git reset不同,git revert 本质上是增加一个 反相的commit

revert只能前进不许后退

在公有分支上 只能前进 不许后退

私有分支上可以使用reset

不过同步时要使用 git push -f

posted @ 2022-11-25 23:30  Euphocc  阅读(39)  评论(0)    收藏  举报