您的位置:首页 > 其它

Git入门基础命令

2016-11-14 20:26 537 查看

git init:初始化一个repository。

实例:

lewis@DESKTOP-IK0DNHT MINGW64 ~/2333
$ git init
Initialized empty Git repository in C:/Users/lewis/2333/.git/


git clone: 从网络clone到本地。

实例:

lewis@DESKTOP-IK0DNHT MINGW64 ~
$ git clone https://github.com/udacity/asteroids.git Cloning into 'asteroids'...
remote: Counting objects: 209, done.
remote: Total 209 (delta 0), reused 0 (delta 0), pack-reused 209
Receiving objects: 100% (209/209), 199.25 KiB | 25.00 KiB/s, done.
Resolving deltas: 100% (126/126), done.


git log: 显示详细的每次commit的日志。

实例:

$ git log
commit b0678b161fcf74467ed3a63110557e3d6229cfa6
Author: cbuckey <caroline@udacity.com>
Date:   Mon May 24 04:15:21 2010 -0700

Revert controls

commit f19cb1b80fe27e938e4d72770ca0a42f25e99ecc
Author: cbuckey <caroline@udacity.com>
Date:   Mon May 24 04:03:05 2010 -0700

Fix typo in space

commit 75928a98e18479b22b18888a33d36379f17b43c1
Author: cbuckey <caroline@udacity.com>
Date:   Mon May 24 03:54:42 2010 -0700

Use space for movement and enter for shooting

commit ac83b72030d79cf35944125793efcbdf57d93635
Author: Doug McInnes <doug@dougmcinnes.com>
Date:   Sun May 23 00:01:21 2010 -0700

mostly finished ipad version




git diff :

查询2个commit的id之间的详细差异,比如删去,添加了哪些代码。

实例:

lewis@DESKTOP-IK0DNHT MINGW64 ~/asteroids (master)
$ git diff 4035769377cce96a88d5c1167079e12f30492391 25ede836903881848fea811df5b6                                                                                        87b59d962da3
diff --git a/game.js b/game.js
index 7595a9d..5daadb0 100644
--- a/game.js
+++ b/game.js
@@ -1167,8 +1167,7 @@ $(function () {
}
};

-  var frameInterval = 25;
-  var mainLoopId = setInterval(mainLoop, frameInterval);
+  var mainLoopId = setInterval(mainLoop, 25);

