Git常见问题汇总

平时开发中git操作遇到的问题,分享一下,希望对大家有用。

1、gitignore文件无法忽略某些文件

在管理一个版本库时,有时候不想要管理某些文件,这个时候我就把这个问件写到.gitignore文件中,这样应该就可以将这个文件忽略,不再进行·版本管理了,但是经常出现的情况是:将这些文件名写到其中了,使用

git status
  • 1

暂存区图示

查看发现这些文件并没有被忽略掉。查了资料发现,想要.gitignore起作用,必须要在这些文件不在暂存区中才可以,.gitignore文件只是忽略没有被staged(cached)文件,对于已经被staged文件,加入ignore文件时一定要先从staged移除,才可以忽略。 
git rm –cached testFile

这样,在.gitignore中写testFile,这个文件才可以被忽略掉。

2、git强制中断,之后无法上传

这里写图片描述

将这个lock删除,rm -f ./.git/index.lock
  • 1

这里写图片描述

然后再用git reset HEAD 
回滚到add之前的版本中。

3、解决每次Git提交都要输入密码

3.1 临时方法 
之前遇到一个问题,我们已经添加了ssh公钥,但是每次提交的时候,还是要我们输入密码,能不能不输入密码呢? 
其实是可以的,步骤如下: 
3.1.1、进入到所要提交的工程文件夹下 
3.1.2、输入 ssh-add ~/.ssh/id_rsa命令,注意:id_rsa是你的私钥的名字

这里写图片描述

3.1.3、如果出现Could not open a connection to your authentication agent. 
输入

ssh-agent bash
  • 1

3.1.4、然后继续输入 ssh-add ~/.ssh/id_rsa

这里写图片描述

3.1.5、至此需求实现

3.2 永久方法 
3.2.1、用当前的私钥创建一个不需要密码的私钥

$ openssl.exe rsa -in .ssh/id_rsa -out .ssh/id_rsa_new
Enter pass phrase for .ssh/id_rsa:
writing RSA key
  • 1
  • 2
  • 3

3.2.2、将旧的私钥备份到id_rsa_old

mv id_rsa id_rsa_old
  • 1

3.2.3、将新的私钥赋值到原来用于提交工程私钥

mv id_rsa_new id_rsa
  • 1

4、如果commit时message写错了怎么办?

git commit --amend
Will open your editor, allowing you to change the commit message of the most recent commit. Additionally, you can set the commit message directly in the command line with:

git commit --amend -m "New commit message"
  • 1
  • 2
  • 3
  • 4

5、将本地分支推送到远程

 git push origin develop
  • 1

6、将本地某一部分的代码,切换到以前版本的代码

git checkout f68b9748ae3 -- src/gisComponets/*
  • 1
  • 2

7、将仓库中的某个文件删除

git rm -cache build/scripts/index.js

猜你喜欢

转载自blog.csdn.net/sunny2come/article/details/80409017