An Intro to Git and GitHub

An Intro to Git and GitHub

首先,要明白Git和GitHub是两种东西。Git和GitHub之间的关系就像是Java和Eclipse的关系。

Git的定义:

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

GitHub的定义:

GitHub is a Git hosting repository that provides developers with tools to ship better code through command line features, issues (threaded discussions), pull requests, code review, or the use of a collection of free and for-purchase apps in the GitHub Marketplace.

总的来说,Git是一种管理方法,而GitHub是你放文件的地方,是Git发挥功能的平台。

上面GitHub的定义中,提到了repository,那什么是repository呢?

A Repository, or Git project, encompasses the entire collection of files and folders associated with a project, along with each file’s revision history.

Repository就是一个project,它看起来就是一个文件夹,但是它包含了这个文件夹的历史记录,比如说,这个project有几个版本,什么时候添加了什么文件之类的。

我这里只介绍用Git command line only(使用的是Mac的terminal). 也会用到GitHub的官方网页。所以需要两件事情:

  1. 安装Git;
  2. 注册GitHub.

(Mac terminal tutorial)
basic terminal commands:

  • cd path 转到path目录下
  • mkdir folder_name 建立一个文件夹
  • echo “some text here” >> file.txt 写一些文字到文件里
  • touch 建立一个文件
  • ls 显示文件夹下面的文件( -a 可以显示隐藏文件)

Step 1: Create a local git repository

首先创建一个普通的文件夹。(可以手动创建)

cd ~/Documents
mkdir myproject

进入这个文件夹

cd myproject

然后把它变成repository:

git init

myproject文件夹下面就会有一个隐藏文件夹.git,里面记录了这个文件的修改历史和不同版本

Step 2: Add a new file to the repository

创建一个文件(可以手动创建)

$ touch file1.txt

这个时候git会注意到文件夹有变化,但是它不会更新project。

git status
$ git status
On branch master
Initial commit
Untracked files:
  (use "git add <file>..." to include in what will be committed)
     file1.txt
nothing added to commit but untracked files present (use "git add" to track)

我们也可以创建一个文件夹,但是这个文件夹里面一定要有文件,不然git是不会管它的。

The difference between staging environment and the commit:

  • commit(名词):是你修改project的记录,commits可以使你可以回到之前的状态。
  • staging environment or index:要修改的文件。
    当修改完文件后,git project会注意到这些变化,但是不会发生改变,要把你觉得可行的修改,加到index里面,然后commit。

Step 3: Add a file to the staging environment

git add file1.txt (如果你修改了很多文件,可以使用 git add . )

如果你这个时候突然不想修改file1.txt,可以使用以下命令:

git rm —cached file1.txt    这个不会删除本地的文件
git rm file1.txt            会删除本地文件

Step 4: Create a commit

git commit -m "Some messages here about the commit"

如果不填写message,这个commit是不生效的。

Step 5: Create a new branch

上面的操作都是在master branch里面操作的。比如说你设计了一个基础的网页app,这个时候你想把这个网页美化一下,但是又担心中间出问题,在没使用GitHub之前,我们可以复制一份,在复制的文件上进行修改。Git可以非常简单的帮助我们处理这种情况。类似以前的做法,原来的branch叫做master,复制的是一个新的branch,我们可以给它起个名字:

git checkout -b my-new-branch

我们可以使用git branch来查看有多少个branch, 其中带 * 的是当前使用的branch。

如何在不同branch之间切换:

git checkout branch_name

我们可以注意到,当我们切换不同branch时,我们的文件夹里面的内容也会随之改变,显示当前branch的版本

Step 6: Create a new repository on GitHub

登录GitHub,create new repository. 简单。
创建完之后,GitHub 会提供一个指向remote repository 的链接
我们可以连接到这个remote repository,并把master branch里面的文件传上去:

$ git remote add origin https://github.com/cubeton/mynewrepository.git
$ git push -u origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 263 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/cubeton/mynewrepository.git
* [new branch]      master -> master
Branch master set up to track remote branch master from origin.

Step 7: Push a branch to GitHub

第一次use GitHub locally, 会让我们输入username and password.

$ git push origin my-new-branch
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 313 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/cubeton/mynewrepository.git
* [new branch]      my-new-branch -> my-new-branch

“origin”的意义:
其实是远程repository的别名

You might be wondering what that “origin” word means in the command above. What happens is that when you clone a remote repository to your local machine, git creates an alias for you. In nearly all cases this alias is called “origin.” It’s essentially shorthand for the remote repository’s URL. So, to push your changes to the remote repository, you could’ve used either the command: git push git@github.com:git/git.git yourbranchname or git push origin your_branch_name

Step 8: Create a Pull Request (PR)

A pull request (or PR) is a way to alert a repo’s owners that you want to make some changes to their code. It allows them to review the code and make sure it looks good before putting your changes on the master branch.

PR是为merge做准备的,所以这里可以选择merge which branch into which branch

Step 9: Merge a PR

Go ahead and click the green ‘Merge pull request’ button. This will merge your changes into the master branch.

这里merge完之后,newbranch不会自动删除,还会继续存在,可以自己手动删掉。

You can also see the hash code of the commit on the right hand side. A hash code is a unique identifier for that specific commit. It’s useful for referring to specific commits and when undoing changes (use the git revert <hash code number> command to backtrack).

Step 10: Get changes on GitHub back to your computer

In order to get the most recent changes that you or others have merged on GitHub, use the

git pull origin master

command (when working on the master branch).

Now we can use the git log command again to see all new commits.

More About GitHub:

Fork vs. Git clone
Fork是GitHub的功能,并不存在于git命令中。fork是server端的clone。
当我们不是一个project的contributor时,我们没办法对这个project做修改(push),但是我们可以把这个项目复制到自己的GitHub中,当作自己的东西,随意修改。这个复制就是fork。

参考文章:
An Intro to Git and GitHub for Beginners (Tutorial)
Git Handbook
Fork vs. Git clone

    原文作者:jShawn
    原文地址: https://segmentfault.com/a/1190000015352330
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