git mac使用初体验(三)

来源:互联网 发布:淘宝怎么买枪暗号 编辑:程序博客网 时间:2024/05/21 09:23

本文接着上一章git 与远程仓库的协作(二),当我们从github 上clone下来仓库代码后,我们要做修改和提交:

  • 仓库状态的查询

  • 文件修改并添加到暂存区

  • 提交到代码管理repo

  • *push 到远程仓库github


仓库状态的查询

打开终端进入git clone下来的项目目录,例如从github上clone的是test项目,现在就是进入到test目录;

  • ls -a

    查看项目目录下文件

    xu:test xiaokai$ ls -a  //查看test目录下有哪些文件.       ..      .git        README.md//.git 文件是隐藏文件,是版本控制repo, README.md是从github上获取的一个文件
  • git remote -v

    查看指向的远程仓库

    xu:test xiaokai$ git remote -v  origin  https://github.com/githubname/test (fetch)origin  https://github.com/githubname/test (push
  • git config –list –global

    查看全局配置的name 和 email

     xu:test xiaokai$ git config --list --global user.name=yourname user.email=youremail@qq.com alias.ci=commit   //这个是第一篇文章配置的commit的别名
  • git status

    查看状态

    xu:test xiaokai$ git statusOn branch master//以下信息提示,说明clone后项目没有变动Your branch is up-to-date with 'origin/master'.nothing to commit, working tree clean

文件修改并添加到暂存区

  • touch a b

    创建文件a 文件b

    xu:test xiaokai$ touch a bxu:test xiaokai$ lsREADME.md   a       b    //多出来文件a   文件b
  • git status

    查看状态

    xu:test xiaokai$ git statusOn branch masterYour branch is up-to-date with 'origin/master'.Untracked files:  (use "git add <file>..." to include in what will be committed)    a    bnothing added to commit but untracked files present (use "git add" to track)

    这段文字的解释:

    注:Untracked files: 未注册的文件,最后一行有提示,use "git add" to track,实现注册
  • git add

    添加到“暂存区” stage

    git add . //注意要有“ . ”,添加整个目录下的文件到“暂存区”
    git add a //添加文件到“暂存区”

    xu:test xiaokai$ git add .xu:test xiaokai$ git statusOn branch masterYour branch is up-to-date with 'origin/master'.Changes to be committed:  (use "git reset HEAD <file>..." to unstage)    new file:   a    new file:   b

    贴上所有代码片段

xu:test xiaokai$ touch a bxu:test xiaokai$ lsREADME.md   a       bxu:test xiaokai$ git statusOn branch masterYour branch is up-to-date with 'origin/master'.Untracked files:  (use "git add <file>..." to include in what will be committed)    a    bnothing added to commit but untracked files present (use "git add" to track)xu:test xiaokai$ git add .xu:test xiaokai$ git statusOn branch masterYour branch is up-to-date with 'origin/master'.Changes to be committed:  (use "git reset HEAD <file>..." to unstage)    new file:   a    new file:   b

提交到代码管理repo

  • git commit -m “这里添加注释”

    提交到本地仓库

        xu:test xiaokai$ git commit -m "add a b" //添加到本地仓库,repo        [master 15c11d4] add a b        2 files changed, 0 insertions(+), 0 deletions(-)        create mode 100644 a        create mode 100644 b        xu:test xiaokai$ git status  //查看状态        On branch master        Your branch is ahead of 'origin/master' by 1 commit.             (use "git push" to publish your local commits)        nothing to commit, working tree clean

在添加后,通过git status 查看状态时提示,在master主分支上有一个提交,在括号中也有提示 (use “git push” to publish your local commits),使用git push 推送到github 上,

push 到远程仓库github

这里有一点要说,如果是多人协作开发,在git push 前,要先执行 git pull(更新服务器上的内容到本地),

  • git pull

    更新服务器上的内容到本地

    xu:test xiaokai$ git pullAlready up-to-date.  //更新成功
  • git push origin master

    push刚才的提交到github上,origin 是指推送的github仓库,就是使用git remote -v 得到仓库地址的代名词,master 推送到github 上的主分支上,

    xu:test xiaokai$ git push origin masterCounting objects: 3, done.Delta compression using up to 4 threads.Compressing objects: 100% (2/2), done.Writing objects: 100% (3/3), 268 bytes | 0 bytes/s, done.Total 3 (delta 0), reused 1 (delta 0)To https://github.com/Kenway090704/testxu:test xiaokai$ git statusOn branch masterYour branch is up-to-date with 'origin/master'.nothing to commit, working tree clean

    这样我们就实现了一次推送文件到github上。
    登录github,刷新,可以看到我们推送的a b文件已经存在了。

这里写图片描述

贴上代码片段

xu:test xiaokai$ git pullAlready up-to-date.xu:test xiaokai$ git push origin masterCounting objects: 3, done.Delta compression using up to 4 threads.Compressing objects: 100% (2/2), done.Writing objects: 100% (3/3), 268 bytes | 0 bytes/s, done.Total 3 (delta 0), reused 1 (delta 0)To https://github.com/Kenway090704/test   af6b54e..15c11d4  master -> masterxu:test xiaokai$ git statusOn branch masterYour branch is up-to-date with 'origin/master'.nothing to commit, working tree cleanxu:test xiaokai$ 

该篇文章主要是最简单的一个使用,实际开发中,要有许多命令用到,有时间再写开发中git的使用。

0 0
原创粉丝点击