本篇主要记录git的一些使用,仅做本人学习、查阅之便,所有指令均以测试通过
Git版本信息
$ git --version
git version 2.33.1.windows.1
git 下载代码
git clone -b 远程分支 仓地址
取消本地的一些改变,会撤销文件的所有改变
git checkout -- <filename>
取消已经add的改动
git restore --staged <filename>
撤销已commit的本地提交
1、仅是撤销commit提交,修改后的文件会保留在暂存区,改动不会消失
git reset --soft HEAD~1
2、清除本次commit的文件改动,新建文件会被删除,回到上一次提交文件的状态【谨慎操作】
git reset --hard HEAD~1
查看本次commit的文件信息
git show HEAD --stat
commit bbcfa26f82132961822d2c5cad86f2e534cd8517 (HEAD -> main)
Author: xx <xxx@163.com>
Date: Sun Jul 14 15:28:44 2024 +0800
测试commit 撤回
test/test1.py | 2 +-
test/test6.py | 1 +
2 files changed, 2 insertions(+), 1 deletion(-)
将一个分支source-branch中的特定文件添加到另一个分支target-branch中,而不合并整个分支
git checkout target-branch
git checkout source-branch -- <path-to-file1> <path-to-file2>
git add <path-to-file1> <path-to-file2>
git commit -m "Add specific files from source-branch"
git push
将分支branchA中的所有改变合并到另一个分支branchB中,同步两个分支的代码
1、 进入要同步代码的分支,落后的分支branchB
git checkout branchB
2 将最新代码branchA合并到branchB分支
git merge branchA
3、如果需要
git push
修改远程仓库的3次提交记录
git rebase -i HEAD~3
此时会进入编辑界面需要将pick修改对应的操作指令,这里是drop,修改完成后直接git push -f即可。