参考: SourceTree使用

git教程

廖学风git

    文档1

   文档2

1. git 概念介绍

  • 工作区: 就是你在电脑里能看到的目录,比如我的learngit文件夹就是一个工作区,工作区下面有.git目录
  • 版本库:工作区有一个隐藏目录.git,这个不算工作区,而是Git的版本库。 Git的版本库里存了很多东西,其中最重要的就是称为stage(或者叫index)的暂存区, 还有Git为我们自动创建的第一个分支master,以及指向master的一个指针叫HEAD。

git 文件的4中状态:

  1. git库所在的文件夹中的文件大致有4种状态
  2.  
  3. Untracked: 未跟踪, 此文件在文件夹中, 但并没有加入到git库, 不参与版本控制. 通过git add 状态变为Staged.
  4.  
  5. Unmodify: 文件已经入库, 未修改, 即版本库中的文件快照内容与文件夹中完全一致. 这种类型的文件有两种去处, 如果它被修改, 而变为Modified. 如果使用git rm移出版本库, 则成为Untracked文件
  6.  
  7. Modified: 文件已修改, 仅仅是修改, 并没有进行其他的操作. 这个文件也有两个去处, 通过git add可进入暂存staged状态, 使用git checkout 则丢弃修改过, 返回到unmodify状态, 这个git checkout即从库中取出文件, 覆盖当前修改
  8.  
  9. Staged: 暂存状态. 执行git commit则将修改同步到库中, 这时库中的文件和本地文件又变为一致, 文件为Unmodify状态. 执行git reset HEAD filename取消暂存, 文件状态为Modified

2. git常用命令

命令大概图解

创建配置版本库

  1. . git init #用于本地版本库的创建
  2.  
  3. "git init --bare #创建一个裸仓库(用于服务器版本库创建),裸仓库没有工作区,服务器上的Git仓库纯粹
  4. 是为了共享,所以不让用户直接登录到服务器上去改工作区,并且服务器上的Git仓库通常都以.git结尾。然后,
  5. 把owner改为git。
  6.  
  7. . git config --global user.email "you@example.com" #配置提交作者
  8. git config --global user.name "Your Name"

添加文件到版本库

  1. . git add files 或者 git add . #添加文件到暂存区(stage)
  2.  
  3. git add -A 将工作区中的所有改动以及新增文件添加到暂存区。
  4.  
  5. git add -i #选择性添加到stage
  6.  
  7. . git commit -m "描述"

回退和查看

  1. . git status #查看加入到暂存区(stage)的文件和工作区文件的改动,可以回退,(git reset filename 回退,从暂存区撤销出来.)
    git status -s
  2.  
  3. . git log 或者 git log dev # 查看分支的提交
  4.  
  5. git log  --oneline #查看更加简短的提交id,这个id也是可以使用的,比如在reset 或者revert命令中。
  6.  
  7.   git log  --oneline --decorate #查看带有 tag的 简短的提交id
  8.  
  9. git log --stat -2 #查看最新的2次提交
  1. . git show 0a87317d6a8ccd3a5145c4d2668b63918a5de4e8 #查看某一个具体的提交
  2.  
  3. .git show-branch --more= #查看当前分支简洁单行摘要
  4.  
  5. 5. 一个文件修改了很多行,如何让它回到最开始没有更改的状态
    git chechkout -- filename

[root@chehce a1]# vim 2.txt
[root@chehce a1]# git status
# On branch master
# Your branch is ahead of 'origin/master' by 3 commits.
# (use "git push" to publish your local commits)
#
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: test
#
# 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: 2.txt
#

  1. [root@chehce git]# git rev-list HEAD #查看提交的所有cimmitid
  2. e7bb519a7b89c85e52c5e2fea076d19e072abe34
  3. a80132df92811ec377f1659014ccb0430f0e038e
  4. 9b526cb779001a371b252a180d9403da5d391ce8
  5. 990c9e00b023405c9f34ff3b4783b9147d8a6d26
  6. a56abde92aeebe9bcd33bac309891de57ca07a2e
  7. 9eb33d3c55803cd9e989859e91af547eefb9a4b3
  8. 50df03d3229824e4f4a4a4a3972e23565eff1b27
  9. 8b0d08cf0f65d81fc5510c895f7a34d5adc7fd6f
  10. [root@chehce git]# git log --oneline
  11. e7bb519 add .txt
  12. a80132d add .txt
  13. 9b526cb 修改ha.txt
  14. 990c9e0 mv haha.txt to ha.txt
  15. a56abde Merge commit '9eb33d3c55803cd9e989859e91af547eefb9a4b3'
  16. 9eb33d3 haha
  17. 50df03d -
  18. 8b0d08c touch .txt

删除,重命名

 
  1. 将版本库中的文件删除或者重命名
  2.  
  3. git rm filename --------> git commit -m "删除filename" #2步完成
  4.  
  5. 如: 在客户端执行 git rm t1.sh , git commit -m "删除t1.sh" 后,
  6.  
  7. git push到服务器.另外一个客户端再git pull后,本地的t1.sh也被删除了。
  8.  
  9. git mv data newdata:
  10.  
  11. git mv f1 f1.bak
  12.  
  13. git commit -m "mv f1 to f1.bak" #通过2步完成
  14.  
  15. 注意: 直接rm 删除或者 mv移动只是改变了个工作区,暂存区是没有改变的。要将暂存区删除得用上面的 git rm --cache 或者git reset

忽略文件

