git入门

参考博文
git权威指南地址
Windows安装git
git官网
git官方命令

  • git 安装

http://msysgit.github.io/

  1. 复制
    git clone https://github.com/GitHubzzj/design-patterns.git
  2. 进入工程文件夹cd design-patterns
git add .        (注:别忘记后面的.,此操作是把Test文件夹下面的文件都添加进来)

git commit  -m  "提交信息"  (注:“提交信息”里面换成你需要,如“first commit”)

git push -u origin master   (注:此操作目的是把本地仓库push到github上面,此步骤需要你输入帐号和密码)
git -->gitbash -->cygwin git service :gitolite

git init 以创建新的 git 仓库
//从远端服务器上检出仓库 
git clone username@host:/path/to/repository 
  • 备份.ssh 公钥和密钥;
  • 备份 .gitconfig ,设置别名
chivalrousdig@gmail.com
123456
  • 设置用户
git config --global user.name "zengzhijun"
git config --global user.email "2537646458@qq.com"
  • 创建版本库
//将当前目录转化为版本库
$ git init
Initialized empty Git repository in D:/git/test/.git/

  • 把文件添加到版本库
root@DESKTOP-925MVJK MINGW64 /d/git/test (master)
// 添加一个文件到仓库,可以添加很多个,再一次性提交commit
$ git add ReadMe.txt

root@DESKTOP-925MVJK MINGW64 /d/git/test (master)
//把文件提交到仓库
$ git commit -m "commit a readme file"
[master (root-commit) c3addb3] commit a readme file
 1 file changed, 2 insertions(+)
 create mode 100644 ReadMe.txt

  • 修改文件后查看状态,显示哪些文件将要被提交
$ git status
On branch 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:   ReadMe.txt

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

  • 查看一个文件的差异
$ git diff
diff --git a/ReadMe.txt b/ReadMe.txt
index d8036c1..013b5bc 100644
--- a/ReadMe.txt
+++ b/ReadMe.txt
@@ -1,2 +1,2 @@
-Git is a version control system.
+Git is a distributed version control system.
 Git is free software.
\ No newline at end of file


  • 修改文件再次提交
root@DESKTOP-925MVJK MINGW64 /d/git/test (master)
$ git status
On branch 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:   ReadMe.txt

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

root@DESKTOP-925MVJK MINGW64 /d/git/test (master)
$ git add ReadMe.txt

