git初学(一)

本文是基于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 commitgit 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分支关联起来,在以后的推送或者拉取时就可以简化命令。

解释来自
https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/0013752340242354807e192f02a44359908df8a5643103a000

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   先拉后推

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值