2026年4月14日 研究日志¶
研究内容:导师会真是累人,不过顺利结束了。要改的内容明天再搞,今天先到这里了。今天终于下定决心把自己的代码git到Github上了,当然是private,所以大家看不到啦。之前一直是一台电脑的单打独斗,而且代码量也少,所以一直懒得学怎么用git管理。今天试了下,有Copilot辅助不算难,今天的技术博客就是这个了。
安装 Git 后的第一步:设置用户名与邮箱(必须做)¶
Git 在第一次 commit 时会问你:
Author identity unknown
Please tell me who you are.
这是因为 Git 需要在每次提交里记录提交者名字和提交者邮箱
否则无法生成 commit。
第一次使用 Git 时必须设置:
git config --global user.name "你的名字"
git config --global user.email "你的邮箱"
查看是否设置成功:
git config --global --list
第一次使用:把一个文件夹变成 Git 仓库¶
假设你有一个项目文件夹:
PhD_Project/
CODE/
analysis.R
plot_tsne.py
utils.R
进入该文件夹:
cd /path/to/PhD_Project/CODE
初始化 Git:
git init
查看状态:
git status
把所有文件加入 Git:
git add .
提交(保存一个版本):
git commit -m "Initial commit: add analysis scripts"
到这里,你的本地仓库已经准备好了。
创建 GitHub 仓库并连接¶
在 GitHub 上创建一个空仓库(private 推荐)。
然后在终端执行:
git remote add origin https://github.com/你的用户名/仓库名.git
git branch -M main
git push -u origin main
现在你的代码已经成功上传到 GitHub(其实你新建的空仓库界面上也会显示这些)。
日常使用:push 和 pull(新手最重要)¶
这是科研人员最常用的两条命令。
push:把本地修改上传到 GitHub¶
每次你修改了脚本(R/Python/Notebook),流程如下:
git add .
git commit -m "描述你做了什么修改"
git push
例如:
git commit -m "Fix bug in scMetaInspector; update t-SNE plot"
push = 上传你的最新版本到 GitHub
pull:从 GitHub 下载最新版本到本地¶
如果你:
- 在另一台电脑修改过代码
- 在 GitHub 网页上改过文件
- 合作者提交了新版本
你需要先 pull:
git pull
pull = 下载 GitHub 上的最新版本到本地
push / pull 的注意事项¶
- 换设备前:push
- 换设备后:pull
- 每天结束前:push
使用 .gitignore 保持仓库干净¶
科研项目常见不需要上传的文件:
.Rhistory
.Rproj.user/
__pycache__/
*.log
*.tmp
*.csv
*.xlsx
*.rds
*.h5
创建 .gitignore:
touch .gitignore
加入上述内容,然后:
git add .gitignore
git commit -m "Add gitignore"
git push
分支(branch):做实验性分析的最佳方式¶
不要在 main 上乱改。
创建分支:
git checkout -b try-new-tsne
完成后合并:
git checkout main
git merge try-new-tsne
删除分支(可选):
git branch -d try-new-tsne
先和导师开小会再在组里开大会把我的精力都耗尽了,今天先休息了,明天继续