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

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

使用Git初体验

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

git init

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

git add -A
git commit -m "提交备注信息"//可选
git remote add [email protected]:account/repository

错误及解决方案

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

git remote rm origin
git remote add [email protected]: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
解决方案: 这个错误可以用命令行处理掉,如下:

ssh-agent
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 '[email protected]:account/repository
原因在于github上创建仓库,建立README.md,导致该文件不在本地代码中,可以通过以下方式解决:

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

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

Git清除本地缓存命令:

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

git设置全局忽略文件

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

git同步fork源仓库

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

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

git stash 使用

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

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

git diff master developer --shortstat # 还有类似的参数:stat, numstat.
# 类似如下输出
# 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章