创建仓库

新建普通仓库:

jxdong@ubuntu-server:~/workspace/git$ git init
Reinitialized existing Git repository in /home/jxdong/workspace/git/.git/

新建 bare 仓库:

jxdong@ubuntu-server:~/workspace/git.git$ git init --bare
Initialized empty Git repository in /home/jxdong/workspace/git.git/

bare 仓库里面不包括源码,仅仅具有版本号管理管理功能,无法运行 checkout 等代码管理操作:

jxdong@ubuntu-server:~/workspace/git.git$ ls
branches config description HEAD hooks info objects refs

克隆仓库

克隆普通 git 仓库:

jxdong@ubuntu-server:~/workspace$ git clone git git_1
Cloning into 'git_1'...
done.
warning: You appear to have cloned an empty repository.

克隆为 bare 仓库:

jxdong@ubuntu-server:~/workspace$ git clone git git_1.git --bare
Cloning into bare repository 'git_1.git'...
done.
warning: You appear to have cloned an empty repository.

由于我们的仓库里面没有提交,所以 git 提示我们克隆了空的仓库

提交代码

第一次提交初始化的版本号能够是什么都没有:

jxdong@ubuntu-server:~/workspace/git$ git commit -m "init: first version." --allow-empty


[master (root-commit) 8a22ddf] init: first version.

jxdong@ubuntu-server:~/workspace/git$ git log 

commit 8a22ddfaaf374531dbdba02ef40bb10006057f6f

Author: Royce Jiang <jiangxd@embedinfo.com>

Date:   Fri May 30 11:40:15 2014 +0800

    init: first version.

加入项目文件

jxdong@ubuntu-server:~/workspace/git$ echo test > myfile1.txt

jxdong@ubuntu-server:~/workspace/git$ git add myfile1.txt

提交到本地仓库

jxdong@ubuntu-server:~/workspace/git$ git commit -m "work: add file1"

[master 8ed6d03] work: add file1

1 file changed, 1 insertion(+)

create mode 100644 myfile1.txt

查看历史

jxdong@ubuntu-server:~/workspace/git$ git log 

commit 8ed6d030278c0aef63052d6ba1946281c8a50ec8

Author: Royce Jiang <jiangxd@embedinfo.com>

Date:   Fri May 30 11:43:52 2014 +0800

    work: add file1

commit 8a22ddfaaf374531dbdba02ef40bb10006057f6f

Author: Royce Jiang <jiangxd@embedinfo.com>

Date:   Fri May 30 11:40:15 2014 +0800

    init: first version.

回退版本号

作为提交的回退

revert 会撤销一次 commit 并把撤销操作作为 commit 提交,所以历史里面包括这次撤销操作的 commit

jxdong@ubuntu-server:~/workspace/git$ git log 

commit 746d6596b7d5b84db9c27ceef1063be977510c4e

Author: Royce Jiang <jiangxd@embedinfo.com>

Date:   Fri May 30 11:44:22 2014 +0800

    Revert "work: add file1"

    This reverts commit 8ed6d030278c0aef63052d6ba1946281c8a50ec8.

commit 8ed6d030278c0aef63052d6ba1946281c8a50ec8

Author: Royce Jiang <jiangxd@embedinfo.com>

Date:   Fri May 30 11:43:52 2014 +0800

    work: add file1

commit 8a22ddfaaf374531dbdba02ef40bb10006057f6f

Author: Royce Jiang <jiangxd@embedinfo.com>

Date:   Fri May 30 11:40:15 2014 +0800

    init: first version.

jxdong@ubuntu-server:~/workspace/git$ ls

强制删除一次提交

reset 与 –hard 參数,将会真正去掉这次commit, 将会删除 git 历史。比較危急,用于那些失败的 提交。

jxdong@ubuntu-server:~/workspace/git$ git reset --hard HEAD~1

HEAD is now at 8ed6d03 work: add file1

jxdong@ubuntu-server:~/workspace/git$ git log 

commit 8ed6d030278c0aef63052d6ba1946281c8a50ec8

Author: Royce Jiang <jiangxd@embedinfo.com>

Date:   Fri May 30 11:43:52 2014 +0800

    work: add file1

commit 8a22ddfaaf374531dbdba02ef40bb10006057f6f

Author: Royce Jiang <jiangxd@embedinfo.com>

Date:   Fri May 30 11:40:15 2014 +0800

    init: first version.

jxdong@ubuntu-server:~/workspace/git$ ls 

myfile1.txt

改动提交历史

我们再加入一个文件,并提交。

jxdong@ubuntu-server:~/workspace/git$ echo test > myfile2.txt  

