git 常用操作集锦

创建仓库

新建普通仓库:

[email protected]:~/workspace/git$ git init
Reinitialized existing Git repository in /home/jxdong/workspace/git/.git/

新建 bare 仓库:

[email protected]:~/workspace/git.git$ git init --bare
Initialized empty Git repository in /home/jxdong/workspace/git.git/

bare 仓库里面不包含源代码,只具有版本管理管理功能,无法执行 checkout 等代码管理操作:

[email protected]:~/workspace/git.git$ ls
branches  config  description  HEAD  hooks  info  objects  refs

克隆仓库

克隆普通 git 仓库:

[email protected]:~/workspace$ git clone git git_1
Cloning into ‘git_1‘...
done.
warning: You appear to have cloned an empty repository.

克隆为 bare 仓库:

[email protected]:~/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 提示我们克隆了空的仓库

提交代码

第一次提交初始化的版本可以是什么都没有:

[email protected]:~/workspace/git$ git commit -m "init: first version." --allow-empty

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

[email protected]:~/workspace/git$ git log

commit 8a22ddfaaf374531dbdba02ef40bb10006057f6f

Author: Royce Jiang <[email protected]>

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

init: first version.

添加项目文件

[email protected]:~/workspace/git$ echo test > myfile1.txt

[email protected]:~/workspace/git$ git add myfile1.txt

提交到本地仓库

[email protected]:~/workspace/git$ git commit -m "work: add file1"

[master 8ed6d03] work: add file1

1 file changed, 1 insertion(+)

create mode 100644 myfile1.txt

查看历史

[email protected]:~/workspace/git$ git log

commit 8ed6d030278c0aef63052d6ba1946281c8a50ec8

Author: Royce Jiang <[email protected]>

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

work: add file1

commit 8a22ddfaaf374531dbdba02ef40bb10006057f6f

Author: Royce Jiang <[email protected]>

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

init: first version.

回退版本

作为提交的回退

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

[email protected]:~/workspace/git$ git log

commit 746d6596b7d5b84db9c27ceef1063be977510c4e

Author: Royce Jiang <[email protected]>

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

Revert "work: add file1"

This reverts commit 8ed6d030278c0aef63052d6ba1946281c8a50ec8.

commit 8ed6d030278c0aef63052d6ba1946281c8a50ec8

Author: Royce Jiang <[email protected]>

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

work: add file1

commit 8a22ddfaaf374531dbdba02ef40bb10006057f6f

Author: Royce Jiang <[email protected]>

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

init: first version.

[email protected]:~/workspace/git$ ls

强制删除一次提交

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

[email protected]:~/workspace/git$ git reset --hard HEAD~1

HEAD is now at 8ed6d03 work: add file1

[email protected]:~/workspace/git$ git log

commit 8ed6d030278c0aef63052d6ba1946281c8a50ec8

Author: Royce Jiang <[email protected]>

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

work: add file1

commit 8a22ddfaaf374531dbdba02ef40bb10006057f6f

Author: Royce Jiang <[email protected]>

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

init: first version.

[email protected]:~/workspace/git$ ls

myfile1.txt

修改提交历史

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

[email protected]:~/workspace/git$ echo test > myfile2.txt

[email protected]:~/workspace/git$ git add myfile2.txt

[email protected]:~/workspace/git$ git commit -m "work: add file2"

[master 021f89c] work: add file2

1 file changed, 1 insertion(+)

create mode 100644 myfile2.txt

[email protected]:~/workspace/git$ git log

commit 021f89cfd5aa195cd841eb0e64a2d712cc1f8356

Author: Royce Jiang <[email protected]>

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

work: add file2

commit 8ed6d030278c0aef63052d6ba1946281c8a50ec8

Author: Royce Jiang <[email protected]>

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

work: add file1

commit 8a22ddfaaf374531dbdba02ef40bb10006057f6f

Author: Royce Jiang <[email protected]>

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,执行一些命令

这里以合并为例

[email protected]:~/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.

看一下,合并后的历史

[email protected]:~/workspace/git$ git log

commit a5d9008e0d87302ed1dc9c5fa8957294efc3e403

Author: Royce Jiang <[email protected]>

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

work: add file1

work: add file2

commit 8a22ddfaaf374531dbdba02ef40bb10006057f6f

Author: Royce Jiang <[email protected]>

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

init: first version.

生成补丁

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

[email protected]:~/workspace/git$ git format-patch -n1

0001-work-add-file1.patch

应用布丁

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

[email protected]:~/workspace/git$ cd ..

[email protected]:~/workspace$ cd git

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

[email protected]:~/workspace$ cd git_1

git_1/     git_1.git/

[email protected]:~/workspace$ cd git_1

[email protected]:~/workspace/git_1$ git am ../git/0001-work-add-file1.patch

Applying: work: add file1

applying to an empty history

获取远端仓库代码

添加远端

[email protected]:~/workspace/git_1$ git remote add git ../git

获取代码

[email protected]:~/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

查看所有分支

[email protected]:~/workspace/git_1$ git branch -a

* master

remotes/git/master

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

合并分支

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

[email protected]:~/workspace/git_1$ echo test2 > myfile2.txt

[email protected]:~/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")

[email protected]:~/workspace/git_1$ git commit -am "work: modfy file2"

[master 503a6c9] work: modfy file2

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

[email protected]:~/workspace/git_1$ git log

commit 503a6c922a6a0f0fc298c1b5b8289fd2babb435c

Author: Royce Jiang <[email protected]>

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

work: modfy file2

commit 24651eb534f5f4db86e61e62f48d7959aaaf4e65

