Git和Github学习

如何使用Git和GitHub

本文主要是对Udacity学城上如何使用Git和GitHub这一课程的学习记录。
附上我随后对Git和Github学习的补充。


Course 1 : 操作提交历史

1. 课程概述

Git

Git is a version control system

GitHub

GitHub is a code sharing and collaboration platform

What we'll cover?

  • Lesson 1 : install git + red-only usage

  • Lesson 2 : read + write git

  • Lesson 3 : share + collaborate

2. 练习查找较大文件之间的差异

Automatically Compare Files

  • Windows —— FC (file compare)

FC favorite-app-old.html favorite-app.html

  • Mac & Linux —— Diff (difference)

diff -u favorite-app-old.html favorite-app.html

3. Windows配置Git Bash打开Sublime Text 3

  1. Find the directory where Sublime is located for you. For many people, this is C:/Program\ Files/Sublime\ Text\ 3/sublime_text.exe within Git Bash. To test this, run dir C:/Program\ Files/Sublime\ Text\ 3/sublime_text.exe. You should see sublime_text.exe listed. If you get the error No such file or directory, Sublime is located somewhere else for you and you'll need to find it. For example, it might be D:/Sublime\ Text\ 3/sublime_text.exe

  2. Run the following command in Git Bash: echo 'alias subl="C:/Program\ Files/Sublime\ Text\ 3/sublime_text.exe"' >> ~/.bashrc If Sublime was in a different directory for you in step 1, use that dictory.

  3. Close and re-open Git Bash.

  4. Type subl in Git Bash.

想要了解更多信息请点击此处

4. Git中的手动提交

Commits

  • In Git terminology, user-created checkpoints are called commits.

  • Commits are the basic building blocks of Git, each one representing a version of the conetent at one point in time.

  • Git requires the user to supply commit message each time a commit is created.

5. 使用Git浏览历史


cd version-control/asteroids

git log

diff --git a/game.js b/game.js

6. 多文件Git提交

Repository(版本库)

Git calls the collection of multiple files that you want to track together a repository.

git log --stat, the --stat option gives some statistics about which files have changed in each commit.

7. 克隆和探索版本库

克隆版本库


git clone https://github.com/udacity/asteroids.git

退出 git log

要停止查看 git log 的输出,请按 q (表示退出)。

获得彩色输出


git config --global color.ui atuo

git log

运行git log会列出最近的提交以及相关的信息(包括提交ID)

git diff

运行git diff(后跟两个提交ID)会比较这两个提交的代码版本


git diff old.txt nex.txt

8. Git 错误和警告

Should not be doing an octopus(不应执行octopus)

Octpus是 Git 用来合并多个不同代码版本的一种策略。如果在不合适的情况下尝试使用该策略,则可能会出现此消息。

You are in 'detached HEAD' state(你处于“分离的 HEAD”状态)

Git 将你目前所在的提交称为 HEAD。可通过切换到前一个提交来“分离”HEAD,这在下一个视频中有说明。虽然此警告听起来不太好,但分离 HEAD 实际上不是坏事。Git 只是向你发出警告,以便你知道自己正在这样做。

Panic! (the 'impossible' happened)((天哪!“不可能的事”发生了))

这是真正的错误消息,但它不是由 Git 输出的,而是由 GHC(编程语言 Haskell 的编译器)输出的。它仅在发生特别让人惊讶的错误时才会出现!

9. 检出代码的旧版本

git checkout

Checking out a commit means resetting all of your files to how they were at the time that commit was made.


git checkout

Course 2 : 习题集1

1. Old File Plus Diff

2. Tracking Versions Using Git

3. Git Command Review

4. Behaviour Of Git Clone

5. Behaviour Of Git Log

6. Behaviour Of Git Diff

7. Behaviour Of Git Checkout

8. Cloning A New Repository

9. Identifying A Bug

10. Fixing The Bug

11. Identifying A Second Bug

12. Fixing The Second Bug

Course 3 : 创建和修改代码库

1. 版本库有何特点?

Repository

For the most part it just looks like a normal directory on your computer. The only real difference is that git repositories store a bunch of metadata about the history of the repository since it was created.

显示隐藏文件.git,使用命令ls -a

2. 初始化版本库

git init

初始化或创建新的Git代码库

3. 检查新版本库

git status

To see that this is a git repository, you can run git status. It shows which files have changed since the last commit.

4. 暂存区

Staging area

You can add files one at a time to the staging area.


git add cake-recipe.txt

git add frosting-recipe.txt

git status

注意:如果你意外地将某个文件添加到暂存区中,可以使用git reset删除它。


git reset cake-recipe.txt

5. 提交更改

在提交更改过程中,我遇到一些问题。

  • 在第一次进行git commit的操作时,出现***Please tell me who you are.Run的信息

说明Git Bsah没有配置好,因此我们需要进行如下操作来解决这个问题


git config user.name "your name"

git config user.email "your [email protected]"

  • 在解决了这个问题之后,进行git commit时出现无法启动sublime的提示

经过google后,找到如下的解决办法


git config --global core.editor "'D:/Sublime Text 3/sublime_text.exe' -w -n"

注意:此处,在路径的空格之前不需要添加\转义字符!

6. 再次学习git diff

  • 当你找到导致错误的代码部分时,你可以进行修改,修改完成后,通过输入git diff可以比较working directorystaging area中修改的地方。

  • 而当你将修改后的文件通过git add添加至暂存区时,需要运行git diff --staged来显示你的修改(我理解为比较staging areaRepository中该文件的不同之处)。

  • 当你想要放弃工作目录或暂存区的所有更改时,你可以输入git reset --hard,取消你的更改。注意:在运行该命令时一定要小心,因为该命令无法撤销,你需要确定自己不需要这些更改,才能使用该命令。

