安装git环境
更新源sudo apt-get update
安装git sudo apt-get install git
查看git版本:git --version
git version 2.34.1
配置github账户
- 本地绑定账号
使用Github用户名:git config –global user.name “Your Name”
使用Github认证邮箱:git config –global user.email “youremail@example.com”
- 生成SSH key
本地生成邮箱对应SSH keyssh-keygen -t rsa -C “youremail@example.com”
注意,这里不可以直接复制生成的key,需要将pub文件内容打印后复制
打印key:cat ~/.ssh/id_rsa.pub - Github上添加SSH key认证
登陆github——右上角头像——Settings
SSH and GPG keys——New SSH key
Title:任意取名
Key Type: Authentication Key
Key:复制第二步中的key内容到此处 - 验证Authentication
ssh -T git@github.com
绑定成功会打印以下消息:
Hi Your name! You’ve successfully authenticated, but Github does not provide shell access.
Git 上传仓库
1.添加git文件
添加当前目录及子目录下所有文件git add .
使用 .gitignore文件可以忽略不需要的内容,如log
在.gitignore文件中添加:
build*/
log*/
*.log
可以使用#添加注释
如果文件已添加可以通过git rm --cached foldername* -r 清除原先添加的文件
git status 实时查看git状态
git commit -m ‘注释信息’ 添加本次push的注释
2. 添加远程库认证并上传
git remote add origin https://github.com/yourname/yourrepo.git
git branch -M main
git push -u origin main
需注意的是HTTPS的认证方式已经失效,为了更安全的认证,目前HTTPS改用 Git Credential Manager进行认证
remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
若不慎已经使用HTTPS绑定远程库,可以通过以下方法修改
使用git remote -v 查看当前的远程库设置,一般情况下,‘origin’ 是默认的远程名称
(1):修改远程设置为SSH:
git remote set-url origin git@github.com:yourname/yourrepo.git
(2):删除远程设置git remote remove origin 添加远程设置git remote add origin git@github.com:yourname/yourrepo.git
考虑到已经使用SSH进行验证,使用SSH进行上传
git remote add origin git@github.com:yourname/yourrepo.git
git branch -M main
git push -u origin main
其他常用git命令
拉取远程仓库代码:git pull
克隆远程某分支上的代码:git clone -b 分支名 http://xxx.git
合并分支到主分支:git merge 分支名称
创建新分支:git branch 新分支名
删除分支:git branch -D 分支名
查看分支:git branch
分支切换:git checkout 分支名称
查看记录:git log
查看地址:git remote -v
强制合并代码(用于当前版本和历史提交版本不一致的情况):git pull origin 分支名--allow-unrelated-histories
本地代码覆盖远程分支代码:git push -f --set-upstream origin 分支名
参考链接
https://github.com/git-ecosystem/git-credential-manager/blob/main/README.md
https://blog.youkuaiyun.com/weixin_51033461/article/details/119997189
本文详细介绍了如何安装Git环境,配置GitHub账号,生成SSH密钥,以及常见的Git操作,包括上传仓库、拉取代码、合并分支等。还提到了HTTPS认证的替代方案GitCredentialManager。
1068

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



