Git学习笔记

Git学习笔记

这学期有一门课的老师要求在github提交作业,很多同学之前没接触过git,当然也包括我。于是在提交过作业的过程中遇到各种问题,今天就把git的基本知识记录下来。

git可视化工具

个人比较喜欢SourceTree,下面介绍最基本的git命令行的的使用。

创建本地仓库

第一步当然是打开终端,在计算机上创建一个目录,然后进入这个目录。在这个目录中使用:

localhost:testRepo zhouzhou$ mkdir testRepo
localhost:testRepo zhouzhou$ cd testRepo/
localhost:testRepo zhouzhou$ git init
Initialized empty Git repository in /Users/zhouzhou/Documents/  iosProjectEx/testRepo/.git/

就可以把这个目录变成git的仓库了。在这个目录中新建一个txt文件。

$ touch test.txt

接下来把这个文件添加到仓库:

$ git add test.txt

然后使用把这个文件提交到仓库:

localhost:testRepo zhouzhou$ git commit -m "add a file"
[master (root-commit) ccb3bf0] add a file
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test.txt

好啦,现在已经把这个文件加入本地git仓库了。下面重点讲一下在把本地仓库同步到GitHub远程仓库中的常见问题。

GitHub远程仓库

我在Github创建一个仓库叫做testRepo的仓库,并且选着了初始化README.md文件,那么新建的远程仓库里就有了README.md这个文件。

如果在这个电脑第一次使用GitHub仓库,接来下还要配置GitHub的SSH Key,这里就不赘述了。

我想把本地仓库与这个远程仓库绑定,这样就能把我本地文件发布到GitHub上保存了。接着输入:

$ git remote add origin git@github.com:username/testRepo.git

username是你自己GitHub账号的用户名。这样就能把本地仓库关联到GitHub的远程仓库了。这个远程仓库的名字就叫origin,当然你也可以使用你喜欢的名字。

下面就可以本地库的内容推送到远程库上:

localhost:testRepo zhouzhou$ git push -u origin master
To git@github.com:zhoudaxiaking/testRepo
! [rejected]        master -> master (fetch first)
error: failed to push some refs to  'git@github.com:zhoudaxiaking/testRepo'
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.

出错了!why?根据提示Updates were rejected because the remote contains work that you do not have locally. This is usually caused by another repository pushing to the same ref. You may want to first integrate the remote changes知道这是因为GitHub上的仓库,按照提示的解决方法 (e.g., 'git pull ...') before pushing again.,应该先把GitHub仓库中的文件Pull到本地仓库,然后再Push本地仓库到GitHub的仓库。

localhost:testRepo zhouzhou$ git pull origin master
From github.com:zhoudaxiaking/testRepo
* branch            master     -> FETCH_HEAD
Merge made by the 'recursive' strategy.
README.md | 2 ++
1 file changed, 2 insertions(+)
create mode 100644 README.md

好啦,Github仓库中的文件已经加载到本地仓库了,查看本地仓库

localhost:testRepo zhouzhou$ ls
README.md   test.txt

README.md文件应经在本地仓库了。然后再把本地仓库master分支的修改推送到远程仓库吧!

localhost:testRepo zhouzhou$ git push origin master
Counting objects: 6, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (5/5), 506 bytes | 0 bytes/s, done.
Total 5 (delta 0), reused 0 (delta 0)
To git@github.com:zhoudaxiaking/testRepo
b84fd1e..5c95663  master -> master

已经完成了。打开GitHub看看吧!

下面是两个链接,他们能让你进一步掌握git的用法!


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值