$(window).keydown(function (e) {
switch (KEY_CODES[e.keyCode]) {
@@ -1182,7 +1181,7 @@ $(function () {
Text.renderText('PAUSED', 72, Game.canvasWidth/2 - 160, 120);
} else {
lastFrame = Date.now();
-          mainLoopId = setInterval(mainLoop, frameInterval);
+          mainLoopId = setInterval(mainLoop, 10);
}
break;
case 'm': // mute
:...skipping...
diff --git a/game.js b/game.js
index 7595a9d..5daadb0 100644
--- a/game.js
+++ b/game.js
@@ -1167,8 +1167,7 @@ $(function () {
}
};

-  var frameInterval = 25;
-  var mainLoopId = setInterval(mainLoop, frameInterval);
+  var mainLoopId = setInterval(mainLoop, 25);

$(window).keydown(function (e) {
switch (KEY_CODES[e.keyCode]) {
@@ -1182,7 +1181,7 @@ $(function () {
Text.renderText('PAUSED', 72, Game.canvasWidth/2 - 160, 120);
} else {
lastFrame = Date.now();
-          mainLoopId = setInterval(mainLoop, frameInterval);
+          mainLoopId = setInterval(mainLoop, 10);
}
break;
case 'm': // mute


git checkout:切换到具体commit的id的分支。

实例:

lewis@DESKTOP-IK0DNHT MINGW64 ~/asteroids (master)
$ git checkout 25ede836903881848fea811df5b687b59d962da3
Note: checking out '25ede836903881848fea811df5b687b59d962da3'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

git checkout -b <new-branch-name>

HEAD is now at 25ede83... a couple missing ends with the ipad version


git status:查询 repository的状态。

实例:

lewis@DESKTOP-IK0DNHT MINGW64 ~/asteroids ((25ede83...))
$ git status
HEAD detached at 25ede83
nothing to commit, working tree clean


git add:添加文件到暂存区

实例:

lewis@DESKTOP-IK0DNHT MINGW64 ~/bilibili_spider/reflections (master)
$ git status
On branch master

Initial commit

Changes to be committed:
(use "git rm --cached <file>..." to unstage)

new file:   lesson_2_reflections.txt

Untracked files:
(use "git add <file>..." to include in what will be committed)

reflection.txt

lewis@DESKTOP-IK0DNHT MINGW64 ~/bilibili_spider/reflections (master)
$ git add reflection.txt

lewis@DESKTOP-IK0DNHT MINGW64 ~/bilibili_spider/reflections (master)
$ git status
On branch master

Initial commit

Changes to be committed:
(use "git rm --cached <file>..." to unstage)

new file:   lesson_2_reflections.txt
new file:   reflection.txt

lewis@DESKTOP-IK0DNHT MINGW64 ~/bilibili_spider/reflections (master)
$


git commit :提交暂存区的文件到git

示例:

lewis@DESKTOP-IK0DNHT MINGW64 ~/bilibili_spider (master)
$ git commit
[master (root-commit) 85b0105] test commit
2 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 reflections/lesson_2_reflections.txt
create mode 100644 reflections/reflection.txt

lewis@DESKTOP-IK0DNHT MINGW64 ~/bilibili_spider (master)
$ git status
On branch master
nothing to commit, working tree clean

lewis@DESKTOP-IK0DNHT MINGW64 ~/bilibili_spider (master)
$ git log
commit 85b0105896b8e8daa31b5f381e14832c60adeeed
Author: qq1367212627 <1367212627@qq.com>
Date:   Tue Nov 15 10:59:07 2016 +0800

test commit

2016/11/15

Powered By Lewis

lewis@DESKTOP-IK0DNHT MINGW64 ~/bilibili_spider (master)


git diff与git diff --staged比较图:



以下命令先维护了2个文件,提交game.js到暂存区(staging area),分别与工作区(working direction)和存库区(repository)比较,最后使用git reset --hard清除staging area。

lewis@DESKTOP-IK0DNHT MINGW64 ~/asteroids (master)
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)

modified:   game.js
modified:   index.html

no changes added to commit (use "git add" and/or "git commit -a")

lewis@DESKTOP-IK0DNHT MINGW64 ~/asteroids (master)
$ git didd
git: 'didd' is not a git command. See 'git --help'.

Did you mean this?
diff

lewis@DESKTOP-IK0DNHT MINGW64 ~/asteroids (master)
$ git diff
diff --git a/game.js b/game.js
index 49bf5ea..8809e58 100644
--- a/game.js
+++ b/game.js
@@ -421,6 +421,7 @@ Ship = function () {
}
if (KEY_STATUS.space) {
if (this.delayBeforeBullet <= 0) {
+         this.delayBeforeBullet = 10;
for (var i = 0; i < this.bullets.length; i++) {
if (!this.bullets[i].visible) {
SFX.laser();
diff --git a/index.html b/index.html
index 8aa618d..ac21916 100644
--- a/index.html
+++ b/index.html
@@ -23,8 +23,7 @@
<div id="left" class='button'>LEFT</div>
<div id="right" class='button'>RIGHT</div>
</div>
-      <div id="right-controls">
-        <div id="space" class='button'>FIRE</div>
+      <div id="right-controls"><div id="space" class='button'>FIRE</div>
</div>
</div>
</body>

lewis@DESKTOP-IK0DNHT MINGW64 ~/asteroids (master)
$ git add game.js

lewis@DESKTOP-IK0DNHT MINGW64 ~/asteroids (master)
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

modified:   game.js

Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)

modified:   index.html

lewis@DESKTOP-IK0DNHT MINGW64 ~/asteroids (master)
$ git --staged
Unknown option: --staged
usage: git [--version] [--help] [-C <path>] [-c name=value]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
<command> [<args>]

lewis@DESKTOP-IK0DNHT MINGW64 ~/asteroids (master)
$ git diff --staged
diff --git a/game.js b/game.js
index 49bf5ea..8809e58 100644
--- a/game.js
+++ b/game.js
@@ -421,6 +421,7 @@ Ship = function () {
}
if (KEY_STATUS.space) {
if (this.delayBeforeBullet <= 0) {
+         this.delayBeforeBullet = 10;
for (var i = 0; i < this.bullets.length; i++) {
if (!this.bullets[i].visible) {
SFX.laser();

lewis@DESKTOP-IK0DNHT MINGW64 ~/asteroids (master)
$ git commit
[master 8fc4917] fix bug
1 file changed, 1 insertion(+)

lewis@DESKTOP-IK0DNHT MINGW64 ~/asteroids (master)
$ git diff
diff --git a/index.html b/index.html
index 8aa618d..ac21916 100644
--- a/index.html
+++ b/index.html
@@ -23,8 +23,7 @@
<div id="left" class='button'>LEFT</div>
<div id="right" class='button'>RIGHT</div>
</div>
-      <div id="right-controls">
-        <div id="space" class='button'>FIRE</div>
+      <div id="right-controls"><div id="space" class='button'>FIRE</div>
</div>
</div>
</body>

lewis@DESKTOP-IK0DNHT MINGW64 ~/asteroids (master)
$ git diff --staged

lewis@DESKTOP-IK0DNHT MINGW64 ~/asteroids (master)
$ git reset --hard
HEAD is now at 8fc4917 fix bug

lewis@DESKTOP-IK0DNHT MINGW64 ~/asteroids (master)
$ git diff

lewis@DESKTOP-IK0DNHT MINGW64 ~/asteroids (master)
$ git reset --hard
HEAD is now at 8fc4917 fix bug

lewis@DESKTOP-IK0DNHT MINGW64 ~/asteroids (master)
$ git diff --staged

lewis@DESKTOP-IK0DNHT MINGW64 ~/asteroids (master)
$ git log
commit 8fc49178ebbe70f57df54a89919ca01efb38ebac
Author: qq1367212627 <1367212627@qq.com>
Date:   Tue Nov 15 11:41:24 2016 +0800

fix bug

Powered By Lewis

2016/11/15


git branch:查询/创建新的分支(git branch 分支名)

以下创建了一个名为:easy-mode的分支,并且将分支从master切换到easy-mode

lewis@DESKTOP-IK0DNHT MINGW64 ~/asteroids (master)
$ git branch
* master

lewis@DESKTOP-IK0DNHT MINGW64 ~/asteroids (master)
$ git branch easy-mode

lewis@DESKTOP-IK0DNHT MINGW64 ~/asteroids (master)
$ git branch
easy-mode
* master

lewis@DESKTOP-IK0DNHT MINGW64 ~/asteroids (master)
$ git checkout
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)

lewis@DESKTOP-IK0DNHT MINGW64 ~/asteroids (master)
$ git checkout easy-mode
Switched to branch 'easy-mode'

lewis@DESKTOP-IK0DNHT MINGW64 ~/asteroids (easy-mode)
$ git branch
* easy-mode
master


文件合并策略示意图:





git remote: 查看远程仓库源

lewis@DESKTOP-IK0DNHT MINGW64 ~/reflection (master)
$ git remote
origin


git remote -v:查看远程仓库详细地址

lewis@DESKTOP-IK0DNHT MINGW64 ~/reflection (master)
$ git remote -v
origin  https://github.com/qq1367212627/reflection.git (fetch)
origin  https://github.com/qq1367212627/reflection.git (push)


git push:提交到github(远程仓库)上(向上更新)

lewis@DESKTOP-IK0DNHT MINGW64 ~/reflection (master)
$ git push
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 284 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/qq1367212627/reflection.git d6024ab..67b2b6b  master -> master



git pull:从Github拉取commit,同步代码(向下更新)。

lewis@DESKTOP-IK0DNHT MINGW64 ~/reflection (master)
$ git log
commit 3913ff943b35f42a645c77deb95c0c0006dc3a91
Author: qq1367212627 <1367212627@qq.com>
Date:   Tue Nov 15 21:13:53 2016 +0800

commit 3

commit 67b2b6b412248367731c193aa0f6fed683b194f9
Author: qq1367212627 <1367212627@qq.com>
Date:   Tue Nov 15 20:24:46 2016 +0800

测试
test
lewis

commit d6024ab8d0fe649d2e17db5b267b817ce1a78471
Author: qq1367212627 <1367212627@qq.com>
Date:   Tue Nov 15 20:15:31 2016 +0800

Initial commit

lewis@DESKTOP-IK0DNHT MINGW64 ~/reflection (master)
$ git pull

orremote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
ginremote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/qq1367212627/reflection 3913ff9..cd96efe  master     -> origin/master
Updating 3913ff9..cd96efe
Fast-forward
new_file.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 new_file.txt

lewis@DESKTOP-IK0DNHT MINGW64 ~/reflection (master)
$

lewis@DESKTOP-IK0DNHT MINGW64 ~/reflection (master)
$ git log
commit cd96efe1c7046005463f69c1dc70b76a1bae11ec
Author: qq1367212627 <1367212627@qq.com>
Date:   Tue Nov 15 21:20:36 2016 +0800

test pull

commit 3913ff943b35f42a645c77deb95c0c0006dc3a91
Author: qq1367212627 <1367212627@qq.com>
Date:   Tue Nov 15 21:13:53 2016 +0800

commit 3

commit 67b2b6b412248367731c193aa0f6fed683b194f9
Author: qq1367212627 <1367212627@qq.com>
Date:   Tue Nov 15 20:24:46 2016 +0800

测试
test
lewis

commit d6024ab8d0fe649d2e17db5b267b817ce1a78471
Author: qq1367212627 <1367212627@qq.com>
Date:   Tue Nov 15 20:15:31 2016 +0800

Initial commit


git add -A :同步工作区与暂存区

git reset --hard HEAD~1 删除上一次的commit


添加远程仓库

要添加一个新的远程仓库,可以指定一个简单的名字,以便将来引用,运行 
git
remote add [shortname] [url]

$ git remote
origin
$ git remote add pb git://github.com/paulboone/ticgit.git
$ git remote -v
origin  git://github.com/schacon/ticgit.git
pb  git://github.com/paulboone/ticgit.git

lewis@DESKTOP-IK0DNHT MINGW64 ~/AndroidStudioProjects/HappyBirthDay (master)
$ git remote add pb https://github.com/qq1367212627/HappyBirthday.git 
lewis@DESKTOP-IK0DNHT MINGW64 ~/AndroidStudioProjects/HappyBirthDay (master)
$ git remote
pb

lewis@DESKTOP-IK0DNHT MINGW64 ~/AndroidStudioProjects/HappyBirthDay (master)
$ git remote -v
pb      https://github.com/qq1367212627/HappyBirthday.git (fetch)
pb      https://github.com/qq1367212627/HappyBirthday.git (push)




                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: