Git 常见问题

.gitignore 规则不生效?

只能忽略那些原来没有被track的文件,如果某些文件已经被纳入了版本管理中,则修改.gitignore是无效的

git rm -r --cached .
git add .
git commit -m 'update .gitignore'

怎样生成 SSH 公钥?

ssh-keygen

#Generating public/private rsa key pair.
#生成公钥/私钥rsa密钥对
#Enter file in which to save the key (/root/.ssh/id_rsa):
#输入密钥文件保存的位置
#Created directory '/root/.ssh'.
#创建目录 "/root/.ssh"
#Enter passphrase (empty for no passphrase):
#输入密码(无密码短语为空)
#Enter same passphrase again:
#再次输入相同的密码
#Your identification has been saved in /root/.ssh/id_rsa.
#您的身份已保存在	/root/.ssh/id_rsa
#Your public key has been saved in /root/.ssh/id_rsa.pub.
#您的公钥已保存在	/root/.ssh/id_rsa.pub
#The key fingerprint is:
#SHA256:hnACpdf1dOno2Zis+1WMgBqXIhDQpFjZ0jPp7vnUyjw root@d157f72da45c
#The key's randomart image is:
#+---[RSA 2048]----+
#|.+**..  . . ..   |
#|.o++*. . = ..    |
#|o .o=o= + oo     |
#|   ..= *  ...o   |
#|   .  o So =. o  |
#|    .  o  * ..   |
#|   . .. ..  .    |
#|    o+E..  .     |
#|     .=..o.      |
#+----[SHA256]-----+

如何退至指定提交版本?

# commitId之后提交的commit都去除
git reset --hard [commitId]

git pull报错?

$ git pull
# 错误信息
Pull is not possible because you have unmerged files.
Please, fix them up in the work tree, and then use 'git add/rm <file>'
as appropriate to mark resolution, or use 'git commit -a'
# 解决方法
git add -u
git commit

引用

  1. pull会使用git merge导致冲突,需要将冲突的文件resolve掉 git add -u, git commit之后才能成功pull.
  2. 如果想放弃本地的文件修改,可以使用git reset --hard FETCH_HEAD,FETCH_HEAD表示上一次成功git pull之后形成的commit点。然后git pull. 注意:git merge会形成MERGE-HEAD(FETCH-HEAD) 。git push会形成HEAD这样的引用。HEAD代表本地最近成功push后形成的引用。
$ git pull
# 错误信息
You are not currently on a branch, so I cannot use any
'branch.<branchname>.merge' in your configuration file.
Please specify which remote branch you want to use on the command
line and try again (e.g. 'git pull <repository> <refspec>').
See git-pull(1) for details.
# 解决方法
git checkout -b temp
git checkout master
# 错误信息
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
# 解决方法
git pull origin master --allow-unrelated-histories
git push
# 错误信息
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
# 解决方法
git push -f
# 错误信息
you need to resolve your current index first 
# 解决方法
git reset --merge
posted @ 2020-08-07 08:58  林一怂儿  阅读(120)  评论(0编辑  收藏  举报