文章作者:Tyan
博客:noahsnail.com | CSDN | 简书
# 命令形式:git init
$ cd TestGit
$ git init
Initialized empty Git repository in /Users/***/TeskGit/.git/
# 命令形式:git init [Directory Name]
$ git init TestGit
Initialized empty Git repository in /Users/***/TeskGit/.git/
# 命令形式:git clone [url] -b [branch name]
$ git clone ssh://git@github.com/**/Refacor -b master
Cloning into ‘Refacor‘...
Saving password to keychain failed
Identity added: /Users/**/.ssh/id_rsa_github ((null))
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
Checking connectivity... done.
# 命令形式:git remote add [remote repository aliase] [url]
$ git remote add origin git@github.com:***/Refacor.git
# 命令形式:git remote [-v]
$ git remote
origin
$ git remote -v
origin git@github.com:***/Refacor.git (fetch)
origin git@github.com:***/Refacor.git (push)
# 命令形式:git remote rm [remote repository aliase]
$ git remote rm origin
$ git remote
# 命令形式:git fetch
$ git fetch
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:***/Refacor
* [new branch] develop -> origin/develop
* [new branch] master -> origin/master
# 命令形式:git remote show [remote repository name]
$ git remote show origin
* remote origin
Fetch URL: git@github.com:***/Refacor.git
Push URL: git@github.com:***/Refacor.git
HEAD branch: master
Remote branches:
develop tracked
master tracked
Local branches configured for ‘git pull‘:
develop merges with remote develop
master merges with remote master
Local refs configured for ‘git push‘:
develop pushes to develop (up to date)
master pushes to master (local out of date)
# 命令形式:git checkout [-b or -B] [local branch name]
$ git checkout -b a
Switched to a new branch ‘a‘
$ git checkout -b a
fatal: A branch named ‘a‘ already exists.
$ git checkout -B a
Switched to and reset branch ‘a‘
# 命令形式:git checkout [local branch name]
$ git checkout develop
Switched to branch ‘develop‘
# 命令形式:git branch [local branch name]
$ git branch
* a
develop
master
$ git branch b
# 命令形式:git branch [-r] [-a]
$ git branch -r
origin/develop
origin/master
$ git branch -a
develop
* master
remotes/origin/develop
remotes/origin/master
# 命令形式:git branch -d [local branch name]
$ git branch -d test
Deleted branch test (was 0b05e43).
# 命令形式:git checkout -b [local branch name] origin/[remote branch name]
$ git checkout -b develop origin/develop
Branch develop set up to track remote branch develop from origin.
Switched to a new branch ‘develop‘
$ git checkout -b master origin/master
Branch master set up to track remote branch master from origin.
Switched to a new branch ‘master‘
# 命令形式:git branch --set-upstream-to [remote repository name/remote branch]
$ git branch --set-upstream-to origin/develop
Branch develop set up to track remote branch develop from origin.
# 命令形式:git push origin --delete [remote branch name] or git branch -dr [remote/branch]
$ git push origin --delete develop
To git@github.com:***/Refacor.git
- [deleted] develop
$ git branch -dr origin/develop
Deleted remote-tracking branch origin/develop (was d6813fd).
注:本地可以有多个分支,远程也可以有多个分支,本地多个分支可以关联远程多个分支,但是,本地分支最好与远程分支同名,以免出现问题。
向远程仓库提交内容之前,要理解三个概念:本地文件,缓冲区,本地仓库。平常修改的文件内容都是本地文件,在往远程仓库提交之前先要提交到缓冲区,再从缓冲区提交到本地仓库,然后本地仓库才能往远程仓库提交。本地的内容分为三大部分,它们是相互独立的,理解了这四个概念就明白为什么要执行git add,git commit,git push命令了。
# 命令形式:git fetch,git merge,git pull
$ git branch
* develop
master
$ git status
On branch develop
Your branch is up-to-date with ‘origin/develop‘.
nothing to commit, working directory clean
$ git fetch
# 命令形式:git merge
# 正常情况下
$ git merege
Already up-to-date.
# 没有提交修改到本地仓库的情况下
$ git merge
Updating d6813fd..2444abc
error: Your local changes to the following files would be overwritten by merge:
README.md
Please, commit your changes or stash them before you can merge.
Aborting
# 本地仓库与取下的远程仓库内容有冲突的情况下
$ git merge
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
冲突内容:
# Refacor
<<<<<<< HEAD
branch develop
=======
branch
>>>>>>> refs/remotes/origin/develop
修改后的内容:
# Refacor
branch develop
处理过程:
$ vim README.md
$ git add .
$ git commit -m "feat: handle conflict"
[develop d4c01a6] feat: handle conflict
$ git push
Counting objects: 4, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 416 bytes | 0 bytes/s, done.
Total 4 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To git@github.com:***/Refacor.git
2444abc..d4c01a6 develop -> develop
# 命令形式:git pull
$ git pull
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:***/Refacor
d4c01a6..afa74f2 develop -> origin/develop
Updating d4c01a6..afa74f2
Fast-forward
README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
备注:git pull = git fetch + git merge,git pull功能很强大,能自动合并冲突,合并后需要手动解决冲突,最好不要直接使用git pull,因为许多细节都看不到。
# 命令形式:git add [filename1] [filename2] or [directoryname] or [.]
$ git add README.md
$ git add README.md a.txt
$ git add .
# 命令形式:git commit [file1] [file2] -m [comment]
$ git commit -m "feat: edit README.md"
[develop d6813fd] feat: edit README.md
1 file changed, 1 insertion(+), 1 deletion(-)
$ git commit README.md a.txt -m "feat: commit test"
# 命令形式:git push [remote repository name] [remote branch]
git push
fatal: The upstream branch of your current branch does not match
the name of your current branch. To push to the upstream branch
on the remote, use
git push origin HEAD:master
To push to the branch of the same name on the remote, use
git push origin develop
$ git push origin HEAD:master
Counting objects: 3, done.
Writing objects: 100% (3/3), 296 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:***/Refacor.git
f7ea1a0..d6813fd HEAD -> master
$ git branch --set-upstream-to origin/develop
Branch develop set up to track remote branch develop from origin.
$ git status
On branch develop
Your branch is ahead of ‘origin/develop‘ by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working directory clean
$ git fetch
$ git merge
Already up-to-date.
$ git push
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:***/Refacor.git
f7ea1a0..d6813fd develop -> develop
# 命令形式:git config --list
$ git config --list
# 命令形式:git config --get [variable]
$ git config --get user.name
test
$ git config --global --get user.email
global@test.com
# 命令形式:git config [--global] [user.name or user.email]
$ git config --global user.name "test global"
$ git config user.name "test"
$ git config --global user.email "global@test.com"
$ git config user.email "local@test.com"
#命令形式:git log
$ git log
commit d6813fdf94c37d51e952aee42155654fe333ef28
Author: *** <***@gmail.com>
Date: Fri Oct 22 17:05:12 2016 +0800
feat: edit README.md
commit f7ea1a04e423e135d92c4c93e4f188e7257b0cad
Author: *** <***@gmail.com>
Date: Fri Oct 22 16:39:45 2016 +0800
feat:develop
commit 68506c5f153fc303fa33ceb733bf15b6da690b00
Merge: 9b7d7c8 f777a9b
Author: *** <***@gmail.com>
Date: Fri Oct 22 16:25:40 2016 +0800
feat: test
commit 9b7d7c87c32e144ea1364c1a0aa0fe16f074c8d5
Author: *** <***@gmail.com>
Date: Fri Oct 22 16:18:18 2016 +0800
# 命令形式:git log --stat
$ git log --stat
commit afa74f2de6caede85b6b4beeefc3ec338ed42986
Author: *** <***@gmail.com>
Date: Fri Oct 22 17:35:32 2016 +0800
Update README.md
README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
commit 9b313361b73cd76505c05e6b6ad3b0c6204357ce
Author: *** <***@gmail.com>
Date: Fri Oct 22 17:25:28 2016 +0800
feat: conflict
README.md | 2 +-
# 命令形式:git reflog
$ git reflog
afa74f2 HEAD@{0}: checkout: moving from master to develop
d6813fd HEAD@{1}: merge refs/remotes/origin/master: Fast-forward
f777a9b HEAD@{2}: checkout: moving from develop to master
afa74f2 HEAD@{3}: pull: Fast-forward
d4c01a6 HEAD@{4}: commit (merge): feat: handle conflict
9b31336 HEAD@{5}: commit: feat: conflict
d6813fd HEAD@{6}: commit: feat: edit README.md
f7ea1a0 HEAD@{7}: commit: feat:develop
68506c5 HEAD@{8}: commit (merge): feat: test
9b7d7c8 HEAD@{9}: commit: feat:branch test
0b05e43 HEAD@{10}: checkout: moving from master to develop
f777a9b HEAD@{11}: pull: Fast-forward
0b05e43 HEAD@{12}: checkout: moving from develop to master
0b05e43 HEAD@{13}: checkout: moving from a to develop
0b05e43 HEAD@{14}: checkout: moving from develop to a
0b05e43 HEAD@{15}: checkout: moving from a to develop
0b05e43 HEAD@{16}: checkout: moving from origin/develop to a
0b05e43 HEAD@{17}: checkout: moving from 0b05e4388c3cb16d23a59f7238d59894c7f1fb8
# 命令形式:git diff
$ git diff
diff --git a/a.txt b/a.txt
index d00491f..cd6c7f8 100644
--- a/a.txt
+++ b/a.txt
@@ -1 +1 @@
-1
+1dfa
备注:还有很多没讲到,例如tag,但我感觉现有的东西已经足够用了,以后需要再去查资料吧。
原文:http://blog.csdn.net/quincuntial/article/details/52886253