jxdong@ubuntu-server:~/workspace/git$ git add myfile2.txt

jxdong@ubuntu-server:~/workspace/git$ git commit -m "work: add file2"

[master 021f89c] work: add file2

1 file changed, 1 insertion(+)

create mode 100644 myfile2.txt

jxdong@ubuntu-server:~/workspace/git$ git log 

commit 021f89cfd5aa195cd841eb0e64a2d712cc1f8356

Author: Royce Jiang <jiangxd@embedinfo.com>

Date:   Fri May 30 11:45:49 2014 +0800

    work: add file2

commit 8ed6d030278c0aef63052d6ba1946281c8a50ec8

Author: Royce Jiang <jiangxd@embedinfo.com>

Date:   Fri May 30 11:43:52 2014 +0800

    work: add file1

commit 8a22ddfaaf374531dbdba02ef40bb10006057f6f

Author: Royce Jiang <jiangxd@embedinfo.com>

Date:   Fri May 30 11:40:15 2014 +0800

    init: first version.

rebase 命令能够回退到指定的提交处,然后改动这次提交之后的历史。

包括的功能:

  1. pick,採用这次提交,什么也不做
  2. reword,採用这次提交,仅仅是改动提交信息
  3. edit,採用提交,并停在这次提交这里,你能够開始改动这次提交
  4. squash,合并提交,将这次提交合并到之前的一次提交里面
  5. fixup,与squash 一致,直接合并忽略提交信息
  6. x,exec,运行一些命令

这里以合并为例

jxdong@ubuntu-server:~/workspace/git$ git rebase -i 8a22ddfaaf374531dbdba02ef40bb10006057f6f


pick 8ed6d03 work: add file1

pick 021f89c work: add file2

# Rebase 8a22ddf..021f89c onto 8a22ddf

#

# Commands:

#  p, pick = use commit

#  r, reword = use commit, but edit the commit message

#  e, edit = use commit, but stop for amending

#  s, squash = use commit, but meld into previous commit

#  f, fixup = like "squash", but discard this commit's log message

#  x, exec = run command (the rest of the line) using shell

#

# If you remove a line here THAT COMMIT WILL BE LOST.

# However, if you remove everything, the rebase will be aborted.

#

".git/rebase-merge/git-rebase-todo" 16L, 542C written                                 


Rebasing (2/2)

# This is a combination of 2 commits.

# The first commit's message is:

work: add file1

# This is the 2nd commit message:

work: add file2

# Please enter the commit message for your changes. Lines starting

# with '#' will be ignored, and an empty message aborts the commit.

# Not currently on any branch.

# Changes to be committed:

#   (use "git reset HEAD <file>..." to unstage)

#

#       new file:   myfile1.txt

#       new file:   myfile2.txt

#

                                                                                                            


~                                                                                                       


".git/COMMIT_EDITMSG" 18L, 438C written

[detached HEAD a5d9008] work: add file1

2 files changed, 2 insertions(+)

create mode 100644 myfile1.txt

create mode 100644 myfile2.txt

Successfully rebased and updated refs/heads/master.

看一下,合并后的历史

jxdong@ubuntu-server:~/workspace/git$ git log 

commit a5d9008e0d87302ed1dc9c5fa8957294efc3e403

Author: Royce Jiang <jiangxd@embedinfo.com>

Date:   Fri May 30 11:43:52 2014 +0800

    work: add file1

    work: add file2

commit 8a22ddfaaf374531dbdba02ef40bb10006057f6f

Author: Royce Jiang <jiangxd@embedinfo.com>

Date:   Fri May 30 11:40:15 2014 +0800

    init: first version.

生成补丁

补丁的生成,-n 后面数字表示将要生成之前的多少次提交作为布丁。

jxdong@ubuntu-server:~/workspace/git$ git format-patch -n1

0001-work-add-file1.patch

应用布丁

应用布丁,须要具有同样祖先的 git 仓库,以避免错误,我们開始克隆过一个仓库,他们是来自同一个祖先

jxdong@ubuntu-server:~/workspace/git$ cd ..

jxdong@ubuntu-server:~/workspace$ cd git

git/       git_1/     git_1.git/ git.git/   

jxdong@ubuntu-server:~/workspace$ cd git_1

git_1/     git_1.git/ 

jxdong@ubuntu-server:~/workspace$ cd git_1

jxdong@ubuntu-server:~/workspace/git_1$ git am ../git/0001-work-add-file1.patch

Applying: work: add file1

applying to an empty history

获取远端仓库代码

加入远端

jxdong@ubuntu-server:~/workspace/git_1$ git remote add git ../git

获代替码

