GitCode 上传和更新指令表
本文档用于把当前 STM32 工程上传到 GitCode,并记录日常更新代码时常用的 Git 指令。
1. 第一次上传工程
1.1 进入工程目录
powershell
cd E:\0WJL\code\motor_contorl
1.2 初始化 Git 仓库
如果当前文件夹还不是 Git 仓库,先执行:
powershell
git init
1.3 查看当前文件状态
powershell
git status
1.4 添加需要上传的文件
添加全部文件:
powershell
git add .
只添加某一个文件:
powershell
git add README_WLWG_PORT.md
只添加某一个文件夹:
powershell
git add App_WLWG_Port
1.5 提交到本地仓库
powershell
git commit -m "first commit"
提交说明建议写清楚本次改了什么,例如:
powershell
git commit -m "add remote mixer and motor control logic"
1.6 绑定 GitCode 远程仓库
先在 GitCode 网页上新建一个仓库,然后复制仓库地址。
HTTPS 地址通常类似:
text
https://gitcode.com/用户名/仓库名.git
SSH 地址通常类似:
text
git@gitcode.com:用户名/仓库名.git
绑定远程仓库:
powershell
git remote add origin https://gitcode.com/用户名/仓库名.git
或者使用 SSH:
powershell
git remote add origin git@gitcode.com:用户名/仓库名.git
查看是否绑定成功:
powershell
git remote -v
1.7 设置主分支名称
如果希望主分支叫 main:
powershell
git branch -M main
1.8 第一次推送到 GitCode
powershell
git push -u origin main
第一次推送后,后续再上传更新通常只需要:
powershell
git push
2. 后续更新代码并上传
每次修改完代码后,一般按下面顺序执行。
2.1 查看改动状态
powershell
git status
2.2 查看具体改了什么
powershell
git diff
查看某个文件的改动:
powershell
git diff App_WLWG_Port/remote_motor_mixer.c
2.3 添加改动
添加全部改动:
powershell
git add .
添加指定文件:
powershell
git add Core/Src/main.c
2.4 提交改动
powershell
git commit -m "update remote motor mixer logic"
2.5 上传到 GitCode
powershell
git push
3. 从 GitCode 拉取别人或远端的更新
3.1 拉取并自动合并
powershell
git pull
3.2 指定从 main 分支拉取
powershell
git pull origin main
建议在开始改代码前先执行一次:
powershell
git pull
这样可以减少和远端代码冲突的概率。
4. 常见查看命令
4.1 查看当前状态
powershell
git status
4.2 查看提交记录
powershell
git log
简洁查看:
powershell
git log --oneline
4.3 查看当前分支
powershell
git branch
4.4 查看远程仓库地址
powershell
git remote -v
4.5 查看某次提交内容
powershell
git show 提交ID
例如:
powershell
git show a1b2c3d
5. 分支常用命令
5.1 新建并切换到新分支
powershell
git checkout -b feature/remote-mixer
或者:
powershell
git switch -c feature/remote-mixer
5.2 切换分支
powershell
git checkout main
或者:
powershell
git switch main
5.3 推送新分支到 GitCode
powershell
git push -u origin feature/remote-mixer
5.4 合并分支到 main
先切回 main:
powershell
git switch main
拉取最新代码:
powershell
git pull
合并功能分支:
powershell
git merge feature/remote-mixer
推送:
powershell
git push
6. 常见撤销命令
6.1 撤销某个文件的未提交修改
注意:这个命令会丢弃该文件的本地修改。
powershell
git restore Core/Src/main.c
6.2 撤销 git add
如果已经 git add,但还没有 commit:
powershell
git restore --staged Core/Src/main.c
撤销全部已暂存文件:
powershell
git restore --staged .
6.3 修改最后一次提交说明
powershell
git commit --amend -m "new commit message"
如果这次提交已经 push 到远端,不建议随便 amend。
7. 常见问题
7.1 origin already exists
说明已经绑定过远程仓库。
查看当前远程地址:
powershell
git remote -v
如果地址错了,可以修改:
powershell
git remote set-url origin https://gitcode.com/用户名/仓库名.git
7.2 failed to push some refs
通常是远端有本地没有的提交。
先拉取:
powershell
git pull
如果没有冲突,再推送:
powershell
git push
7.3 忘记当前有没有提交
执行:
powershell
git status
如果显示:
text
nothing to commit, working tree clean
说明当前没有未提交改动。
7.4 想忽略编译输出文件
可以新建或编辑 .gitignore,常见 STM32/Keil 工程可以忽略:
gitignore
*.o
*.d
*.crf
*.map
*.lst
*.axf
*.hex
*.bin
*.build_log.htm
Objects/
Listings/
MDK-ARM/Objects/
MDK-ARM/Listings/
修改 .gitignore 后提交:
powershell
git add .gitignore
git commit -m "add gitignore"
git push
8. 推荐日常流程
每次开始写代码前:
powershell
git pull
修改代码后:
powershell
git status
git diff
git add .
git commit -m "说明本次修改内容"
git push
如果只是想确认有没有改动:
powershell
git status