注意:此时,HEAD仍处于检出旧提交时的“分离”状态,而下面的命令将修复该问题。


git checkout master

7. 分支

Branches

When you need to update your records and use this new commit as your real version, Git allows you to create labels for your commits.

Master

Master is the name given to main branch in most Git repositories and every time you create a repository, Git creates a master branch for you.

The tip of the branch

The current last commit on a branch.

Attention: If you check out a branch and then make a commit, the branch label automatically updates to the new commit. That branch also stays checked out, so you don't have to check it out again.

8. 创建分支

(1). You can use git branch to show yourself the current branches

(2). Run the following command to create the branch.


git branch easy-mode

(3). After create the easy-mode branch, you can run git branch to see what happens. The shar next to master means that master is the branch that is currently checked out.

(4). You need to run the command git checkout easy-mode in order to check out the branch.

(5). Then you can modify the codes and commit it.

9. 用分支协同工作

You can visualize the branch structure via the following command:


git log --graph --oneline master coins

10. 再次学习分离的HEAD

  • 当你检出顶点前的ID时,该ID上的提交的commit在检出到Master上就会变的不可访问(即git log无法显示该ID上的commit),除非你记得该ID。

  • 不过你可以使用下面的命令在该ID处创建一个分支:


git checkout -b new_branch_name

11. 通过命令行合并

  • You can run the following command to create a merged version of the two branches.

git merge master coins

  • When you want to see the diff between this commit and its parent, you can run the following command.

git show 3884#commit_id

  • Then if you don't need the coins branch, you can run the following command to delete it.

git branch -d coins

注意:由于是合并两个分支,因此在命令行中按何顺序键入分支并不重要。关键是要记住,git merge始终将所有指定的分支合并到当前检出的分支中,并为该分支新建一个提交。

  • 合并冲突

如果你收到类似以下所示的消息:


Auto-merging game.js

CONFLICT (content): Merge conflict in game.js

Automatic merge failed; fix conflicts and then commit the result.

这表示在你开始合并时,文件的状态不同于 Caroline 的文件状态。要修复此问题,请完成以下步骤:

  1. 运行 git merge --abort,将文件恢复到你开始合并之前的状态。

  2. 仔细检查文件的状态。如果在检出 master 分支时运行git log,则应看到 Caroline 的“Add color”提交是第二新的提交,而最新的提交应为你修复 bullet 错误的提交。如果使用 git diff将你的提交与 Caroline 的提交进行对比,你的提交应在第 424 行引入this.delayBeforeBullet = 10; 这行代码。应仅使用空格(无制表符)使该行的缩进程度与其下面一行相同,而且该行之后应无空格。

  3. 在文件处于正确的状态后,利用你所做的更改新建一个提交。

  4. 重新尝试合并。

  5. 更多内容请参考此页。

12. 解决合并冲突

当你合并分支时可能出现冲突的现象:


git checkout easy-mode

git merge master easy-mode

在输入上述代码后,可能跳出如下的说明:


Auto-merging game.js

CONFLICT (content): Merge conflict in game.js

Automatic merge failed; fix conflicts and then commit the result.

于是,就需要手动去解决此冲突。可以将自己修改的代码部分整合入其它人的代码中。在修改后,进行下一步的操作。


git status#查看git状态

git add game.js

git status#查看git状态

git commit

git log -n 1#查看第一条记录

Course 4 : 使用GitHub协作

1. 添加远程版本库

  1. 首先在GitHub上创建一个代码库

  2. 然后使用命令git remote查看和创建远程代码库

  3. 接着将GitHub上的代码库当作远程代码库,运行命令git remote add origin https://gith ub.com/evansyangs/reflections.git,其中origin为代码库名称,如果你只有一个远程代码库,标准做法是将其命名为origin,同时要提供该远程代码库的URL

  4. 再次运行git remote就可以看到origin远程代码库已经创建了

  5. 为验证添加的URL正确无误,可以运行git remote -v,输出更多信息

  6. 要向远程代码库发送更改,可以使用命令git push origin master,将本地代码库的master分支复制到远程代码库origin上

  7. 要从远程代码库拉回主分支时,可以使用命令git pull origin master,将远程代码库origin上的内容同步到本地代码库的master分支上

2. Fork版本库

在GitHub上你可以Fork其它用户的版本库,作为副本存放在自己的账号上,然后可以使用git clone克隆至自己的计算机中,进行本地的代码修改,再push至远程代码库的副本上。

3. 更新远程分支的本地副本

  • 如果你对每个代码库进行了不同的更新,即在本地进行了一项更新,然后在远程进行了另一项更新,这将有可能产生冲突。

  • 你可以通过运行命令git fetch更新远程分支的本地副本,而实际的本地版本保持不动。

  • 然后你可以使用git merge将它们包含在master分支中。

  • 实际上当你运行git pull时发生的正是这种情况。首先,远程分支会被取回,更新远程分支的本地副本,然后该分支与本地分支进行合并。

  • git pull origin master=git fetch origin+git merge master origin/master

4.创建拉取请求

GitHub提供了一种叫pull request的协作方式,这可以请求对方将一些修改加入到master分支中,方便自己拉取。

5. 保持Fork最新

你可以使用以下命令git remote add upstream https://github.com/udacity/create-your-own-adventure.git与远程代码库连接,然后通过git pull upstream master来更新本地库,接着使用git log查看修改,并合并修改,最后使用命令git push origin master保持Fork最新。

感谢阅读!

你可能感兴趣的:(Git和Github学习)