本文是基于git bash来管理仓库的(右击鼠标,就可以看到git bash的选项,点击进入即可)。这个bash是基于linux的,所以需要熟悉一下命令行。
常用命令行
ls:显示当前文件夹下的文件情况
mkdir:新建文件夹
pwd:显示绝对路径,
cd,切换目录
touch <file>:新建一个文件
cat <file>:打印文件
初始化
初始化就是登陆你的用户信息,昵称和邮箱(之前在github上处理好的)。在一台电脑上登陆一次之后,就无需再初始化了。
git config --global user.name...
git config --global user.email ....
git config --list (查看配置信息)
本地仓库管理
git init :创建一个空的版本库,在本地生成一个.git的隐藏文件夹,它是Git来跟踪管理版本库的,不要随意人为修改。
file的传递路径为:工作区(Working Directory)----->暂存区----->版本库(Repository)
git add <file>:把工作区的文件修改加入到暂存区
git rm <file>:删除暂存区的file
(注意:rm <file>:删除工作区的file)
git commit -m '说明':把暂存区的所有内容提交到当前分支,rm和add之后都需要commit一下
注意:工作区的每次修改,如果不用git add
到暂存区,那就不会加入到commit
中。
git status:会有几种文件类型:new file,modified,Untracked files,第三个是还未添加到暂存区的。在commit后,而且工作区无改动,则会显示 nothing to commit。
git diff HEAD -- <file>
命令可以查看工作区和版本库里面最新版本的区别
两种撤销修改的方法
git checkout -- file:
可以丢弃工作区错误的修改或者删除操作,让这个文件回到最近一次git commit
或git add
时的状态(最新版本)。发生在git add之前的补救。
git reset HEAD <file>:
可以把暂存区的修改撤销掉,重新放回工作区,其中HEAD表示最新版本。发生在git commit的补救。
本地仓库与远程仓库的连接
首先在github上建立一个仓库,
git remote add origin git@github.com:<username>/repository name.git 让二者关联
git push -u origin master :如果远程库是空的,第一次推送master
分支时,加上-u
参数,这样Git会把本地的master
分支和远程的master
分支关联起来,在以后的推送或者拉取时就可以简化命令。
git clone 支持多种协议:
ssh
支持的原生git
协议速度更快:git clone git@github.com
http协议更通用:git clone https://github.com...
git push -u origin master的一些错误
错误一:
The authenticity of host 'github.com (13.229.188.59)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,13.229.188.59' (RSA) to the list of known hosts.
(前面这些是正常的,第一次关联都会出现)
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
后面报错了,经分析应该是github账号上不存在SSH keys。所以需要自己配置了。具体操作参考博客:https://blog.csdn.net/isunnyvinson/article/details/52598863
错误二:
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'git@github.com:melodyhappy/RL.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
解决办法1:git push -f --set-upstream origin master
-f是强制push的意思
解决办法2:git pull --rebase 先拉后推