常用命令之git
前言:git命令有很多,不是每个都记得住,在此记录,方便查询。
其他GitHub在8.13不能使用的问题解决
使用GitHub客户端。
1,在客户端。ssh-keygen -t rsa -C "xx@yy.com"
2,在GitHub设置上添加pub公钥。
九、常见报错
1,git pull问题解决error: cannot lock ref
解决命令:git remote prune origin
解析:这将删除文件夹中对远程分支的引用.git/refs/remotes/origin。因此,这不会影响您的本地分支机构,也不会更改任何远程对象,但会更新您对远程分支机构的本地引用。在某些情况下,这些引用可能包含Git无法正确处理的数据。
参考:https://blog.csdn.net/zgpeace/article/details/112171725
八、fork了别人项目之后,如何保持同步更新
只需要下面三步:
- 把fork的项目添加到本地仓库中 add upstream
- fetch upstream别人的更新
- 回到自己的main分支,合并更新git merge upstream/main
- (非必须)再push到自己仓库的远程。
git push origin main
参考:https://www.jianshu.com/p/021bb953ee8d
七、把当前仓库remote更改为新的remote
git remote -v
方式一:移除。git remote remove origin
git remote -v
git remote add origin git@github.com:组织名/web3-devops-frontend.git
方式二:更新。git remote set-url origin [new-url]
git remote set-url origin git@github.com:组织名/web3-devops-frontend.git
方式三:重命名。git remote rename origin dev
git remote add origin git@github.com:组织名/web3-devops-frontend.git
参考文档:https://komodor.com/learn/how-to-fix-fatal-remote-origin-already-exists-error/
六、GitHub仓库从HTTPS转向ssh
最开始用命令,从去年不让用HTTPS后开始用桌面端,前段时间桌面端老报错,最终又回到命令行。
第一步:跟使用gitlab一样。在本地创建公私钥对,添加到GitHub个人设置下的ssh key。
第二步:命令行使用 git@github.com:xxxxx.git 就正常了。
问题:对于已经是https的项目,如何切换到ssh?
答:一个命令即可:
git remote set-url origin git@github.com:xxxxx.git
五、fork相关
git remote -v
git pull origin main # fork后的仓库
git pull upstream main # 源仓库
如果fork后的仓库与源仓库修改冲突,则:
git stash # 先存
中间操作
git stash pop # 再取
四、Git配置和账户相关
(1)切换账号
1. 查看当前登录账号:
git config user.name
2. 查看当前登录邮箱:
git config user.email
3. 修改用户名和邮箱:
git config --global user.name "Your_username"
git config --global user.email "Your_email"
(2)在docker中git pull遇到这个问题
hint: Pulling without specifying how to reconcile divergent branches is
hint: discouraged. You can squelch this message by running one of the following
hint: commands sometime before your next pull:
hint:
hint: git config pull.rebase false # merge (the default strategy)
hint: git config pull.rebase true # rebase
hint: git config pull.ff only # fast-forward only
hint:
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
解决办法:
git config --global pull.rebase false # 这将保留默认行为并禁止显示警告。
https://www.cnblogs.com/hongdada/p/9549506.html
三、撤销相关
(1)撤销commit
git reset --soft HEAD^
参考:https://www.jianshu.com/p/a9f327da3562
(2)删除本地文件,并commit了,想恢复
git restore --staged src/components # 撤销commit
git checkout . # 恢复文件到本地
(3)关于回退到某一个历史版本(commit)
git reset --hard 3b6be5ef353440718c4129ac84f66fd6052f1c2
注意:上述命令执行成功之后,会彻底返回到回退前的版本状态,新发生的变更将会丢失。对于部分发生了变更,但是变更部分的文件夹存在未提交的文件可能导致目录非空而删除失败,此时需要自行处置。
# 强推到远程:
git push origin HEAD --force
二者区别:
git reset –-soft:回退到某个版本,只回退了commit的信息,不会恢复到index file一级。如果还要提交,直接commit即可;
git reset -–hard:彻底回退到某个版本,本地的源码也会变为上一个版本的内容,撤销的commit中所包含的更改被冲掉;
初始状态,可见当前分支最后一个提交是debug the initialization page
现在要撤销该commit,但是又不能撤销该提交包含的更改,使用git reset --soft,执行结果为
可见commit取消了,代码更改并没有取消。
参考:https://blog.csdn.net/yangfengjueqi/article/details/61668381
二、关于远程操作
(1)删除远程分支:
先查看远程分支:git branch -r
使用下面两条命令来删除远程分支
git branch -r -d origin/branch-name git push origin :branch-name
参数含义: -r, --remotes List or delete (if used with -d) the remote-tracking branches.
上面的第一句是删除了本地的远程跟踪分支( 我也不知道怎么描述更加清楚),此时使用git branch -a查看,分支remotes/origin/branch-name应该已经不存在了。
为什么还需要第二句,因为上面只是把本地的远程跟踪分支删除了,远程的分支还没有删除,所以第二句就是真正的删除原种分支。
若出现错误 重新操作
Pushing an empty <src> allows you to delete the <dst> ref from the remote repository.
(2)更改GitHub远程仓库源
git remote rm origin
git remote add origin NewGitURL
参考:https://www.cnblogs.com/x9mars/p/12391095.html
(3)GitHub删除tag
GitHub界面上没权限
git push origin --delete v0.16.2
一、Git最基本用法
(1)git 开新分支:
想基于某一个分支切新分支:就先到该分支下,运行命令git checkout -b即可。
git checkout okexchain/branchA
git checkout -b okexchain/branchB
(2)更换主分支:由main改为master。(在GitHub上遇到)
git branch -m main master
git fetch origin
git branch -u origin/master master