Author: Royce Jiang <[email protected]>

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

work: add file1

work: add file2

合并分支

[email protected]:~/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.

处理冲突

首先查看冲突

[email protected]:~/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

修改冲突并提交

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

[email protected]:~/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

提交完成合并成功

[email protected]:~/workspace/git_1$ git log

commit d1feccc31ba37983e2d217ec7b497bffebdde3f1

Merge: 503a6c9 a5d9008

Author: Royce Jiang <[email protected]>

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

Merge remote-tracking branch ‘git/master‘

Conflicts:

myfile2.txt

commit 503a6c922a6a0f0fc298c1b5b8289fd2babb435c

Author: Royce Jiang <[email protected]>

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

work: modfy file2

commit 24651eb534f5f4db86e61e62f48d7959aaaf4e65

Author: Royce Jiang <[email protected]>

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

work: add file1

work: add file2

commit a5d9008e0d87302ed1dc9c5fa8957294efc3e403

Author: Royce Jiang <[email protected]>

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

work: add file1

work: add file2

commit 8a22ddfaaf374531dbdba02ef40bb10006057f6f

Author: Royce Jiang <[email protected]>

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

git 常用操作集锦,布布扣,bubuko.com

时间: 2024-10-27 12:39:22

git 常用操作集锦的相关文章

git报错:&#39;fatal:remote origin already exists&#39;怎么处理?附上git常用操作以及说明。

git添加远程库的时候有可能出现如下的错误, 怎么解决? 只要两步: 1.先删除 $ git remote rm origin 2.再次执行添加就可以了. ----------------------------------------------git常用操作------------------------------------------------ 说明,以下整理来自廖雪峰大神的<git教程>. 各位童鞋要下载git但是网速不给力的,可以从这里下载:https://pan.baidu.

Git工程开发实践(三)——Git常用操作

Git工程开发实践(三)--Git常用操作 一.Git仓库操作 1.Git仓库创建 git init在当前目录中初始化Git仓库git init [project-name]创建一个新目录并初始化仓库初始化git仓库会默认创建一个mater分支,创建名为.git的子目录,内含初始化Git仓库中所有的骨干文件,此时仓库中的文件还没有被跟踪.通过git add命令来实现对指定文件的跟踪,然后执行git commit提交. git add . git commit -m 'initial projec

一图学完GIT常用操作

git是目前最广泛使用的版本控制软件.虽然git功能齐全,完全掌握需要花费一番功夫,然而,对于普通的开发者而言,常用的操作及其有限,而不常用的操作,即使学习了,也会很快忘记.因此,我个人的观点是我们仅仅需要掌握自己常用的操作即可.下图是我对git常用操作的一个总结,现公布出来,以飨大家.另推荐一本git学习极好的书籍<git pro>:https://vdisk.weibo.com/s/ADfpX8eK88Km. 原文地址:https://www.cnblogs.com/doctor-li/p

Git 常用命令集锦

远程仓库相关命令 克隆远程仓库:git clone git://github.com/jquery/jquery.git 查看远程仓库:git remote -v 添加远程仓库:git remote add [name] [url] 删除远程仓库:git remote rm [name] 修改远程仓库:git remote set-url --push [remoteName] [newUrl] 拉取远程仓库:git pull [remoteName] [remoteBranchName]:[l

git常用操作笔记

这是我看了廖雪峰的git教程,写的笔记,仅作为一个学习的记录 一.大多数我们面临的是已经有一个进行中的项目了,我们只需克隆下来就可以了 1.安装git,安装完后,可输入git,回车,查看是否已安装 2.配置全局用户名     $git config --global user.name "Your Name" 配置邮箱地址        $git config --global user.email "[email protected]" 3.在一个空的文件夹里,右

git常用操作

git init 初始化 git status 查看git commit 文件的状态 git add <file>文件加入下一个commit git rm <fiile> 文件从下一个commit stage中删除,同时删除物理文件 git reset HEAD <file> 文件从下一个commit stage中删除,但不删除物理文件 git commit -m "test commit" 发射一个commit git commit --amend

git - 常用操作汇总

远程仓库相关命令 检出仓库:$ [email protected]:yaoxiabing/saltstack.git查看远程仓库:$ git remote -v添加远程仓库:$ git remote add [name] [url]删除远程仓库:$ git remote rm [name]修改远程仓库:$ git remote set-url --push [name] [newUrl]拉取远程仓库:$ git pull [remoteName] [localBranchName]推送远程仓库:

拾遗:Git 常用操作回顾

温故而知新,可以为师矣. Git 布局 工作区---->暂存区---->本地仓库---->远程仓库 Create Repository git init PATH git add PATH git commit -m CONTENTS Revoke HEAD 表示当近一次 commit 版本,HEAD^ 或 HEAD~1 表示上一次提交,HEAD^^ HEAD~2(波浪线) 表示上上次提交...依次类推:也可以用 [email protected]{N}  或 哈希值等形式直接定位(根据

git学习笔记03-本地git常用操作及原理-文件增删改

1.查看git状态 git status  这个可以告诉我们对git做了哪些操,比如增删改 2.既然我们修改了东西,有的时候想看看修改了什么,毕竟我们的记忆力并不如电脑 git diff 文件名 (默认和暂存区比较.啥事暂存区后面说) 3.修改了之后我提交了,我想看看提交的日志git log -- pretty = oneline  (后面这是格式化用的,可以不写) 4.突然想起来我提交的这个有错误怎么办,我想恢复到之前的版本 git reset --hard HEAD^ ( HEAD^ 是之前