使用Git遇到的问题及解决方案

素颜马尾好姑娘i 2022-09-25 04:17 352阅读 0赞

第一次使用Git进行代码提交,遇到一些问题,于此记录并分享之

使用Git初体验

首先做了一个练习项目,在本地项目目录进行了初始化命令:

  1. git init

在github网站新建了一个repository,勾选了Initialize this repository with a README,cloneSSH格式的仓库地址,现在开始提交:

  1. git add -A
  2. git commit -m "提交备注信息"//可选
  3. git remote add git@github.com:account/repository

错误及解决方案

提示出错信息:
fatal: remote origin already exists.
解决方案:

  1. git remote rm origin
  2. git remote add git@github.com:account/repository

这个错误应该不会再出现了。
然后提交的时候,又出现错误:
fatal: Could not read from remote repository.Please make sure you have the correct access rights.and the repository exists.
出现这个问题是因为,没有在github账号添加SSH key
解决方案: 这个错误可以用命令行处理掉,如下:

  1. ssh-agent
  2. ssh-add ~/.ssh/id_key

但简洁的方法是使用GitGui的show SSH Key工具,如图所示:
GitGui的show SSH Key工具
然后将生成的SSH Key复制,打开Github网站,在setting选项页中点击SSH and GPG keys链接,
SSH and GPG keys
点击右上角的New SSH Key按钮,将之前复制的SSH Key粘贴上去,title随便起个名字。
于此,这个错误已经解决了。
又出现新的错误:
failed to push some refs to ‘git@github.com:account/repository
原因在于github上创建仓库,建立README.md,导致该文件不在本地代码中,可以通过以下方式解决:

  1. git pull --rebase origin master
  2. //把远程服务器github上面的文件拉下来
  3. //再次执行git push origin master即可完成代码上传
  4. git push origin master

终于,项目上传到Git仓库中了。

Git清除本地缓存命令:

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

git设置全局忽略文件

  1. 在个人文件夹添加文件*.gitignore_global*,在里面写入你要全局忽略的内容,语法同.gitignore文件
  2. 运行命令git config --global core.excludesfile ~/.gitignore_global

git同步fork源仓库

  1. # checkout 出你要合并的目标分支,这里是master分支
  2. git checkout master
  3. # 下载(fetch)远程的代码变动
  4. git fetch upstream
  5. # 合并
  6. git rebase
  7. # 将合并后的代码push到你的远程分支上
  8. git push
  9. # 至此, origin的master branch已经于原作者项目同步了。

使用git merge, 会让你当前在working branch上面已经做的更改与upstream master的更改在timeline上出现分支. 而使用rebase, 会把你的更改加到upstream master更改的后面, 结果是整体时间轴呈线性的, 没有分岔。

git stash 使用

  1. 可以在一个分支上保存一个储藏,切换到另一个分支,然后尝试重新应用这些修改,git stash apply命令并不会删除stash列表的记录,要删除,可用如下命令:

    1. # 从栈上删除储藏
    2. git stash drop stash@{ 2}
    3. # 应用后立即删除记录
    4. git stash pop

git统计两个commit之间的文件变动数

  1. git diff master developer --shortstat # 还有类似的参数:stat, numstat.
  2. # 类似如下输出
  3. # 93 files changed, 3169 insertions(+), 1969 deletions(-)

术语

  • WIP:work in progress, do not merge yet 开发中
  • LGTM:looks good to me Review完别人的PR,没有问题
  • PTAL:please take a look 帮我看下,一般都是请别人review自己的PR
  • ACK — acknowledgement, i.e. agreed/accepted change
  • NACK/NAKnegative acknowledgement, i.e. disagree with change and/or concept
  • RFC — request for comments, i.e. I think this is a good idea, lets discuss
  • AFAIK/AFAICT — as far as I know / can tell
  • IIRC — if I recall correctly
  • IANAL — “I am not a lawyer”, but I smell licensing issues

发表评论

表情:
评论列表 (有 0 条评论,352人围观)

还没有评论,来说两句吧...

相关阅读