1、当项目没有被修改
git status
结果为
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
2、当项目中修改内容,比如文件内容、文件的增删
#1.README.md更新,但没有进入暂存区
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: README.md
no changes added to commit (use "git add" and/or "git commit -a")
------------------------------------------------------------------------
#2.新建了test.py文件,新文件存在于工作目录中,但Git尚未开始跟踪它们
Untracked files:
(use "git add <file>..." to include in what will be committed)
test.py
nothing added to commit but untracked files present (use "git add" to track)
3、这时可以选择推进暂存区,也可以回溯(撤销更新)
#git add 更新的文件
git add README.md
#撤销暂存前的更改 git restore 文件名
git restore test.py
#撤销暂存后的更改 git restore --staged 文件名
git restore --staged test.py
4、将暂存区的内容,提交到本地仓库
# -m (--message),提交信息对更新的描述
git commit -m "添加了test.py测试代码"
5、本地推送至远程仓库(即更新github仓库的项目)
#先查看当前分支
git branch #main
#git push remote_name远程仓库名 local_branch本地分支:remote_branch远程分支
git push origin main #由于local_branch和remote_branch相等,因此可以省略
6、重新拉取更新后的项目,并且合并到本地项目
git fetch: 从远程仓库获取所有分支的更新,但不会自动合并到当前分支。
git merge: 将远程分支的更改合并到当前本地分支。
git pull是fetch和merge的组合:
git pull [options] [repository] [refspec]
[options]: 可以是一些选项,比如 --tags 来同步标签。
[repository]: 通常指远程仓库的名称,默认是 origin。
[refspec]: 指定要拉取的分支,比如 master 或 main。
使用如下:
#git pull origin远程仓库名 remote-branch远程分支:local-branch本地分支
git pull #其他默认即可
完成之后,在vscode中会有新的git提醒,那些代码在该文件上发生了变化(当然前提是正在编辑的状态)