在work dir 或者它的子目录 新建 文件 .gitignore 文件,将要忽略的文件写入其中,那么在 git add 的时候就可以忽略掉这些文件。

可以使用匹配。例如  *.h 表示 所有.h 结尾的文件。

  1. [root@chehce git]# ls
  2. .txt .txt .txt .txt .txt .txt ha.txt
  3. [root@chehce git]# touch {..}.h #新建了几个文件
  4. [root@chehce git]# ls
  5. .h .txt .h .txt .h .txt .h .txt .h .txt .txt ha.txt
  6. [root@chehce git]# ls
  7. .h .txt .h .txt .h .txt .h .txt .h .txt .txt ha.txt
  8. [root@chehce git]# vim .gitignore #编辑忽略文件
  9. [root@chehce git]# git status #查看git 状态的时候,并没有提示那些文件需要 git add。 但是提示了 忽略文件自身.
  10. # On branch master
  11. # Untracked files:
  12. # (use "git add <file>..." to include in what will be committed)
  13. #
  14. # .gitignore
  15. nothing added to commit but untracked files present (use "git add" to track)

忽略掉自身

  1. [root@chehce git]# cat .gitignore
  2. *.h
  3. .gitignore #文件中加入自己
  4. [root@chehce git]# git status
  5. # On branch master
  6. nothing to commit, working directory clean

文件归档

git branch

git branch #查看本地分支

git branch -a #查看本地和远程的所有分支

git branch dev #创建分支

git checkout dev #切换分支

git branch -D dev 删除本地分支

git push origin :br 删除远程分支

#例子:在本地仓库创建分支,提交内容,然后push到远程仓库.另外一个客户端再pull远程仓库。

  1. 本地仓库的操作:
  2.  
  3. git branch dev
  4.  
  5. git checkout dev
  6.  
  7. touch test-dev.txt
  8.  
  9. git add test-dev.txt
  10.  
  11. git commint -m "add test-dev.txt"
  12.  
  13. #将本地dev分支push到远程仓库的dev分支(远程仓库不存在dev分支会创建)
  14. git push origin dev:dev #origin 代表远程仓库,dev(本地分支):dev(远程分支)
  15.  
  16. #再另外一个git客户端pull的时候,是在master客户端执行的。只能pull到master客户端。
  17. git pull #在客户端master分支上pull只能pull到远程仓库的master分支。
  18.  
  19. #将远程仓库分支到本地与本地dev分支合并。
  20. git pull origin dev:dev

git pull

  • 将远程存储库中的更改合并到当前分支中。在默认模式下,git pull是 git fetch后跟git merge FETCH_HEAD的缩写。

  • 更准确地说,git pull使用给定的参数运行git fetch,并调用git merge将检索到的分支头合并到当前分支中。 使用–rebase,它运行git rebase而不是git merge。

  • 示例

  1. $ git pull <远程主机名> <远程分支名>:<本地分支名>
  2.  
  3. 比如,要取回origin主机的next分支,与本地的master分支合并,需要写成下面这样 -
  4.  
  5. $ git pull origin next:master
  6.  
  7. 如果远程分支(next)要与当前分支合并,则冒号后面的部分可以省略。上面命令可以简写为:
  8.  
  9. $ git pull origin next

git push

  • git push命令用于将本地分支的更新,推送到远程主机。它的格式与git pull命令相似。

  1. $ git push <远程主机名> <本地分支名>:<远程分支名>
  2.  
  3. #将本地的master分支推送到origin主机的master分支。如果master不存在,则会被新建。
  4.  
  5. $ git push origin master
  6.  
  7. #如果省略本地分支名,则表示删除指定的远程分支,因为这等同于推送一个空的本地分支到远程分支。
  8.  
  9. $ git push origin :master
  10. # 等同于
  11. $ git push origin --delete master
  12.  
  13. #将当前分支推送到origin主机的对应分支。
  14.  
  15. $ git push origin
  16.  
  17. 将本地的master分支推送到origin主机,同时指定origin为默认主机,
  18. 后面就可以不加任何参数使用git push了。
  19. git push -u origin master
  • 不带任何参数的git push,默认只推送当前分支,这叫做 simple 方式。 此外,还有一种 matching 方式,会推送所有有对应的远程分支的本地分支。 Git 2.0 版本之前,默认采用 matching 方法,现在改为默认采用 simple 方式。 如果要修改这个设置,可以采用 git config 命令。

  • 还有一种情况,就是不管是否存在对应的远程分支,将本地的所有分支都推送到远程主机,这时需要使用–all选项。

  1. $ git push --all origin 将所有本地分支都推送到origin主机。

