Git
配置与github的ssh连接
git config --global user.name "XXX"
git config --global user.email "xxx"
ssh-keygen -t rsa -C "xxx" # 按回车三次
clip < ~/.ssh/id_rsa.pub # 复制公钥到github上 -> cat ~/.ssh/id_rsa.pub
rm -rf ~/.ssh/* # 把现有的 ssh key 都删掉
ssh -T git@github.com # 测试是否可以正常访问git hub
本地操作
git add xxx.c # 文件提交到暂存区
git commit -m "[ADD]: 提交新代码xxx.c" # 提交代码到本地仓库
git status # 查看哪些文件是被修改的(有改动显示红色)
git reset --hard <版本号>
git checkout [file_name] # 撤销文件修改
git commit -am '***' # add+commit
git log --graph --oneline --all # 查看提交记录
远程操作
git clone git@xxx/code.git # 下载代码
git remote add origin git@github.com:自己的GitHub账户名/自己的仓库名.git # 添加远程仓库
git remote -v # 查看所有远程库
git branch -r # 列出远程分支
git branch -vv # 列出本地分支与远程分支的绑定情况
git fetch # 从远程仓库下载本地仓库中缺失的提交记录,更新远程分支指针(如 o/main)
git fetch origin :bugFix # 在本地创建bugFix分支
git fetch origin bugFix # 获取远程指定分支
git pull # 获取origin/main分支内容并合并到当前分支,==》git fetch + git merge
git pull origin foo # 从指定远程库获取并合并到当前分支,git fetch origin foo; git merge o/foo
git pull origin foo:bugFix # git fetch origin foo:bugFix; git merge bugFix
git pull origin :bugFix # 删除本地和远程的bugFix分支
git push # 推入远程同名分支
git push origin foo:bugFix # 推入指定远程分支
git push origin :bugFix # 删除远程bugFix分支
git push -u origin main # 当添加“-u”参数时,表示下次继续push的这个远端分支的时候推送命令就可以简写成“git push”。
分支操作
git branch # 查看分支; [-a] 列出所有分支; [xxx]基于当前分支末梢创建新分支
git branch -M main # 将当前节点名称修改为main, 新建仓库时需要修改
git branch -m dev2 version.2 # 修改某一分支名称
git branch -f bugFix HEAD^2 # 将bugFix分支移动到某个节点
git branch -d foo # 删除foo分支
git checkout newBranch # 切换分支
git chekcout <commit> # 切换到指定commit
git checkout -b newBranch # 创建并切换分支
git checkout -b newBranch origin/newBranch # 切换到远程分支
git checkout c4 # 将HEAD切换到某一提交记录
git checkout HEAD^ # 将HEAD移动到该分支的前一个
git checkout HEAD~3 # 将HEAD移动到该分支的前三个
git checkout -- filename # 从暂存区移到工作区
git checkout . # 放弃所有的文件修改
git reset HEAD # 将所有此次修改的 file 退回到工作区
git reset HEAD filename # 将某一退回到工作区
git reset [版本号] # 工作目录的修改、暂存区的内容以及由 reset 所导致的新的文件差异,都会被放进工作目录
git reset --hard [版本号] # 回退到某个版本,工作区和暂存区内容全部丢失
git merge bugFix # 将bugFix合并到当前分支(会产生一个合并记录)
git rebase main # 将当前分支内容合并到main分支
git rebase main bugFix # 将bugFix分支内容合并到main
git cherry-pick [版本号] # cherry-pick 将提交树上提交记录取过来追加到 HEAD 上
提交代码
-
提交步骤
# 创建新分支,分支名:工号/版本号/简要描述 git checkout -b 91448/6.8.0/update-spu-overset git add . # 本地提交 git commit -m 'xxx' git push # 提示错误,会显示正确提交语句(较长,复制即可) # 然后转到gitlab网页中,查看流水线是否通过 # 然后新建合并请求 -
提交技巧
-
多次提交一次记录
git status git add . git commit --amend --no-edit git push -f -
落后提交
git pull --rebase # git fetch + git rebase orgin/<分支名> # 将本地分支与远程分支合并 git push -f -
修改上一次提交中的内容
git checkout main # 切换到主分支 git cherry-pick cx1 # 将要修改的提交拿过来 git commit --amend # 补充提交 git cherry-pick cx2 # 将后面要修改的提交也拿过来 -
commit后发现忘记创建新分支
git checkout -b feature git branch -f main HEAD^ git push
-
远程追踪
git checkout -b foo origin/main
git commit
git push
git branch -u origin/main foo
Step 1: From your project repository, check out a new branch and test the changes.
git checkout -b wchi-remote-main main
git pull git@github.com:wchi-remote/timiOS.git main
Step 2: Merge the changes and update on GitHub.
git checkout main
git merge --no-ff wchi-remote-main
git push origin main
删除文件的git仓库
git rm --cached 文件名 # 移除版本控制中的指定文件并需要在工作区中保留该文件
rm -rf ./.git
实用技巧
将本地的已有项目关联到github上的新的项目上
1、创建仓库(名称最好与项目名称相同)
2、本地仓库与远程仓库关联
git remote add origin git@github.com:yourname/repositoryname.git # 将本地仓库和你自己的远程仓库关联起来,origin是仓库的名字
git pull origin master # 先将关联后的github仓库中的代码pull下来
# 将最新的修改推送到远程仓库 将本地仓库的文件推送到远程仓库,可能你的本地项目关联着几个仓库,
# 你可以根据仓库的名字git push origin master 将代码提交到不同仓库中,可以指定分支,
# 第一次使用加上了-u参数,是推送内容并关联分支。
# 推送成功后就可以看到远程和本地的内容一模一样,下次只要本地作了提交,就可以通过命令:
git push -u origin master
git push origin master #把最新内容推送到Github上关联的远程仓库中去。
本文详细介绍了如何配置Git的SSH连接,包括设置用户名、邮箱,生成SSH密钥对,测试访问权限,以及本地与远程操作的一系列命令,涵盖分支管理、提交、拉取与推送等关键步骤。

3万+

被折叠的 条评论
为什么被折叠?