jxdong@ubuntu-server:~/workspace/git_1$ git fetch git 

warning: no common commits

remote: Counting objects: 5, done.

remote: Compressing objects: 100% (3/3), done.

remote: Total 5 (delta 0), reused 0 (delta 0)

Unpacking objects: 100% (5/5), done.

From ../git

* [new branch]      master     -> git/master

查看全部分支

jxdong@ubuntu-server:~/workspace/git_1$ git branch -a

* master

  remotes/git/master

表示当前在本地的 master 分支,我们另一个远端叫 git 的仓库,里面包括一个 master 分支

合并分支

我们先在分支上再提交一次,这次也加入一个文件。

jxdong@ubuntu-server:~/workspace/git_1$ echo test2 > myfile2.txt 

jxdong@ubuntu-server:~/workspace/git_1$ git commit -m "work: modfy file2"

# 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:   myfile2.txt

#

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

jxdong@ubuntu-server:~/workspace/git_1$ git commit -am "work: modfy file2"

[master 503a6c9] work: modfy file2

1 file changed, 1 insertion(+), 1 deletion(-)

jxdong@ubuntu-server:~/workspace/git_1$ git log 

commit 503a6c922a6a0f0fc298c1b5b8289fd2babb435c

Author: Royce Jiang <jiangxd@embedinfo.com>

Date:   Fri May 30 11:50:30 2014 +0800

    work: modfy file2

commit 24651eb534f5f4db86e61e62f48d7959aaaf4e65

Author: Royce Jiang <jiangxd@embedinfo.com>

Date:   Fri May 30 11:43:52 2014 +0800

    work: add file1

    work: add file2

合并分支

jxdong@ubuntu-server:~/workspace/git_1$ git merge git/master 

Auto-merging myfile2.txt

CONFLICT (add/add): Merge conflict in myfile2.txt

Automatic merge failed; fix conflicts and then commit the result.

处理冲突

首先查看冲突

jxdong@ubuntu-server:~/workspace/git_1$ git diff

diff --cc myfile2.txt

index 180cf83,9daeafb..0000000

--- a/myfile2.txt

+++ b/myfile2.txt

@@@ -1,1 -1,1 +1,5 @@@

++<<<<<<< HEAD

+test2

++=======

+ test

++>>>>>>> git/master

改动冲突并提交

解决冲突主要是看须要保留本地的内容还是要合并的内容

jxdong@ubuntu-server:~/workspace/git_1$ git commit -a

Merge remote-tracking branch 'git/master'

Conflicts:

        myfile2.txt

#

# It looks like you may be committing a merge.

# If this is not correct, please remove the file

#       .git/MERGE_HEAD

# and try again.

# Please enter the commit message for your changes. Lines starting

# with '#' will be ignored, and an empty message aborts the commit.

# On branch master

提交完毕合并成功

jxdong@ubuntu-server:~/workspace/git_1$ git log 

commit d1feccc31ba37983e2d217ec7b497bffebdde3f1

Merge: 503a6c9 a5d9008

Author: Royce Jiang <jiangxd@embedinfo.com>

Date:   Fri May 30 11:56:46 2014 +0800

    Merge remote-tracking branch 'git/master'

    Conflicts:

        myfile2.txt

commit 503a6c922a6a0f0fc298c1b5b8289fd2babb435c

Author: Royce Jiang <jiangxd@embedinfo.com>

Date:   Fri May 30 11:50:30 2014 +0800

    work: modfy file2

commit 24651eb534f5f4db86e61e62f48d7959aaaf4e65

Author: Royce Jiang <jiangxd@embedinfo.com>

Date:   Fri May 30 11:43:52 2014 +0800

    work: add file1

    work: add file2

commit a5d9008e0d87302ed1dc9c5fa8957294efc3e403

Author: Royce Jiang <jiangxd@embedinfo.com>

Date:   Fri May 30 11:43:52 2014 +0800

    work: add file1

    work: add file2

commit 8a22ddfaaf374531dbdba02ef40bb10006057f6f

Author: Royce Jiang <jiangxd@embedinfo.com>

Date:   Fri May 30 11:40:15 2014 +0800