如果远程主机的版本比本地版本更新,推送时Git会报错,要求先在本地做 git pull 合并差异,然后再推送到远程主机。 这时,如果你一定要推送,可以使用“`–force“ 选项。

  1. $ git push --force origin

git merge

  • git merge命令用于将两个或两个以上的开发历史加入(合并)一起。

  1. 将分支dev合并到当前分支中,自动进行新的提交:
  2.  
  3. $ git merge dev

git fetch

  • git fetch命令用于从另一个存储库下载对象和引用。远程跟踪分支已更新(Git术语叫做commit), 需要将这些更新取回本地,这时就要用到git fetch命令。取回的更新现在看不到必须merge后才能查看到内容

  1. 要更新所有分支
  2. $ git fetch <远程主机名> = git fetch
  3.  
  4. 取回特定分支的更新,可以指定分支名
  5. $ git fetch <远程主机名> <分支名>
  6.  
  7. 取回远程主机的更新以后,可以在它的基础上,使用git checkout命令创建一个新的分支。
  8. $ git checkout -b newBrach origin/master
  9.  
  10. 也可以使用git merge命令或者git rebase命令,在本地分支上合并远程分支。在当前分支上,合并origin/master
  11. $ git merge origin/master
  12. # 或者
  13. $ git rebase origin/master

git 冲突

首先,用git diff查看冲突在哪里,然后手动修改。

也可用 git status查看, 可以告诉我们冲突的文件.

 
  1. $ git status 查看冲突的文件
  2.  
  3. 然后修改文件的冲突部分
  4.  
  5. 再次提交。
  6. $ git add readme.txt
  7. $ git commit -m "conflict fixed"

3.  git  branch

创建分支

  1. git branch branch-name [starting commint]

  2. git branch t1 23fe826
  3. #如果没有starting comminit,,就默认从当前分支的最近提交创建分支.

列出分支

  1. git branch #列出本地分支
  2.  
  3. git branch -a #列出本地和远程分支
  4.  
  5. git branch --merged #已经合并的分支.包括现在 所在的分支,master分支 和 新分支(未做任何开发提交)
  6.  
  7. git branch --no-merged # 没有合并的分支。已经开发提交了的分支。但是还没合并过来的.

切换分支

  1. git checkout branch-name

合并分支

  1. git merge branch-name #合并分支到本分支

分支修改删除

  1. [root@chehce a1]# git branch -m dev dev1 #修改
  2. [root@chehce a1]# git branch
  3. dev1
  4. * master
  5. t1
  6. [root@chehce a1]# git branch -d dev1 #删除
  7. Deleted branch dev1 (was fefba76).
  8. [root@chehce a1]# git branch
  9. * master
  10. t1

4. git的撤销和回滚

1. git commint 之前

未添加到暂存区的撤销(没有git add),比如修改了文件(新增加文件没法撤销,只能手动删除文件).

可以通过

  1. git checkout -- filename 来撤销修改
  1. # On branch master
  2. # Changes not staged for commit:
  3. # (use "git add <file>..." to update what will be committed)
  4. # (use "git checkout -- <file>..." to discard changes in working directory)
  5. #
  6. # modified: .txt
  7. #
  8. no changes added to commit (use "git add" and/or "git commit -a")

如果想将多个文件一次性撤销可以用,(用于撤销文件中内容的修改,新建文件不起作用)

  1. git checkout -- .

要撤销新建的文件可以用:

  1. gti clean -fd

添加到暂存区域的(执行了 git add 命令的),状态如下

  1. [root@chehce git]# git status
  2. # On branch master
  3. # Changes to be committed:
  4. # (use "git reset HEAD <file>..." to unstage)
  5. #
  6. # modified: .txt
  7. # modified: .txt
  8. #

利用git reset file来撤销  , 随后 用  git checkout -- filename 。 这两步来回到没更改的状态.

  1. [root@chehce git]# git status
  2. # On branch master
  3. # Changes not staged for commit:
  4. # (use "git add <file>..." to update what will be committed)
  5. # (use "git checkout -- <file>..." to discard changes in working directory)
  6. #
  7. # modified: .txt
  8. # modified: .txt
  9. #
  10. no changes added to commit (use "git add" and/or "git commit -a")
  11. [root@chehce git]# git add .
  12. [root@chehce git]# git status
  13. # On branch master
  14. # Changes to be committed:
  15. # (use "git reset HEAD <file>..." to unstage)
  16. #
  17. # modified: .txt
  18. # modified: .txt
  19. #
  20. [root@chehce git]# git reset .
  21. Unstaged changes after reset:
  22. M .txt
  23. M .txt
  24. [root@chehce git]# git status
  25. # On branch master
  26. # Changes not staged for commit:
  27. # (use "git add <file>..." to update what will be committed)
  28. # (use "git checkout -- <file>..." to discard changes in working directory)
  29. #
  30. # modified: .txt
  31. # modified: .txt
  32. #
  33. no changes added to commit (use "git add" and/or "git commit -a")
  34. [root@chehce git]# git checkout -- .
  35. [root@chehce git]# git status
  36. # On branch master
  37. nothing to commit, working directory clean

2. git commint 以后

如果当commit提交后想撤销的话,这就需要revert命令。git revert 命令是撤销某次操作,而在此次操作之前和之后的提交记录都会保留。

先修改了几个文件然后commit 再用git log查看提交记录。

  1. commit 2842c8065322085c31fb7b8207b6296047c4ea3
  2. Author: songguojun <songgj@kingnet.sh>
  3. Date: Sat Apr :: +
  4.  
  5. add content

然后使用revert  后面跟上git提交的commitid

  1. git revert 2842c8065322085c31fb7b8207b6296047c4ea3

然后在推送到远端更新远程仓库代码,修改的文件就撤销回来了。注意的是revert奇数次生效,偶数次又回到之前的修改状态。比如一个文件内容是a,那么修改为ab,revert后文件变成了a,如果在revert后文件又还原成ab了。在上次revert的基础上再来一次 reavert就回到了原来。

所以  git revert 是可逆的,该命令主要在对某些修改了的文件恢复到某个 状态,不管这个文件是在哪个时间修改的。然后又可以恢复回来.

3.回到之前某个版本reset

还有就是如果想回到之前某个版本,可以用reset命令,可以回退到某次提交,那次提交之后的提交都会回滚,不过这种覆盖是不可逆的,之前的提交记录都没有了。所以平时开发中尽量注意,避免使用reset。相当于这个版本的之后的添加的修改的文件全部都没有了。

用法:

  1. git reset --hard commit_id
  • --hard – 强制将缓存区和工作目录都同步到你指定的提交

git reset commit_id  查看日志的时候已经没有以前的提交记录,但是文件还没有被清理。  用  git clean -df 可以清理.就得到了回滚时候的状态.

这个时候,git服务器的版本肯定是领先本地reset后的版本,要想push的话,的用 -f 参数

  1. git push -f origin master:master

同理。其它的客户端想要pull也要用 -f

  1. git pull -f origin master:master

git reset 参数使用

4.挽救reset 的重置

git reflog show master  ,通过logs查看分支指向的变迁。 默认非裸版本库提供分支日志功能。

  1. [root@chehce git]# git reflog show master
  2. 8b0d08c master@{}: reset: moving to 8b0d08c
  3. master@{}: revert: Revert "Revert "touch .txt""
  4. f3002c2 master@{}: revert: Revert "touch 1.txt"
  5. 7612e4d master@{}: commit:
  6. a4aaa0c master@{}: commit: -
  7. 8b0d08c master@{}: reset: moving to 8b0d08cf0f65d81fc5510c895f7a34d5adc7fd6f
  8. 07d57c8 master@{}: pull: Fast-forward
  9. 8b0d08c master@{}: reset: moving to 8b0d08cf0f65d81fc5510c895f7a34d5adc7fd6f
  10. 78660cc master@{}: revert: Revert "Revert """
  11. 701a173 master@{}: revert: Revert ""
  12. 5f02a91 master@{}: commit:
  13. 07d57c8 master@{}: branch: Created from refs/remotes/origin/master

注意:  最新的改变放在了最前面

  1. [root@chehce git]# git reflog show master
  2. 8b0d08c master@{}: reset: moving to 8b0d08cf0f65d81fc5510c895f7a34d5adc7fd6f
  3. 50df03d master@{}: commit: -
  4. 8b0d08c master@{}: reset: moving to 8b0d08c
  5. master@{}: revert: Revert "Revert "touch .txt""
  6. f3002c2 master@{}: revert: Revert "touch 1.txt"
  7. 7612e4d master@{}: commit:
  8. a4aaa0c master@{}: commit: -
  9. 8b0d08c master@{}: reset: moving to 8b0d08cf0f65d81fc5510c895f7a34d5adc7fd6f
  10. 07d57c8 master@{}: pull: Fast-forward
  11. 8b0d08c master@{}: reset: moving to 8b0d08cf0f65d81fc5510c895f7a34d5adc7fd6f
  12. 78660cc master@{}: revert: Revert "Revert """
  13. 701a173 master@{}: revert: Revert ""
  14. 5f02a91 master@{}: commit:
  15. 07d57c8 master@{}: branch: Created from refs/remotes/origin/master
  16. [root@chehce git]# git reset --hard master@{1} #重置master为1次变更之前的值。
  17. HEAD is now at 50df03d -
  18. [root@chehce git]# ls
  19. .txt .txt .txt .txt

修改 最新一次的提交说明

更改最新一次的 提交 说明。

git commit --amend -m "添加6.txt"

5. git cherry-pick (捡选,从其它分支捡选需要的 提交 到本HEAD(分支))

参考: https://www.jianshu.com/p/4ac5f52d9210

首先,Git必须知道当前版本是哪个版本,在Git中,用HEAD
表示当前版本,也就是最新的提交3628164...882e1e0
(注意我的提交ID和你的肯定不一样),上一个版本就是HEAD^
,上上一个版本就是HEAD^^
,当然往上100个版本写100个^
比较容易数不过来,所以写成HEAD~100
  1. [root@chehce git]# git checkout dev
  2. Branch dev set up to track remote branch dev from origin.
  3. Switched to a new branch 'dev'
  4. [root@chehce git]# cat .git/HEAD #当前版本是dev,也就是指向dev上最新的一个提交
  5. ref: refs/heads/dev
  6. [root@chehce git]# git checkout master
  7. Switched to branch 'master'
  8. [root@chehce git]# cat .git/HEAD #切换到master上, 当前版本是 master。
  9. ref: refs/heads/master

举例:  有 A B C D E F 6个提交,现在要去掉提交D

1.  上演第一幕: 干掉 D

  1. [root@chehce git]# git log --oneline --decorate -6 #查看6次提交
  2. 3ba31ba (HEAD, tag: F, master) add F.txt
  3. 144cb97 (tag: E) add E.txt
  4. (tag: D) add D.txt
  5. 424dd1c (tag: C) add C.txt
  6. c22e544 (tag: B) add B.txt
  7. 628e28c (tag: A) add A.txt
  8. [root@chehce git]# git checkout C #切换到tag C
  9. Note: checking out 'C'.
  10.  
  11. You are in 'detached HEAD' state. You can look around, make experimental
  12. changes and commit them, and you can discard any commits you make in this
  13. state without impacting any branches by performing another checkout.
  14.  
  15. If you want to create a new branch to retain commits you create, you may
  16. do so (now or later) by using -b with the checkout command again. Example:
  17.  
  18. git checkout -b new_branch_name
  19.  
  20. HEAD is now at 424dd1c... add C.txt
  21. [root@chehce git]# cat .git
  22. .git/ .gitignore
  23. [root@chehce git]# cat .git/HEAD #查看现在的HEAD 是指向 C
  24. 424dd1c5c63353127836b86511868af18812125e
  25. [root@chehce git]# git cherry-pick E #将tag E 选捡到当前
  26. [detached HEAD 5c8783e] add E.txt
  27. file changed, insertion(+)
  28. create mode E.txt
  29. [root@chehce git]# git cherry-pick F #将 tag F 选捡到当前
  1. [detached HEAD 309f21e] add F.txt file changed, insertion(+) create mode F.txt
  2.  
  3. [root@chehce git]# git checkout master #切换到master
    Warning: you are leaving commits behind, not connected to
    any of your branches:
      309f21e add F.txt
      5c8783e add E.txt
    If you want to keep them by creating a new branch, this may be a good time
    to do so with:
      git branch new_branch_name 309f21e
    Switched to branch 'master'
  4.  
  5. [root@chehce git]# git reset --hard HEAD@{1} #将HEAD@{1}相当于切换回master分支前的指向。也就是 重新选捡后的tag F 的commintid, 也可以把HEAD@{1}直接换成
  1. 重新选捡后的tag F commintid。可以看一下下面的演示。
  1. HEAD is now at 309f21e add F.txt

[root@chehce git]# git log --oneline --decorate -5   #在次查看发现tag D 已经不在了。
309f21e (HEAD, master) add F.txt
5c8783e add E.txt
424dd1c (tag: C) add C.txt
c22e544 (tag: B) add B.txt
628e28c (tag: A) add A.txt

  1. [root@chehce git]# git log --oneline --decorate
  2. 3ba31ba (HEAD, tag: F, master) add F.txt
  3. 144cb97 (tag: E) add E.txt
  4. (tag: D) add D.txt
  5. 424dd1c (tag: C) add C.txt
  6. c22e544 (tag: B) add B.txt
  7. 628e28c (tag: A) add A.txt
  8. cda8768 add .txt .txt
  9. 9b526cb (tag: version1.) 修改ha.txt
  10. 990c9e0 mv haha.txt to ha.txt
  11. a56abde (tag: old_practice) Merge commit '9eb33d3c55803cd9e989859e91af547eefb9a4b3'
  12. 9eb33d3 haha
  13. 50df03d -
  14. 8b0d08c touch .txt
  15. [root@chehce git]# git log --oneline --decorate -
  16. 3ba31ba (HEAD, tag: F, master) add F.txt
  17. 144cb97 (tag: E) add E.txt
  18. (tag: D) add D.txt
  19. 424dd1c (tag: C) add C.txt
  20. c22e544 (tag: B) add B.txt
  21. 628e28c (tag: A) add A.txt
  22. [root@chehce git]# git checkout C
  23. Note: checking out 'C'.
  24.  
  25. You are in 'detached HEAD' state. You can look around, make experimental
  26. changes and commit them, and you can discard any commits you make in this
  27. state without impacting any branches by performing another checkout.
  28.  
  29. If you want to create a new branch to retain commits you create, you may
  30. do so (now or later) by using -b with the checkout command again. Example:
  31.  
  32. git checkout -b new_branch_name
  33.  
  34. HEAD is now at 424dd1c... add C.txt
  35. [root@chehce git]# cat .git
  36. .git/ .gitignore
  37. [root@chehce git]# cat .git/HEAD
  38. 424dd1c5c63353127836b86511868af18812125e
  39. [root@chehce git]# cat .git/refs/
  40. heads/ remotes/ tags/
  41. [root@chehce git]# cat .git/refs/heads/master
  42. 3ba31baf3649969d10d94b864b70828ce466fe81
  43. [root@chehce git]#
  44. [root@chehce git]# git cherry-pick E
  45. [detached HEAD 433ac03] add E.txt
  46. file changed, insertion(+)
  47. create mode E.txt
  48. [root@chehce git]# cat .git/refs/heads/master
  49. 3ba31baf3649969d10d94b864b70828ce466fe81
  50. [root@chehce git]# cat .git/HEAD
  51. 433ac03c1843c90a677eeef63ffc18b5eff875aa
  52. [root@chehce git]# git cherry-pick F
  53. [detached HEAD 48a27b6] add F.txt
  54. file changed, insertion(+)
  55. create mode F.txt
  56. [root@chehce git]# cat .git/refs/heads/master
  57. 3ba31baf3649969d10d94b864b70828ce466fe81
  58. [root@chehce git]# cat .git/HEAD #现在 HEAD 的指针
  59. 48a27b694a5c1820588716b6c51fb7b611b3e4d4
  60. [root@chehce git]# git checkout master
  61. Warning: you are leaving commits behind, not connected to
  62. any of your branches:
  63.  
  64. 48a27b6 add F.txt
  65. 433ac03 add E.txt
  66.  
  67. If you want to keep them by creating a new branch, this may be a good time
  68. to do so with:
  69.  
  70. git branch new_branch_name 48a27b6
  71.  
  72. Switched to branch 'master'
  73. [root@chehce git]# cat .git/HEAD #master的指针还是在master
  74. ref: refs/heads/master
  75. [root@chehce git]# git reset --hard 48a27b694a5c1820588716b6c51fb7b611b3e4d4 #将master的 指针指向上面 tag F 的HEAD
  76. HEAD is now at 48a27b6 add F.txt
  77. [root@chehce git]# git log --oneline --decorate
  78. 48a27b6 (HEAD, master) add F.txt
  79. 433ac03 add E.txt
  80. 424dd1c (tag: C) add C.txt
  81. c22e544 (tag: B) add B.txt
  82. 628e28c (tag: A) add A.txt
  83. cda8768 add .txt .txt
  84. 9b526cb (tag: version1.) 修改ha.txt
  85. 990c9e0 mv haha.txt to ha.txt
  86. a56abde (tag: old_practice) Merge commit '9eb33d3c55803cd9e989859e91af547eefb9a4b3'
  87. 9eb33d3 haha
  88. 50df03d -
  89. 8b0d08c touch .txt

HEAD@{1}的演示

例子2:

从一个分支cherry-pick多个commit

https://blog.csdn.net/amanicspater/article/details/78624973

5. git检出

HEAD(头指针) 的重置即检出。

HEAD 指向了master分支.

  1. [root@chehce git]# cat .git/HEAD
  2. ref: refs/heads/master
  1. [root@chehce git]# git checkout 8b0d08cf0f65d81fc5510c895f7a34d5adc7fd6f #检出到commintID
  2. Note: checking out '8b0d08cf0f65d81fc5510c895f7a34d5adc7fd6f'.
  3.  
  4. You are in 'detached HEAD' state. You can look around, make experimental
  5. changes and commit them, and you can discard any commits you make in this
  6. state without impacting any branches by performing another checkout.
  7.  
  8. If you want to create a new branch to retain commits you create, you may
  9. do so (now or later) by using -b with the checkout command again. Example:
  10.  
  11. git checkout -b new_branch_name
  12.  
  13. HEAD is now at 8b0d08c... touch .txt
  14. [root@chehce git]# cat .git/HEAD #HEAD 头指针指向不在是master,指向了一个具体的提交ID
  15. 8b0d08cf0f65d81fc5510c895f7a34d5adc7fd6f

# 新建文件并且提交

  1. [root@chehce git]# vim haha.txt
  2. [root@chehce git]# git add .
  3. [root@chehce git]# git commit -m "haha"
  4. [detached HEAD 9eb33d3] haha
  5. file changed, insertion(+)
  6. create mode haha.txt
  7. [root@chehce git]# ls
  8. .txt haha.txt
  9. [root@chehce git]# cat .git/HEAD
  10. 9eb33d3c55803cd9e989859e91af547eefb9a4b3
  11. [root@chehce git]# git log
  12. commit 9eb33d3c55803cd9e989859e91af547eefb9a4b3
  13. Author: che <che@example.com>
  14. Date: Thu Jun :: +
  15.  
  16. haha
  17.  
  18. commit 8b0d08cf0f65d81fc5510c895f7a34d5adc7fd6f
  19. Author: chepingan <@qq.com>
  20. Date: Thu Jun :: +
  21.  
  22. touch .txt
  23. [root@chehce git]# git log --graph --pretty=oneline #新的提交是建立在之前的提交上。
  24. * 9eb33d3c55803cd9e989859e91af547eefb9a4b3 haha
  25. * 8b0d08cf0f65d81fc5510c895f7a34d5adc7fd6f touch .txt

切换回到 master分支,上面新建的文件不见了。日志里面也没有它的提交记录。如何让他可以到master分支呢? 合并,对将合并到master分支。

  1. git merge 9eb33d3c55803cd9e989859e91af547eefb9a4b3

发现文件出现了。

  1. [root@chehce git]# git log --graph --pretty=oneline
  2. * a56abde92aeebe9bcd33bac309891de57ca07a2e Merge commit '9eb33d3c55803cd9e989859e91af547eefb9a4b3' #合并
  3. |\
  4. | * 9eb33d3c55803cd9e989859e91af547eefb9a4b3 haha # 创建文件提交
  5. * | 50df03d3229824e4f4a4a4a3972e23565eff1b27 -4 #检出到 commitid
  6. |/
  7. * 8b0d08cf0f65d81fc5510c895f7a34d5adc7fd6f touch .txt

6. git tag操作

https://blog.csdn.net/wangjia55/article/details/8793577/

标签也叫做里程碑。

1. 标签列出和查看

git tag   #查看当前分支下所有标签

  1. [root@chehce git]# git tag
  2. A
  3. old_practice

git tag -l "*old*"   #过滤标签

  1. [root@chehce git]# git tag -l "*old*"
  2. old_practice

git describe   #显示可从提交中获得的最新标签。

  1. [root@chehce git]# git describe
  2. old_practice--g9b526cb #这个表示的是在tag-修改次数-g现在的提交id.

git log  --oneline  --decorate   #在提交id旁显示该提交的关联的引用(里程碑或者分支)。

  1. [root@chehce git]# git log --oneline --decorate
  2. 9b526cb (HEAD, master) 修改ha.txt
  3. 990c9e0 mv haha.txt to ha.txt
  4. a56abde (tag: old_practice, origin/master) Merge commit '9eb33d3c55803cd9e989859e91af547eefb9a4b3'
  5. 9eb33d3 haha
  6. 50df03d -
  7. 8b0d08c (tag: A) touch .txt

git show  标签名字

  1. [root@chehce git]# git show version1.
  2. tag version1.
  3. Tagger: che <che@example.com>
  4. Date: Fri Jun :: +
  5.  
  6. version 1.0
  7.  
  8. commit 9b526cb779001a371b252a180d9403da5d391ce8
  9. Author: che <che@example.com>
  10. Date: Fri Jun :: +
  11.  
  12. 修改ha.txt
  13.  
  14. diff --git a/ha.txt b/ha.txt
  15. index 3740b8b..89f2424
  16. --- a/ha.txt
  17. +++ b/ha.txt
  18. @@ - +, @@
  19. afaf
  20. +afsf
  21. +

2. 打标签

给当前这点打标签,不用跟其它的

  1. git tag "标签名字" -m "说明"
  1. [root@chehce git]# git tag "version1.0" -m "version 1.0"
  2. [root@chehce git]# git describe
  3. version1.
  4. [root@chehce git]# git log --oneline --decorate
  5. 9b526cb (HEAD, tag: version1., master) 修改ha.txt
  6. 990c9e0 mv haha.txt to ha.txt
  7. a56abde (tag: old_practice, origin/master) Merge commit '9eb33d3c55803cd9e989859e91af547eefb9a4b3'
  8. 9eb33d3 haha
  9. 50df03d -
  10. 8b0d08c (tag: A) touch .txt

给某一个 commmint id 打标签

  1. git tag "标签名字" commintID -m "说明"
  1. [root@chehce git]# git tag "version1.2" e7bb519a7b89c85e52c5e2fea076d19e072abe34 -m "version 1.2"
  2. [root@chehce git]# git tag
  3. A
  4. old_practice
  5. version1.
  6. version1.
  7. version1.
  8. [root@chehce git]# git log --oneline --decorate
  9. e7bb519 (HEAD, tag: version1., master) add .txt
  10. a80132d (tag: version1.) add .txt
  11. 9b526cb (tag: version1.) 修改ha.txt
  12. 990c9e0 mv haha.txt to ha.txt
  13. a56abde (tag: old_practice, origin/master) Merge commit '9eb33d3c55803cd9e989859e91af547eefb9a4b3'
  14. 9eb33d3 haha
  15. 50df03d -
  16. 8b0d08c (tag: A) touch .txt

3. 切换到标签(就相当于直接切换到某个commint ID)

  1. [root@chehce git]# git tag
  2. A
  3. old_practice
  4. version1.
  5. version1.
  6. [root@chehce git]# ls
  7. .txt .txt .txt .txt .txt ha.txt
  8. [root@chehce git]# git checkout version1.0 #切换到version1.0所引用的commint id。
  9. Note: checking out 'version1.0'.
  10.  
  11. You are in 'detached HEAD' state. You can look around, make experimental
  12. changes and commit them, and you can discard any commits you make in this
  13. state without impacting any branches by performing another checkout.
  14.  
  15. If you want to create a new branch to retain commits you create, you may
  16. do so (now or later) by using -b with the checkout command again. Example:
  17.  
  18. git checkout -b new_branch_name
  19.  
  20. HEAD is now at 9b526cb... 修改ha.txt
  21. [root@chehce git]# git branch
  22. * (detached from version1.)
  23. dev1
  24. master
  25. [root@chehce git]# ls
  26. .txt .txt .txt .txt ha.txt

4. 删除标签

git  tag -d "标签名字"

  1. [root@chehce git]# git tag -d "version1.2"
  2. Deleted tag 'version1.2' (was da064c9)
  3. [root@chehce git]# git tag
  4. A
  5. old_practice
  6. version1.
  7. version1.

5. 标签发布(提交,默认是不会提交到git服务器的)

git push origin v0.1.2 # 将v0.1.2标签提交到git服务器
 git push origin –tags # 将本地所有标签一次性提交到git服务器

  1. [root@chehce git]# git push --tags
  2. Counting objects: , done.
  3. Delta compression using up to threads.
  4. Compressing objects: % (/), done.
  5. Writing objects: % (/), bytes | bytes/s, done.
  6. Total (delta ), reused (delta )
  7. To 172.10.30.228:/root/test/git
  8. * [new tag] A -> A
  9. * [new tag] old_practice -> old_practice
  10. * [new tag] version1. -> version1.
  11. * [new tag] version1. -> version1.

7. git服务器搭建

git服务端

  • git服务器端和客户端可以位于同一台机器上。服务器端时用来协同多个开发客户端进行版本的同步,提供共享功能。

  • 第一步,安装git:

  1. $ sudo apt-get install git

第二步,创建一个git用户,用来运行git服务:

  1. $ sudo adduser git

第三步,创建证书登录:(也可直接用密码忽略这一步)

  1. 收集所有需要登录的用户的公钥,就是他们自己的id_rsa.pub文件,
  2. 把所有公钥导入到/home/git/.ssh/authorized_keys文件里,一行一个。

第四步,初始化Git仓库:

  1. 服务器上的Git仓库通常都以.git结尾。然后,把owner改为git
  2.  
  3. $ sudo git init --bare sample.git #--bare创建一个裸仓库,git服务器端使用。

git权限配置

  1. sudo chown -R git:git sample.git
  2.  
  3. usermod -s /usr/bin/git-shell git #出于安全考虑,第二步创建的git用户不允许登录shell.
  4. git用户可以正常通过ssh使用git,但无法登录shell,因为git用户指定的git-shell每次一登录就自动退出。

git 客户端

第一步:从服务器端克隆git版本库。

  1. git clone git@172.10.30.228:/opt/gamesoul.git

第二步: 更改配置文件用于表明客户端的身份,要不然会跟服务端一样

  1. git config --global user.email "you@example.com" #根据实际情况填写
  2. git config --global user.name "Your Name"
  3.  
  4. git config -l

第三步:git 客户端push一些文件到服务器。

  1. echo >.txt
  2. git add .txt
  3. git commit -m "1.txt"
  4. git push #客户端1 push到服务器
  5.  
  6. git pull #客户端2 pull更新的文件到本地
  • 开发时,我认为一般是在master以外的分支上作开发,然后合并到本地master分支,再push到远程仓库

  • git 报错

  1. Git push 报错1warning: push.default is unset; 解决如下
  2.  
  3. //设置默认行为为simple。 Git2.0以后
  4. git config --global push.default simple
  5. //设置默认行为为matching。Git2.0以前
  6. git config --global push.default matching
  7.  
  8. git push报错2
  9.  
  10. ! [rejected] master -> master (fetch first)
  11. error: failed to push some refs to 'git@172.10.30.228:/opt/gamesoul.git'
  12.  
  13. 原因: 因为本地的内容和服务器上不同(服务器上有本地没有),解决
  14.  
  15. git pull ,在git push

git之一: git基础的更多相关文章

  1. GIT之二 基础篇(1)

    GIT基础 取得项目的 Git 仓库 有两种取得 Git 项目仓库的方法.第一种是在现存的目录下,通过导入所有文件来创建新的 Git 仓库.第二种是从已有的 Git 仓库克隆出一个新的镜像仓库来. 在 ...

  2. Git命令汇总(基础篇)

    自己用Git有一段时间了,随着项目越来越多,功能分支也随之增加,从简单的基础命令到随心所欲,需要自己不断地去尝试总结,下面来分享一下我的Git使用总结. 本章基础篇主要讲解一些Git代码提交流程和Gi ...

  3. Windows 下安装Git工具及基础使用

    Git简介 git是很好一个工具使用,可以执行liunx命令,有git环境后windows系统就可以进行shell命令操作,就可以添加其他liunx辅助软件进行执行,git也代码库管理工具,无论是上传 ...

  4. Git的常见基础操作命令

    Git的常见基础操作命令 1安装初始化 1.1安装git本地安装Windows版本 下载地址: https://git-scm.com/downloads/ 1.2初始化Git用户信息配置 配置git ...

  5. 3分钟学会git命令的基础使用

    前言废话 下面我们就来看看gitlab服务器搭建好(http://www.cnblogs.com/JeremyWYL/p/8258368.html) 之后,git命令的基础使用,基本上就能满足我们平时 ...

  6. Git操作(基础篇)

    Git操作(基础篇) Git是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目.Git的读音为/gɪt/.Git是一个开源的分布式版本控制系统,用以有效.高速的处理从很小到非常 ...

  7. Git学习笔记----基础运用

    安装Git Windows: 进入官网下载或百度网盘下载 Git(V2.23_x64) 提取码:uf2x Ubuntu: sudo apt-get -install git 安装完成之后打开git命令 ...

  8. 【Git 系列】基础知识全集

    Git 是一种分布式版本控制系统,它可以不受网络连接的限制,加上其它众多优点,目前已经成为程序开发人员做项目版本管理时的首选,非开发人员也可以用 Git 来做自己的文档版本管理工具. 一.Git 基础 ...

  9. Git Pro - (1) 基础

    近乎所有操作都可本地执行 在Git中的绝大多数操作都只需要访问本地文件和资源,不用连网. 三种状态 对于任何一个文件,在 Git 内都只有三 种状态:已提交(committed),已修改(modifi ...

随机推荐

  1. hibernate映射关系(多对多)

    Student与Teacher关系多对多(只建了2个实体类) public class Student { private int id; private String name; private S ...

  2. .net常用的代码生成工具

    之前很多从事C#开发的用过动软代码生成器,然后随着IT技术的快速发展,涌现出很多优秀的工具关于.Net的,首推微软的Entity Framework,其次是NHibernate.Entity Fram ...

  3. [C]控制外部变量访问权限的extern和static关键字

    一.extern 概述 编译器是由上至下编译源文件的,当遇到一些函数引用外部全局变量,而这个变量被定义在该函数声明主体的下方,又或者引用自其它的编译单元,这个情况就需要extern来向编译器表明此变量 ...

  4. 安装mysql5.7与创建用户和远程登录授权

    环境:ubuntu18.04 参考文章:安装并远程登录授权:https://www.cnblogs.com/chancy/p/9444187.html 用户管理:https://www.cnblogs ...

  5. 实体类和json互相转换

    /// <summary> /// 将实体类转换为json数据 /// </summary> /// <returns></returns> publi ...

  6. web中绝对路径换虚拟路径

    最近在做一个web项目,将图片上传到服务器后,再访问时拿到的是绝对路劲,而需要的是虚拟路劲.经过一番折腾找到了下列方法可以直接转换. /// <summary>        /// 将W ...

  7. lua 复制table

    cocos2d-lua提供了复制方法clone(),源码如下: function clone(object) local lookup_table = {} local function _copy( ...

  8. Android 组件化方案探索与思考

    Android 组件化方案探索与思考 组件化项目,通过gradle脚本,实现module在编译期隔离,运行期按需加载,实现组件间解耦,高效单独调试. 本项目github地址 https://githu ...

  9. PID控制器开发笔记之五:变积分PID控制器的实现

    在普通的PID控制算法中,由于积分系数Ki是常数,所以在整个控制过程中,积分增量是不变的.然而,系统对于积分项的要求是,系统偏差大时,积分作用应该减弱甚至是全无,而在偏差小时,则应该加强.积分系数取大 ...

  10. Modbus库开发笔记之三:Modbus TCP Server开发

    在完成了前面的工作后,我们就可以实现有针对性的应用了,首先我们来实现Modbus TCP的服务器端应用.当然我们不是做具体的应用,而是对Modbus TCP的服务器端应用进行封装以供有需要时调用. 这 ...