root@DESKTOP-925MVJK MINGW64 /d/git/test (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   ReadMe.txt


root@DESKTOP-925MVJK MINGW64 /d/git/test (master)
$ git commit -m "distributed add"
[master 25be81a] distributed add
 1 file changed, 1 insertion(+), 1 deletion(-)

root@DESKTOP-925MVJK MINGW64 /d/git/test (master)
$ git status
On branch master
nothing to commit, working tree clean

第一次 git status 可以查看到哪些文件修改过,并且没有add 到仓库,提示要add,注意:一定要先add才能commit,commit后查看状态,显示没有修改了


  • 查看日志
$ git log
commit 07df332f3812ec11b63d4e104e083dd258fa5d4b (HEAD -> master)
Author: zengzhijun <2537646458@qq.com>
Date:   Sat Aug 18 16:08:35 2018 +0800

    distributed add 2

commit 25be81a9cb4c4b0329a95527101ec7b12ac09e3b
Author: zengzhijun <2537646458@qq.com>
Date:   Sat Aug 18 16:03:51 2018 +0800

    distributed add

commit c3addb387047a846b5f16112a0efde6a5597b1d5
Author: zengzhijun <2537646458@qq.com>
Date:   Sat Aug 18 15:50:04 2018 +0800

    commit a readme file

root@DESKTOP-925MVJK MINGW64 /d/git/test (master)
$ git log --pretty=oneline
07df332f3812ec11b63d4e104e083dd258fa5d4b (HEAD -> master) distributed add 2
25be81a9cb4c4b0329a95527101ec7b12ac09e3b distributed add
c3addb387047a846b5f16112a0efde6a5597b1d5 commit a readme file
  • 版本回退

上⼀个版本就是HEAD^,上上⼀个版本就是HEAD^^,当然往上100 个版本写100个^⽐较容易数不过来,所以写成HEAD~100

$ git reset --hard HEAD^
HEAD is now at 25be81a distributed add
  • 重返未来,回退到一开始最新的版本
    这里是distributed add 2 这个版本
$ git reset --hard 07df332f3812ec11b63d4e104e083dd258fa5d4b
HEAD is now at 07df332 distributed add 2

07df332f3812ec11b63d4e104e083dd258fa5d4bdistributed add 2 这个版本号的commit ID

  • 查看命令历史git reflog
    可以得到commit ID ,从而回滚到指定版本
$ git reflog
07df332 (HEAD -> master) HEAD@{0}: reset: moving to 07df332f3812ec11b63d4e104e083dd258fa5d4b
25be81a HEAD@{1}: reset: moving to HEAD^
07df332 (HEAD -> master) HEAD@{2}: commit: distributed add 2
25be81a HEAD@{3}: commit: distributed add
c3addb3 HEAD@{4}: commit (initial): commit a readme file


  • 工作区和暂存区
  1. git add 把文件添加进去,实际就是把文件修改添加到 暂存区
  2. git commit 把暂存区的所有内容添加到当前分支
实践操作
$ git status
On branch 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:   ReadMe.txt

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

        LICENSE.txt

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

root@DESKTOP-925MVJK MINGW64 /d/git/test (master)
$ git add ReadMe.txt

root@DESKTOP-925MVJK MINGW64 /d/git/test (master)
$ git add LICENSE.txt

root@DESKTOP-925MVJK MINGW64 /d/git/test (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   LICENSE.txt
        modified:   ReadMe.txt


root@DESKTOP-925MVJK MINGW64 /d/git/test (master)
$ git commit -m "understand how stage work"
[master 28347ad] understand how stage work
 2 files changed, 3 insertions(+), 1 deletion(-)
 create mode 100644 LICENSE.txt

root@DESKTOP-925MVJK MINGW64 /d/git/test (master)
$ git status
On branch master
nothing to commit, working tree clean

  • 管理修改
    git跟踪并管理的是修改而不是文件
    第⼀次修改 -> git add -> 第⼆次修改 -> git commit
  • 查看工作区和版本库里的最新版本的区别
$ git diff HEAD -- ReadMe.txt
diff --git a/ReadMe.txt b/ReadMe.txt
index db28b2c..9a8b341 100644
--- a/ReadMe.txt
+++ b/ReadMe.txt
@@ -1,4 +1,4 @@
 Git is a distributed version control system.
 Git is free software distributed under the GPL.
 Git has a mutable index called stage.
-Git tracks changes.
\ No newline at end of file
+Git tracks changes of files.
\ No newline at end of file

每次修改,如果不add到暂存区,那就不会加⼊到commit中


  • 撤销修改

场景1:当你改乱了⼯作区某个⽂件的内容,想直接丢弃⼯作区的修改时,⽤命令git
checkout -- file。
git checkout -- ReadMe.txt
场景2:当你不但改乱了⼯作区某个⽂件的内容,还添加到了暂存区时,想丢弃修改,分两
步,第⼀步⽤命令git reset HEAD file,就回到了场景1,第⼆步按场景1操作。

git reset HEAD ReadMe.txt
git checkout -- ReadMe.txt

场景3:已经提交了不合适的修改到版本库时,想要撤销本次提交,参考版本回退⼀节,不
过前提是没有推送到远程库
git reset --hard HEAD^


  • 删除已经提交的文件
  1. 本地文件管理器直接删除文件,git 会立马知道,用git status查看
  2. 如果确实要删除那就用如下步骤
git rm test.txt
git commit -m "delete file "
  1. 如果删错了想恢复,就用如下命令
    git checkout -- test.txt

  • 远程仓库
  1. 创建SSH KEY
    在用户主目录的.ssh目录下 创建 id_rsa和id_rsa.pub 文件
    执行命令
    ssh-keygen -t rsa -C "2537646458@qq.com"
  2. 登陆GitHub,打开“Account settings”,“SSH Keys”⻚⾯,然后,点“Add SSH Key”,填上任意Title,在Key⽂本框⾥粘贴id_rsa.pub⽂件的内容


    image.png
  • 添加远程库
  1. 在GitHub上创建一个项目


    image.png
  2. 根据提示把本地的库关联到GitHub上
    git remote add origin https://github.com/GitHubzzj/gittest.git
    第一次用git push -u origin master,后面就可以直接用git push origin master 不用-u选项了

    image.png

    push的时候会提示输入用户名密码

root@DESKTOP-925MVJK MINGW64 /d/git/test (master)
$ git remote add origin https://github.com/GitHubzzj/gittest.git

root@DESKTOP-925MVJK MINGW64 /d/git/test (master)
$ git push -u origin master
Enumerating objects: 20, done.
Counting objects: 100% (20/20), done.
Delta compression using up to 4 threads.
Compressing objects: 100% (15/15), done.
Writing objects: 100% (20/20), 1.61 KiB | 549.00 KiB/s, done.
Total 20 (delta 5), reused 0 (delta 0)
remote: Resolving deltas: 100% (5/5), done.
To https://github.com/GitHubzzj/gittest.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

  • 从远处克隆

root@DESKTOP-925MVJK MINGW64 /d/git
$ git clone https://github.com/GitHubzzj/gitskills.git
Cloning into 'gitskills'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
  • 分支管理

Git的分⽀是与众不同的,⽆论创建、切换和删除分⽀,Git在1秒钟之内就能完成!⽆论
你的版本库是1个⽂件还是1万个⽂件。

  • 创建与合并分⽀
  1. 我们创建dev分⽀,然后切换到dev分⽀:
$ git checkout -b dev
Switched to a new branch 'dev'
git checkout命令加上-b参数表⽰创建并切换,相当于以下两条命令:
$ git branch dev
$ git checkout dev
Switched to branch 'dev'
  1. git branch命令查看当前分支
$ git branch
* dev
  master

git branch命令会列出所有分⽀,当前分⽀前⾯会标⼀个*号。

  1. 修改文件提交
$ git add README.md

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (dev)
$ git commit -m "modify readme.md"
[dev 9c4c899] modify readme.md
 1 file changed, 2 insertions(+), 1 deletion(-)

  1. 切换回master分⽀
$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

切换回master分⽀后,再查看⼀个readme.txt⽂件,刚才添加的内容不⻅了!因为那个提
交是在dev分⽀上,⽽master分⽀此刻的提交点并没有变

 git merge dev
Updating 7fdc402..9c4c899
Fast-forward
 README.md | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

注意到上⾯的Fast-forward信息,Git告诉我们,这次合并是“快进模式”,也就是直接把
master指向dev的当前提交,所以合并速度⾮常快。

  1. 删除dev分⽀
$ git branch -d dev
Deleted branch dev (was 9c4c899).

命令 说明
查看分支 git branch
创建分支 git branch name
切换分支 git checkout name
创建+切换分支 git checkout -b name
合并某分支到当前分支 git merge name
删除分支 git branch -d name
  • 解决冲突
//创建新的分支
$ git  checkout -b feature1
Switched to a new branch 'feature1'

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (feature1)
$ git add README.md

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (feature1)
//提交分支
$ git commit -m "and simple"
[feature1 145b667] and simple
 1 file changed, 1 insertion(+), 1 deletion(-)

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (feature1)
//切换到master分支
$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (master)
$ git add README.md

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (master)
//在 master分支上提交
$ git commit -m "& simple"
[master 5e3b26f] & simple
 1 file changed, 1 insertion(+), 1 deletion(-)

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (master)
//此时有冲突,合并分支会报错,需要解决冲突后才可以
$ git merge feature1
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (master|MERGING)
//查看状态也能看出哪些是有冲突的文件
$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)

        both modified:   README.md

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

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (master|MERGING)
$ git branch
  feature1
* master

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (master|MERGING)
$ git add README.md

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (master|MERGING)
//处理完冲突后提交
$ git commit -m "conflict fixed"
[master bba27ff] conflict fixed

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (master)
//下面命令可以看到分支合并图
$ git log --graph --pretty=oneline --abbrev-commit
*   bba27ff (HEAD -> master) conflict fixed
|\
| * 145b667 (feature1) and simple
* | 5e3b26f & simple
|/
* 9c4c899 modify readme.md
* 7fdc402 (origin/master, origin/HEAD) Initial commit

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (master)
$ git branch
  feature1
* master

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (master)
//删除分支
$ git branch -d feature1
Deleted branch feature1 (was 145b667).

  • 分支管理策略

Fast forward”模式
--no-ff⽅式

//创建并切换到Dev分支
$ git checkout -b dev
Switched to a new branch 'dev'

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (dev)
$ git branch
* dev
  master

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (dev)
$ git add README.md

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (dev)
//提交
$ git commit -m "add merge"
[dev c918345] add merge
 1 file changed, 1 insertion(+)

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (dev)
//切换到master分支
$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 4 commits.
  (use "git push" to publish your local commits)

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (master)
//用  --no-ff 策略合并分支
$ git merge --no-ff -m "merge with no-ff" dev
Merge made by the 'recursive' strategy.
 README.md | 1 +
 1 file changed, 1 insertion(+)

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (master)
//查看分支合并树
$ git log --graph --pretty=oneline --abbrev-commit
*   f4f8345 (HEAD -> master) merge with no-ff
|\
| * c918345 (dev) add merge
|/
*   bba27ff conflict fixed
|\
| * 145b667 and simple
* | 5e3b26f & simple
|/
* 9c4c899 modify readme.md
* 7fdc402 (origin/master, origin/HEAD) Initial commit

image.png
  • 分⽀策略

合并分⽀时,加上--no-ff参数就可以⽤普通模式合并,合并后的历史有分⽀,能看出来曾经
做过合并,⽽fast forward合并就看不出来曾经做过合并。

  • Bug分⽀

git stash
修复bug时,我们会通过创建新的bug分⽀进⾏修复,然后合并,最后删除;
当⼿头⼯作没有完成时,先把⼯作现场git stash⼀下,然后去修复bug,修复后,再git
stash pop,回到⼯作现场

$ git status
On branch dev
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:   README.md

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

        a.txt

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

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (dev)
$ git add a.txt

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (dev)
$ git commit -m "add a.txt"
[dev 2656fc1] add a.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 a.txt

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (dev)
$ git status
On branch dev
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:   README.md

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

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (dev)
$ git add README.md

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (dev)
$ git status
On branch dev
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   README.md


root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (dev)
$ git add b.txt

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (dev)
$ git status
On branch dev
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   README.md
        new file:   b.txt


root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (dev)
$ git stash
Saved working directory and index state WIP on dev: 2656fc1 add a.txt

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (dev)
$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 8 commits.
  (use "git push" to publish your local commits)

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (master)
$ git checkout -b issue-101
fatal: A branch named 'issue-101' already exists.

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (master)
$ git branch
  dev
  issue-101
* master

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (master)
$ git branch -d issue-101
Deleted branch issue-101 (was 51d3b9e).

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (master)
$ git checkout -b issue-101
Switched to a new branch 'issue-101'

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (issue-101)
$ git add README.md

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (issue-101)
$ git commit -m "fix bug 101"
[issue-101 dfff753] fix bug 101
 1 file changed, 2 insertions(+), 1 deletion(-)

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (issue-101)
$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 8 commits.
  (use "git push" to publish your local commits)

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (master)
$ git merge --no-ff -m "merged bug fix 102" issue-101
Merge made by the 'recursive' strategy.
 README.md | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (master)
$ git branch -d issue-101
Deleted branch issue-101 (was dfff753).

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (master)
$ git checkout dev
Switched to branch 'dev'

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (dev)
$ git status
On branch dev
nothing to commit, working tree clean

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (dev)
$ git stash list
stash@{0}: WIP on dev: 2656fc1 add a.txt

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (dev)
$ git stash pop
On branch dev
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   b.txt

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:   README.md

Dropped refs/stash@{0} (1a66946c817cc0ea3af0ff79f9114e4bb0603c04)

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (dev)
$ git stash list

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (dev)
$ git stash apply stash@{0}
stash@{0} is not a valid reference

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (dev)
$ git status
On branch dev
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   b.txt

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:   README.md


root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (dev)
$ git checkout master
error: Your local changes to the following files would be overwritten by checkout:
        README.md
Please commit your changes or stash them before you switch branches.
Aborting

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (dev)
$ git commit -m "commit dev "
[dev 67033b4] commit dev
 1 file changed, 1 insertion(+)
 create mode 100644 b.txt

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (dev)
$ git checkout master
error: Your local changes to the following files would be overwritten by checkout:
        README.md
Please commit your changes or stash them before you switch branches.
Aborting

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (dev)
$ git status
On branch dev
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:   README.md

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

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (dev)
$ git add README.md

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (dev)
$ git commit -m "commit dev2"
[dev 61deaec] commit dev2
 1 file changed, 1 insertion(+)

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (dev)
$ git status
On branch dev
nothing to commit, working tree clean

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (dev)
$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 10 commits.
  (use "git push" to publish your local commits)

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (master)
$ git merge dev
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (master|MERGING)
$ git status
On branch master
Your branch is ahead of 'origin/master' by 10 commits.
  (use "git push" to publish your local commits)

You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Changes to be committed:

        new file:   a.txt
        new file:   b.txt

Unmerged paths:
  (use "git add <file>..." to mark resolution)

        both modified:   README.md


root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (master|MERGING)
$ git add README.md

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (master|MERGING)
$ git status
On branch master
Your branch is ahead of 'origin/master' by 10 commits.
  (use "git push" to publish your local commits)

All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

Changes to be committed:

        modified:   README.md
        new file:   a.txt
        new file:   b.txt


root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (master|MERGING)
$ git commit -m "fixed bug and dev"
[master 3dc5d28] fixed bug and dev

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (master)
$ git status
On branch master
Your branch is ahead of 'origin/master' by 14 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

1.一开始是在Dev环境开发,进度到一半,此时突然来了个bug,我们要先将开发的add了.但是不能提交,因为此时还没开发完,不能提交.

  1. 利用git stash功能,先将工作现场储藏起来
  2. 切换到需要修改bug的分支,比如master,
    git checkout master,再git checkout -b issue-101 创建并切换到issue-101 分支,修改bug,提交.
  3. 切换到master分支,合并bug分支git merge --no-ff -m "merged bug fix 102" issue-101,并删除bug分支git branch -d issue-101
  4. 继续切换到Dev的开发分支开发,git checkout dev;用git stash list 查看刚刚存储现场.
  5. 恢复现场,⼀是⽤git stash apply恢复,但是恢复后,stash内容并不删除,你需要⽤git stash drop来删
    除;另⼀种⽅式是⽤git stash pop,恢复的同时把stash内容也删了.
  6. 等 Dev环境也开发并提交完,就要切换到master分支,将Dev分支合并进去git merge dev,如果有冲突就要解决冲突并提交.
    注意Dev环境开发完要提交才能切换到master分支,不然会报错:
error: Your local changes to the following files would be overwritten by checkout:
        README.md
Please commit your changes or stash them before you switch branches.
Aborting

  • Feature分⽀
//接到新任务,新建一个vulcan分支开发之
$ git checkout -b feature-vulcan
Switched to a new branch 'feature-vulcan'

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (feature-vulcan)
//开发完毕,新建了一个文件
$ git status
On branch feature-vulcan
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        feature-vulcan.txt

nothing added to commit but untracked files present (use "git add" to track)

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (feature-vulcan)
$ git add feature-vulcan.txt

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (feature-vulcan)
//提交该修改,准备合并
$ git commit -m "add vulcan"
[feature-vulcan 64977f6] add vulcan
 1 file changed, 1 insertion(+)
 create mode 100644 feature-vulcan.txt

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (feature-vulcan)
//切换到 Dev环境,准备合并
$ git checkout dev
Switched to branch 'dev'

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (dev)
//接到通知,vulcan不开发了,要放弃.本想直接删掉,结果报错.
$ git branch -d feature-vulcan
error: The branch 'feature-vulcan' is not fully merged.
If you are sure you want to delete it, run 'git branch -D feature-vulcan'.

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (dev)
//强行删除
$ git branch -D feature-vulcan
Deleted branch feature-vulcan (was 64977f6).

  • 多人协作
  • 查看远程库信息
origin

root@DESKTOP-925MVJK MINGW64 /d/git/gitskills (dev)
//查看远程库详细信息
$ git remote -v
origin  https://github.com/GitHubzzj/gitskills.git (fetch)
origin  https://github.com/GitHubzzj/gitskills.git (push)

  • 推送分支
//推送master分支
git push origin master
//推送Dev分支
git push origin dev
  • 抓取分支

• 查看远程库信息,使⽤git remote -v;
• 本地新建的分⽀如果不推送到远程,对其他⼈就是不可⻅的;
• 从本地推送分⽀,使⽤git push origin branch-name,如果推送失败,先⽤git pull抓
取远程的新提交;
• 在本地创建和远程分⽀对应的分⽀,使⽤git checkout -b branch-name origin/branchname,本地和远程分⽀的名称最好⼀致;
• 建⽴本地分⽀和远程分⽀的关联,使⽤git branch --set-upstream branch-name
origin/branch-name;
• 从远程抓取分⽀,使⽤git pull,如果有冲突,要先处理冲突。

  • git branch --set-upstream dev origin/dev 设置dev和origin/dev的链接

如果git pull提⽰“no tracking information”,则说明本地分⽀和远程分⽀的链接关系没
有创建,⽤命令git branch --set-upstream branch-name origin/branch-name。

在本地创建和远程分⽀对应的分⽀,使⽤git checkout -b branch-name origin/branchname,本地和远程分⽀的名称最好⼀致;


  • 标签管理

发布⼀个版本时,我们通常先在版本库中打⼀个标签,这样,就唯⼀确定了打标签时刻的版
本。将来⽆论什么时候,取某个标签的版本,就是把那个打标签的时刻的历史版本取出来。
所以,标签也是版本库的⼀个快照。

分⽀可以移动,标签不能移动

  • 创建标签
//查看分支
$ git branch
* dev
  master

root@DESKTOP-925MVJK MINGW64 /d/git/git-skills-dev/gitskills (dev)
//切换到主分支
$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

root@DESKTOP-925MVJK MINGW64 /d/git/git-skills-dev/gitskills (master)
//查看是否切换成功
$ git branch
  dev
* master

root@DESKTOP-925MVJK MINGW64 /d/git/git-skills-dev/gitskills (master)
//给当前的版本打上v1.0的标签
$ git tag v1.0

root@DESKTOP-925MVJK MINGW64 /d/git/git-skills-dev/gitskills (master)
//查看所有的标签,按照字母顺序排列
$ git tag
v1.0

root@DESKTOP-925MVJK MINGW64 /d/git/git-skills-dev/gitskills (master)
//查看以前的提交
$ git log --pretty=oneline --abbrev-commit
3dc5d28 (HEAD -> master, tag: v1.0, origin/master, origin/HEAD) fixed bug and dev
61deaec commit dev2
67033b4 commit dev
0dddf15 merged bug fix 102
dfff753 fix bug 101
2656fc1 add a.txt
2a11bf3 merged bug fix 101
51d3b9e 'issue-101'
f4f8345 merge with no-ff
c918345 add merge
bba27ff conflict fixed
5e3b26f & simple
145b667 and simple
9c4c899 modify readme.md
7fdc402 Initial commit

root@DESKTOP-925MVJK MINGW64 /d/git/git-skills-dev/gitskills (master)
//给以前某一个提交打上标签
$ git tag v0.9 0dddf15

root@DESKTOP-925MVJK MINGW64 /d/git/git-skills-dev/gitskills (master)
//查看刚刚打的标签
$ git tag
v0.9
v1.0

root@DESKTOP-925MVJK MINGW64 /d/git/git-skills-dev/gitskills (master)
//已经打上的标签不能再打
$ git tag v0.9
fatal: tag 'v0.9' already exists

root@DESKTOP-925MVJK MINGW64 /d/git/git-skills-dev/gitskills (master)
//查看某一个标签的详细信息
$ git show v0.9
commit 0dddf15a82cf6bc9c0fc7a92cc11e36b752f21b5 (tag: v0.9)
Merge: 2a11bf3 dfff753
Author: zengzhijun <2537646458@qq.com>
Date:   Sat Aug 18 23:17:32 2018 +0800

    merged bug fix 102


root@DESKTOP-925MVJK MINGW64 /d/git/git-skills-dev/gitskills (master)
//创建带有说明的标签,⽤-a指定标签名,-m指定说明⽂字
$ git tag -a v0.1 -m "version 0.1 released" 7fdc402

root@DESKTOP-925MVJK MINGW64 /d/git/git-skills-dev/gitskills (master)
//查看刚刚打的带有说明文字的标签
$ git show v0.1
tag v0.1
Tagger: zengzhijun <2537646458@qq.com>
Date:   Sun Aug 19 19:49:24 2018 +0800

version 0.1 released

commit 7fdc4029e1fbfcfbb1731e994dd2ae5d676e7360 (tag: v0.1)
Author: GitHubzzj <2537646458@qq.com>
Date:   Sat Aug 18 18:02:14 2018 +0800

    Initial commit

diff --git a/README.md b/README.md
new file mode 100644
index 0000000..e87becc
--- /dev/null
+++ b/README.md
@@ -0,0 +1 @@
+# gitskills
\ No newline at end of file

root@DESKTOP-925MVJK MINGW64 /d/git/git-skills-dev/gitskills (master)
//通过-s⽤私钥签名⼀个标签,签名采⽤PGP签名,因此,必须⾸先安装gpg(GnuPG),如果没有找到gpg,或者没有gpg密钥对,就会报错:
//⽤PGP签名的标签是不可伪造的
$ git tag -s v0.2 -m "signed version 0.2 released" 145b667
gpg: directory `/c/Users/root/.gnupg' created
gpg: new configuration file `/c/Users/root/.gnupg/gpg.conf' created
gpg: WARNING: options in `/c/Users/root/.gnupg/gpg.conf' are not yet active during this run
gpg: keyring `/c/Users/root/.gnupg/secring.gpg' created
gpg: keyring `/c/Users/root/.gnupg/pubring.gpg' created
gpg: skipped "zengzhijun <2537646458@qq.com>": secret key not available
gpg: signing failed: secret key not available
error: gpg failed to sign the data
error: unable to sign the tag

root@DESKTOP-925MVJK MINGW64 /d/git/git-skills-dev/gitskills (master)
$ git show v0.2
fatal: ambiguous argument 'v0.2': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

  • 操作标签
//删除标签
$ git tag -d v0.1
Deleted tag 'v0.1' (was e239afd)

root@DESKTOP-925MVJK MINGW64 /d/git/git-skills-dev/gitskills (master)
//推送某个标签到远程
$ git push origin v1.0
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/GitHubzzj/gitskills.git
 * [new tag]         v1.0 -> v1.0

root@DESKTOP-925MVJK MINGW64 /d/git/git-skills-dev/gitskills (master)
//⼀次性推送全部尚未推送到远程的本地标签
$ git push origin --tags
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/GitHubzzj/gitskills.git
 * [new tag]         v0.9 -> v0.9

root@DESKTOP-925MVJK MINGW64 /d/git/git-skills-dev/gitskills (master)
$ git tag
v0.9
v1.0
root@DESKTOP-925MVJK MINGW64 /d/git/git-skills-dev/gitskills (master)
//删除已经推送到远程的标签,第一步,删除本地的标签
$ git tag -d v0.9
Deleted tag 'v0.9' (was 0dddf15)

root@DESKTOP-925MVJK MINGW64 /d/git/git-skills-dev/gitskills (master)
//删除已经推送到远程的标签,第二步,删除远程的标签
//可以在GitHub的release下面看到已经打了几个标签
$ git push origin :refs/tags/v0.9
To https://github.com/GitHubzzj/gitskills.git
 - [deleted]         v0.9
  • 自定义Git
命令 说明
git config --global color.ui true 让Git适当地显⽰不同的颜⾊

忽略⽂件的原则是:

  1. 忽略操作系统⾃动⽣成的⽂件,⽐如缩略图等;
  2. 忽略编译⽣成的中间⽂件、可执⾏⽂件等,也就是如果⼀个⽂件是通过另⼀个⽂件⾃动⽣成的,那⾃动生成的⽂件就没必要放进版本库,⽐如Java编译产⽣的.class⽂件;
  3. 忽略你⾃⼰的带有敏感信息的配置⽂件,⽐如存放⼝令的配置⽂件。
  • 配置别名
命令 说明
git config --global alias.st status git st -->git status
git config --global alias.co checkout git co -->git checkout
git config --global alias.ci commit git ci -->git commit
git config --global alias.br branch git br --> git branch
git config --global alias.unstage 'reset HEAD' git unstage --> git reset HEAD
git config --global alias.last 'log -1' git last --> git log -l
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit" 牛逼的log
  • 搭建Git服务器

第⼀步,安装git:
$ sudo apt-get install git
第⼆步,创建⼀个git⽤户,⽤来运⾏git服务:
$ sudo adduser git
第三步,创建证书登录:
收集所有需要登录的⽤户的公钥,就是他们⾃⼰的id_rsa.pub⽂件,把所有公钥导⼊到/
home/git/.ssh/authorized_keys⽂件⾥,⼀行⼀个。
第四步,初始化Git仓库:
先选定⼀个目录作为Git仓库,假定是/srv/sample.git,在/srv目录下输⼊命令:
$ sudo git init --bare sample.git
Git就会创建⼀个裸仓库,裸仓库没有⼯作区,因为服务器上的Git仓库纯粹是为了共享,所
以不让⽤户直接登录到服务器上去改⼯作区,并且服务器上的Git仓库通常都以.git结尾。然
后,把owner改为git:
$ sudo chown -R git:git sample.git
第五步,禁⽤shell登录:
出于安全考虑,第⼆步创建的git⽤户不允许登录shell,这可以通过编辑/etc/passwd⽂件
完成。找到类似下⾯的⼀⾏:
git:x:1001:1001:,,,:/home/git:/bin/bash
改为:
git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell
这样,git⽤户可以正常通过ssh使⽤git,但⽆法登录shell,因为我们为git⽤户指定的gitshell每次⼀登录就⾃动退出。
第六步,克隆远程仓库:
现在,可以通过git clone命令克隆远程仓库了,在各⾃的电脑上运⾏:
$ git clone git@server:/srv/sample.git
Cloning into 'sample'...
warning: You appear to have cloned an empty repository.


要⽅便管理公钥,⽤Gitosis;
要像SVN那样变态地控制权限,⽤Gitolite。

推荐阅读更多精彩内容

  • Git 基础 基本原理 客户端并不是只提取最新版本的文件快照,而是把代码仓库完整的镜像下来。这样一来,任何一处协同...
    __silhouette阅读 11,259评论 5 137
  • Git 命令行学习笔记 Git 基础 基本原理 客户端并不是只提取最新版本的文件快照,而是把代码仓库完整的镜像下来...
    sunnyghx阅读 2,404评论 0 11
  • Git常用语法 [TOC] Git简介 描述 ​ Git(读音为/gɪt/。)是一个开源的分布式版本控制系统,...
    君惜丶阅读 2,023评论 0 13
  • 配钥匙的人 以野 西校区门前的马路看样子已经修了很久,为修路...
    以野之名阅读 514评论 10 3
  • 上过的人都知道,大学宿舍里总有那么一两个惹人心烦的。每个人的关系都变得很微妙,友情?利益?我且列了几条,您且看且过...
    天蝎少女深夜放毒阅读 425评论 5 4