git 经常使用操作集锦的更多相关文章

  1. git命令行操作

    从本地上传代码到仓库(假设已经建好仓库): 1.初始化: git init 2.将所有文件加入缓存区: git add * 3.提交当前工作空间的修改内容: git commit -m 'commit ...

  2. Eclipse for Java EE软件操作集锦(二)

    看本文章之前请确保已经了解eclipse建立web工程,如果有疑问请查看本系列文章第一篇 eclipse软件操作集锦(一) 1.我们添加一个servlet 配置一下web.xml测试一下是否能正常显示 ...

  3. Git基本命令行操作 (转)

    Git远程操作详解   作者: 阮一峰 Git是目前最流行的版本管理系统,学会Git几乎成了开发者的必备技能. Git有很多优势,其中之一就是远程操作非常简便.本文详细介绍5个Git命令,它们的概念和 ...

  4. windows下使用TortoiseGit代替Git命令行操作

    windows下使用TortoiseGit代替Git命令行操作 大家在使用svn的时候,都非常喜欢使用小乌龟,也就是TortoiseSVN:那么git也有小乌龟版本,即TortoiseGit. 1.安 ...

  5. Git Book 中文版 - Git的撤消操作 - 重置, 签出 和 撤消

    Git Book 中文版 - Git的撤消操作 - 重置, 签出 和 撤消 Git的撤消操作 - 重置, 签出 和 撤消 Git提供了多种修复你开发过程中的错误的方法. 方法的选择取决于你的情况: 包 ...

  6. 2.4 Git 基础 - 撤消操作

    2.4 Git 基础 - 撤消操作 撤消操作 任何时候,你都有可能需要撤消刚才所做的某些操作.接下来,我们会介绍一些基本的撤消操作相关的命令.请注意,有些撤销操作是不可逆的,所以请务必谨慎小心,一旦失 ...

  7. delphi关于文件操作集锦

        关于文件操作集锦 取得该快捷方式的指向EXE关键词:快捷方式 LNK unit Unit1; interface usesWindows, Messages, SysUtils, Varian ...

  8. git上传中的排除的配置文件, git实际的操作代码;

    git上传中的排除的配置文件: git实际的操作 在主目录建立.gitignore文件并输入以下保存: *.class #package file *.war *.ear #kdiff3 ignore ...

  9. centos下升级git版本的操作记录

    在使用git pull.git push.git clone的时候,或者在使用jenkins发版的时候,可能会报类似如下的错误: error: The requested URL returned e ...

随机推荐

  1. Java学习(正则表达式、Date类、DateFormat类、Calendar类)

    一.正则表达式 1.概念:英语:Regular Expression,在代码中常简写为regex.正则表达式,是一个字符串,使用单个字符串来描述.用来定义匹配规则,匹配一系列符合某个句法规则的字符串. ...

  2. USACO 4.4 Shuttle Puzzle

    Shuttle PuzzleTraditional The Shuttle Puzzle of size 3 consists of 3 white marbles, 3 black marbles, ...

  3. 用 Java 实现一个快速排序算法

           快速排序是排序算法中效率最高的一种,它是利用递归的原理,把数组无限制的分成两个部分,直到所有数据都排好序为止. 快速排序是对冒泡排序的一种改进.它的基本思想是通过一趟排序将要排序的数据分 ...

  4. CentOS7安装和配置mongodb3.6

    (1)安装mongodb 1.参考文档 https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/ 2.创建yum源 #v ...

  5. CSU - 2056 a simple game

    Description 这一天,小A和小B在玩一个游戏,他俩每人都有一个整数,然后两人轮流对他们的整数进行操作,每次在下列两个操作任选一个: (1)对整数进行翻转,如1234翻转成4321 ,1200 ...

  6. 五、django rest_framework源码之版本控制剖析

    1 绪论 Djangorest_framework的版本控制允许用户更改不同客户端之间的行为,且提供了许多不同的版本控制方案.版本控制由传入的客户端请求确定,可以基于请求URL,也可以基于请求标头. ...

  7. 为什么Java7开始在数字中使用下划线

    JDK1.7的发布已经介绍了一些有用的特征,尽管大部分都是一些语法糖,但仍然极大地提高了代码的可读性和质量.其中的一个特征是介绍字面常量数字的下划线.从Java7开始,你就可以在你的Java代码里把长 ...

  8. NetCore+Dapper WebApi架构搭建(一):基本框架

    初衷是想用dapper搭建一个高性能的架构,因为dapper操作数据库的效率很高 1.VS创建一个NetCore WebApi的框架,然后解决方案添加一个NetStandard的类库 整个解决方案如图 ...

  9. c#程序员机试题

    一.题目: 有一数组: int[] arr = new int[] { 48,1,3,55,15,29,12,33,26,41,56,32}; 1.求出最大值 2.按每个数字的10位数分组(说明:0~ ...

  10. 深入理解Python生成器(Generator)

    我们可以通过列表生成式简单直接地创建一个列表,但是受到内存限制,列表容量肯定是有限的.而且,创建一个包含100万个元素的列表,不仅占用很大的存储空间,而且如果我们仅仅需要访问前面几个元素,那后面绝大多 ...