Code School Try Git git互动课程

Code School Try Git git互动教程

> git init

Initialized empty Git repository in /.git/

Success!

$ git status

# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)

Success!


当创建一个文件:octocat.txt 后

$ git status

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


现在要把这个文件加入到git的准备区域(staging area):

$ git add octocat.txt

$ git status

# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: octocat.txt
#



$ git commit -m "Add cute octocat story"

[master (root-commit) 20b5ccd] Add cute octocat story
1 file changed, 1 insertion(+)
create mode 100644 octocat.txt


现在目录底下又添加了多个txt文件:

$ git add '*.txt'

$ git commit -m 'Add all the octocat txt files'

[master 3852b4d] Add all the octocat txt files
4 files changed, 4 insertions(+)
create mode 100644 blue_octocat.txt
create mode 100644 octofamily/baby_octocat.txt
create mode 100644 octofamily/momma_octocat.txt
create mode 100644 red_octocat.txt


可以查看commit记录:

$ git log

commit 3852b4db1634463d0bb4d267edb7b3f9cd02ace1
Author: Try Git <try_git@github.com>
Date: Sat Oct 10 08:30:00 2020 -0500

Add all the octocat txt files

commit b652edfd888cd3d5e7fcb857d0dabc5a0fcb5e28
Author: Try Git <try_git@github.com>
Date: Sat Oct 10 08:30:00 2020 -0500

Added cute octocat story


现在在Github有一个repository:https://github.com/try-git/try_git.git

现在要配置,把这个地址作为远程的repository

$ git remote add origin https://github.com/try-git/try_git.git


现在远程的repository名叫做origin,默认本地的分支叫做master,-u是让git记住参数,下一次就可以直接git push

$ git push -u origin master

Branch master set up to track remote branch master from origin.


从github上下载到本地:

$ git pull origin master

Updating 3852b4d..3e70b0f
Fast-forward
yellow_octocat.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 yellow_octocat.txt


现在有一些变化,可以用git diff查看变化,HEAD表示最近一次commit

$ git diff HEAD

diff --git a/octocat.txt b/octocat.txt
index 7d8d808..e725ef6 100644
--- a/octocat.txt
+++ b/octocat.txt
@@ -1 +1 @@
-A Tale of Two Octocats
+[mA Tale of Two Octocats and an Octodog


还可以用diff来查看已经被staged(即未被commit)的文件

所以先添加一个文件:

$ git add octofamily/octodog.txt

$ git diff --staged

diff --git a/octofamily/octodog.txt b/octofamily/octodog.txt
new file mode 100644
index 0000000..cfbc74a
--- /dev/null
+++ b/octofamily/octodog.txt
@@ -0,0 +1 @@
+[mwoof


可以用git reset来unstage一个staged的文件:

$ git reset octofamily/octodog.txt


可以用git checkout -- <target> 回到最后一次commit <target>

$ git checkout -- octocat.txt


创立一个新的branch:

$ git branch clean_up


切换到<branch>: git checkout <branch>

$ git checkout clean_up

Switched to branch 'clean_up'


用git rm移除所有硬盘中的文件和staged的文件

$ git rm '*.txt'

rm 'blue_octocat.txt'
rm 'octocat.txt'
rm 'octofamily/baby_octocat.txt'
rm 'octofamily/momma_octocat.txt'
rm 'red_octocat.txt'


$ git commit -m "Remove all the cats"

[clean_up 63540fe] Remove all the cats
5 files changed, 5 deletions(-)
delete mode 100644 blue_octocat.txt
delete mode 100644 octocat.txt
delete mode 100644 octofamily/baby_octocat.txt
delete mode 100644 octofamily/momma_octocat.txt
delete mode 100644 red_octocat.txt


$ git checkout master


合并clean_up branch:

$ git merge clean_up

Updating 3852b4d..ec6888b
Fast-forward
blue_octocat.txt | 1 -
octocat.txt | 1 -
octofamily/baby_octocat.txt | 1 -
octofamily/momma_octocat.txt | 1 -
red_octocat.txt | 1 -
5 files changed, 5 deletions(-)
delete mode 100644 blue_octocat.txt
delete mode 100644 octocat.txt
delete mode 100644 octofamily/baby_octocat.txt
delete mode 100644 octofamily/momma_octocat.txt
delete mode 100644 red_octocat.txt


可以用git branch -d <branch name> 来删除某个branch

$ git branch -d clean_up

Deleted branch clean_up (was ec6888b).


$ git push

To https://github.com/try-git/try_git.git
3e70b0f..132b453 master -> master


http://try.github.io/levels/1/challenges/1

关于Github和一些Git命令(更新中)