Git详解

目录

前言

发展过程

集中式与分布式的区别

Debian/Linux安装git

配置git环境

创建本地空仓库

新建文件添加到本地仓库

改写提交

回滚代码仓库

工作区与缓存区

修改缓存区内容

将所有改动文件添加到缓存区

将文件撤销回到最近一次修改的状态

查看单个文件可回滚版本

查看提交历史

git 基本组成框架

github的使用

git 将远程仓库关联到本地和拉取指定分支、切换远程分支

 github提交本地仓库到远程仓库

git修改分支命令

git保存当前工作切换分支

将别的分支修改转移到自己的分支

git远程删除分之后本地git branch -a 依然看的到的问题

git 强制合并分支

git 拉取远程所有分支

git 子模块管理

git分支开发步骤

git 强制删除分支

git查看不同分支的文件差异

git查看仓库信息

git 查看日志简介方法

搭建本地git服务器

克隆远程仓库

git命令总结

1、仓库

2、配置

3、增加、删除文件

4、代码提交

5、分支

6、标签

7、查看信息

8、远程同步

9、撤销

10、 其他


前言

本篇文章的环境是在Linux环境下编写。

在日常工作中git少不了,所以编写本篇文章教大家如何使用git,便于日后工作与学习。

同时本篇文章也积累了很多博主在工作开发中包括自己日常开发中都用到的一些git技巧,在本文的最后整理了一份关于git的常用命令表。

git命令很多,但是常用的只有十多个。

发展过程

Git最初是由Linux开发者linus用了仅仅两周时间纯C语言编写而成,在编写完成之后就立马上手接管Linux源代码,不过在此之前Linux是由BitMover公司开发的BitKeeper分布式版本控制系统所管理源代码,它是商业收费的分布式版本控制器,但BitMover公司看中Linux开源精神,免费授权给Linux社区使用,在2002年时,Linux开始使用BitKeeper分布式版本控制系统管理源代码,但好景不长,有一天Linux社区成员Andrew(samba(局域网共享文件c/s程序)的作者)试图破解BitKeeper共享给所有人使用,被BitMover公司发现并收回了免费使用的权限,随后linus就用了两周时间开发了git,也就是目前位置最好用的分布式版本控制系统。

大名鼎鼎的github用的就是git系统来管理他们的网站,这里需要区分一下,github和git是两个东西,github是一个社区,git是一个服务系统,github只支持git分布式系统,所以名为github。

集中式与分布式的区别

除了git还有svn、cvs这样的版本控制系统,他们的区别在于一个是分布式一个是集中式。

集中式就是svn和csv这样的版本控制系统,分布式是git

区别在于集中式的版本控制系统每次在写代码时都需要从服务器中拉取一份下来,并且如果服务器丢失了,那么所有的就都丢失了,你本机客户端仅保存当前的版本信息,换句话说,集中式就是把代码放在一个服务器上集中管理,你的所有回滚等操作都需要服务器的支持。

分布式的区别在于,每个人的电脑都是服务器,当你从主仓库拉取一份代码下来后,你的电脑就是服务器,无须担心主仓库被删或者找不到的情况,你可以自由的本地回滚,提交,当你想把自己的代码提交到主仓库时,只需要合并推送待主仓库就可以了,同时你可以把自己的代码新建一份仓库分享给其他人。

像集中式他们都有一个主版本号,所有的版本迭代都以这个版本号为主,而分布式因为每个客户端都是服务器,git没有固定的版本号,但是有一个由哈希算法算出的id,用来回滚的,同时也有一个master仓库,这个仓库是一切分支仓库的主仓库,我们可以推送提交到master并合并到主仓库上,主仓库的版本号会迭代一次,我们客户端上的git版本号无论迭代多少次,都跟master无关,只有合并时,master才会迭代一次。

Debian/Linux安装git

# linux安装方式
# yum install -y git
# debian安装方式
sudo apt install git

# search搜索git
# sudo apt search git

配置git环境

# git config --glocal

# 参数讲解:
  config:用来配置git环境的
  --global:长命令表示配置整个git环境

# git config --global 
usage: git config [<options>]

Config file location
    --global              use global config file #全局配置
    --system              use system config file #系统配置
    --local               use repository config file #使用存储库配置文件
    --worktree            use per-worktree config file 
    -f, --file <file>     use given config file # 使用给定的配置文件
    --blob <blob-id>      read config from given blob object #从给定的blob对象读取配置

Action
    --get                 get value: name [value-regex] 
    --get-all             get all values: key [value-regex]
    --get-regexp          get values for regexp: name-regex [value-regex]
    --get-urlmatch        get value specific for the URL: section[.var] URL
    --replace-all         replace all matching variables: name value [value_regex]
    --add                 add a new variable: name value
    --unset               remove a variable: name [value-regex]
    --unset-all           remove all matches: name [value-regex]
    --rename-section      rename section: old-name new-name
    --remove-section      remove a section: name
    -l, --list            list all
    -e, --edit            open an editor
    --get-color           find the color configured: slot [default]
    --get-colorbool       find the color setting: slot [stdout-is-tty]

Type
    -t, --type <>         value is given this type
    --bool                value is "true" or "false"
    --int                 value is decimal number
    --bool-or-int         value is --bool or --int
    --path                value is a path (file or directory name)
    --expiry-date         value is an expiry date

Other
    -z, --null            terminate values with NUL byte
    --name-only           show variable names only
    --includes            respect include directives on lookup
    --show-origin         show origin of config (file, standard input, blob, command line)
    --show-scope          show scope of config (worktree, local, global, system, command)
    --default <value>     with --get, use default value when missing entry

初次使用git需要设置你的用户名以及邮箱,这将作为当前机器git的表示,如果你用他来下载远程仓库一些需要登录权限的仓库会要求登录,git默认使用配置邮箱以及用户名登入,但会要求你手动输入密码。

用户名及邮箱配置

# user代表用户,name代表配置用户的名称。
# git config --global user.name "hellogit"
# git config --global user.email "307555729@qq.com"

# git config --global user.
user.email   #配置用户的邮箱
user.name     #配置用户的名称      
user.signingKey      # 签名秘钥
user.useConfigOnly   # 仅使用配置

不配置也行,当遇到要求登录权限的远程仓库会让你在手动输入用户名、邮箱、以及密码。

创建本地空仓库

# git init

# init:初始化当前目录为仓库,初始化后会自动将当前仓库设置为master

创建本地仓库的条件是需要一个空目录,然后在空目录中初始化你的项目

如我想创建一个名为"gittest"的空项目

# 创建目录
# mkdir /gittest
# cd /gittest

# 使用git init 初始化当前仓库
# git init 
Initialized empty Git repository in /gittest/.git/
# ll -a 
total 0
drwxr-xr-x.  7 root root 119 Dec 21 13:37 .git
# cd .git/  # 这里可以看到在.git目录下生成了以下文件
# ls
HEAD  branches  config  description  hooks  info  objects  refs

新建文件添加到本地仓库

# git add 
# git commit -m 
add: 将文件添加到缓存区
commit:提交到本地仓库

用刚刚上一节所创建的空仓库gittest为例,我们用touch命令新建一个文件,名为test.c

# cd /gittest
# touch test.c

# 用git add 命令将文件添加到本地仓库的提交缓存
# git add test.c

这个时候还不算添加到本地仓库,我们还需要使用git commit命令为其添加修改的描述信息。

注意在使用git commit时我们只需要简单描述一下我们做了什么,不要像写注释那样写一大堆,不然将来在回滚代码或者查看历史版本时,很难审阅。

我们需要使用命令-m命令来简写描述我们的信息,如果不使用-m,会调用终端的注释编辑器让你输入描述信息,但是不建议使用,因为注释编辑器比较难用。

# git commit -m "add new file \"test.c\""
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test.c

git commit 会为我们生成40位的哈希值,用于版本ID,并把刚刚用git add 添加到提交缓存区里的文件提交到本地仓库中,便于我们回滚,至此,这个文件就已经添加到本地仓库中了,同时本地仓库也迭代了一个版本。

改写提交

# git commit --amend

# --amend: 重写上一次的提交信息

刚刚的例子里,我们提交了仓库,但是发现注释写错了,我们可以使用--amend长命令选项来改写提交。

# git commit --amend
#
# 输入以上命令后会进入编辑界面
add new file "test.c"

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Tue Dec 21 13:55:38 2021 +0800
#
# On branch master
#
# Initial commit
#
# Changes to be committed:
#       new file:   test.c
#

#######################################

查看历史提交日志

# git log
# 查看我们刚刚改写提交的,想要确定是否改写成功,我们使用git log看

commit 93c1a7665f20985ef4efc66bfd728916d7fc306d (HEAD -> master)
Author: hellogit <307555729@qq.com>
Date:   Tue Dec 21 13:55:38 2021 +0800

    add new file test.c

# 可以看到没有生成新的版本号,而是直接改写了刚刚提交的注释。

# 第一行的commit是哈希算法算出来的id,正如一开始所说的,分布式没有一个主版本号,他们都是用id来做标识的,同时用master作为主仓库,其他的分支怎么迭代都不会影响到master。
目前仓库就是master,因为我们没有拉取分支是直接用git init 创建的,就是master。
后面的head是指向的意思,表示这次提交到哪,head-> master代表这次提交到master主仓库,如果是head-> 分支仓库则代表提交到分支仓库
Author是提交者的用户名和邮箱,
date是提交时间,后面+0800这个是格林尼治时间,代表当前是以哪的时间为基准,这是世界时间,用他来作为基数与当前所在地时差进行计算,包括地球自转等公式。
最下面就是注释信息。

回滚代码仓库

# git reset --hard 
reset: 重置命令
--hard是重置代码仓库版本

有三种模式
--soft 、--mixed 、--hard 
# 使用--soft就仅仅将头指针恢复,已经add的暂存区以及工作空间的所有东西都不变。
# 使用--mixed,将头恢复,已经add的暂存区也会丢失,工作空间的代码是不变的。
# 使用--hard ,将一切全部恢复,头变,add的暂存区消失,代码也恢复到以前状态。

回滚到指定历史版本

# 先使用git log查看历史版本
# git log
# 在使用git reset --hard 命令回滚
# git reset --hard 要回滚id

#实例
# # git reset --hard 93c1a7665f20985ef4efc66bfd728916d7fc306d

回滚到当前仓库指向的版本

上面说过,HEAD是指向当前仓库的,历史版本中可能有别的分支,我们只想迭代我们仓库的上一个版本,只需要用HEAD来只想就可以了。

# git reset --hard HEAD^

# ^代表上一个版本的意思,HEAD代表当前仓库的指向,当前HEAD指向master,就代表回滚到master上一次提交的版本。

# 我们还可以使用另一种方式来回滚到当前仓库的指定版本

# git reset --hard HEAD~3

# 后面的~3代表以当前版本为基数,回滚多少次。HEAD~3代表回滚master前三个版本

# 如果觉得log打印内容过多,可以加--pretty=loeline选项简介输出。

查看提交之后文件是否做了改动

# 修改仓库文件
# echo test > test.c
# touch min.c 

#git status

status: 查看当前仓库状态

# git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   test.c    # 修改了文件
 Untracked files:
  (use "git add <file>..." to include in what will be committed)
        min.c               # 新增了一个文件

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

# 未修改
# git status
On branch master
nothing to commit, working tree clean

A:未修改
AM:修改
Untracked :未提交
modified: 修改文件,未提交

工作区与缓存区

在git下有一个概念是缓存区,这是其他集中式版本控制系统没有的。

工作区:工作区就是你当前的工作目录

缓存区:这里存放了你使用git add 命令提交的文件描述信息,他位于.git 目录下的index文件中

有的低版本中叫stage

这些文件中存储了我们一些提交的缓存数据,git会解析他们,HEAD文件就是指向当前的仓库

最后使用git commit提交时git会提交到当前仓库中,当前的工作区也就成为了最新一次提交的仓库版本。

修改缓存区内容

比如我们使用git add 添加了一个名为min.c 的文件,但是还没有提交的时候我们修改了它的内容,修改完成之后提交会发现内容并不是我们第二次修改的内容。

这就要说明一点,当我们使用git add 添加到缓存区的内容后,我们在修改这个文件时,它跟缓存区内容是没有任何关系的,我们使用git commit提交时,它只会提交缓存区内容。

如果想提交第二次修改,我们只需要在git add一次,然后再使用git commit提交就可以了,git会自动帮我们合并提交。

实例

# 将文件添加到缓存区中
# git add min.c

# 修改文件内容
# echo abcdtest > min.c

# 再次添加到缓存区
# git add min.c

# 提交
# git commit -m "add min.c"

将改动文件添加到缓存区

git add

平时可能写代码的时候不可能保证只改动了一个文件,我们改来改去最后都不知道自己改了哪些文件,为了保证所有文件都能被准确提交,我们可以使用git add我们确定修改的文件,当git add后在使用status查看一下状态,看看是否有遗漏没有提交的文件

# git status   
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   test.c

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

# git add test.c 
# git commit -m "add test.c"

[master baa81a9] add test.c
 1 file changed, 1 insertion(+)
# git log

# test.c没有提交,使用git add 将test.c添加进行就可以了

将所有改动文件添加到缓存区

# 将目录下包括子目录所有改动的文件提交到缓存区,注意只包括改动的文件。
# git add --all

# 这个命令还会把删除的文件也提交进去
# 如果你在本地删除了min.c文件,这个命令会把删除信息也记录进去,然后再提交的时候把仓库里对应的min.c也删除掉,也就是说你在本地做的删除操作也会被同步到仓库删除同样的文件。
# 如果不想删除文件,可以使用git add. ,注意后面有一个“.” 点符号,这个命令跟git add --all 一样,但不会记录删除操作。
最后提交到仓库,git commit

将文件撤销回到最近一次修改的状态

# git checkout -- file

checkout : 切换参数,通常用来切换分支仓库

# 当我们修改了一个文件,发现内容改的不对,需要重新修改,这个时候又不知道自己改了什么代码,想撤销修改,有一个最简单的方法,就是git checkout -- file,注意中间要有“--” ,checkout这个命令是切换分支的功能,加上“--” 可以用来将文件切换到最近一次的状态
# 注意这个恢复只能恢复到上一次提交的状态,如你刚提交了这个文件到仓库,随后你修改了他,那么使用这个命令只会回到刚刚提交后的那个状态,不能回到你还没有提交,但修改的状态。

# 实例

# echo bbbbbbbb >> test.c 

# cat test.c 
test
bbbbbbbb

# git checkout -- test.c

# cat test.c 
test

# 这个功能不能一直迭代恢复,如你恢复到了修改前的版本,想再次回滚到修改前的版本是不行的。

查看单个文件可回滚版本

当我们想回滚指定文件到指定版本时,需要查看该文件有多少个版本可以回滚时,可以使用git log filename命令

# 查看文件可回滚的版本
# git log test.c
commit baa81a99d4333ff072f5434515491a3b2ee2eb29 (HEAD -> master)
Author: hellogit <307555729@qq.com>
Date:   Tue Dec 21 14:52:21 2021 +0800

    add test.c

commit 93c1a7665f20985ef4efc66bfd728916d7fc306d
Author: hellogit <307555729@qq.com>
Date:   Tue Dec 21 13:55:38 2021 +0800

    add new file test.c


# git log min.c
commit 045a78bfd99df87b31e361f120f06c6e41ee1477
Author: hellogit <307555729@qq.com>
Date:   Tue Dec 21 14:49:44 2021 +0800

    add min.c
# 使用git reset命令将其回滚就可用了,命令如下:
# git reset 045a78bfd99df87b31e361f120f06c6e41ee1477 min.c

删除文件

git rm

如果我们使用普通的命令,rm删除文件,git状态会提示你删除了文件,你只需要使用add重新提交一次就可以了。

#  rm min.c -rf
# git status
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        deleted:    min.c
        modified:   test.c

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

# 使用git rm 删除文件,删除完之后使用git commit提交一次
# git rm min.c 
rm 'min.c'

# 看下状态
# git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        deleted:    min.c

# git commit -m "rm min.c"
[master d6fd9e6] rm min.c
 1 file changed, 1 deletion(-)
 delete mode 100644 min.c

查看提交历史

git reflog

git reflog 可以查看当前版本库的提交历史,凡是对仓库版本进行迭代的都会出现在这个里面,包括你回滚版本都会出现在这个历史中。

# git reflog 
d6fd9e6 (HEAD -> master) HEAD@{0}: commit: rm min.c
279f5c6 HEAD@{1}: commit: add min.c
93c1a76 HEAD@{2}: reset: moving to HEAD^
045a78b HEAD@{3}: reset: moving to 045a78bfd99df87b31e361f120f06c6e41ee1477
baa81a9 HEAD@{4}: commit: add test.c
045a78b HEAD@{5}: commit: add min.c
93c1a76 HEAD@{6}: reset: moving to HEAD~3
0d4cb76 HEAD@{7}: commit: si
46543e0 HEAD@{8}: reset: moving to HEAD^
bf48629 HEAD@{9}: commit: newapp
46543e0 HEAD@{10}: commit: testgit
8a5f468 HEAD@{11}: commit: hellogit
93c1a76 HEAD@{12}: reset: moving to 93c1a7665f20985ef4efc66bfd728916d7fc306d
93c1a76 HEAD@{13}: commit (amend): add new file test.c
fa9d29c HEAD@{14}: commit (amend): add new file test.c
1ff5dc9 HEAD@{15}: commit (initial): add new file "test.c"

git 基本组成框架

workspace :开发者工作区

index/stage :暂存区/缓存区

repository : 仓库区(本地仓库)

remote: 远程仓库

workspace:开发者工作区,也就是你当前写代码的目录,它一般保持的是最新仓库代码。

index/stage:缓存区,以前叫stage,新版改为index,位于.git目录中,它用来存放临时动作,比如我们做了git add或者git rm ,都是吧文件提交到缓存区,这是可以撤销的,然后在通过git commit将缓存区的内容提交到本地仓库。

repository:仓库区,是仓库代码,所有的提交都在这里,git会保存好每一个历史版本,存放在仓库区,它可以是服务端也可以是本地的,因为是分布式中,任何人都可以是主仓库。

remote:远程仓库,只能是别的电脑的仓库,即服务器仓库。

git rm 后恢复文件

git rm 

git reset 

git checkout

此方法仅限git rm ,因为git rm 会先将文件放入缓存区,且没有使用commit提交的情况下

实例

# git rm d.c
#
# 重置所有缓存区操作
# git reset 

# 重置完后使用git checkout命令将文件取消操作。
# git checkout d.c

# git rm d.c 
rm 'd.c'

# ls
test.c

# git reset 
Unstaged changes after reset:
D       d.c

# git checkout d.c
Updated 1 path from the index

# ls
d.c  test.c

如果已经提交了,恢复方法是把当前目录全部提交一次,这样做事为了防止我们等下回滚的时候导致一些修改的文件被替换掉,然后回滚到有那个文件的版本,将那个文件copy到别的目录,然后回滚到最新版本代码,再将文件copy回来,提交进去。

git创建分支

git branch

git checkout

# 使用git checkout -b 参数来创建一个分支,创建完成后会自动切换过去

# git checkout -b dev

# 然后我们在使用branch来查看当前属于哪个分支,也就是查看HEAD的指向

# git branch


# git checkout -b dev
Switched to a new branch 'dev'

# git branch
* dev
  master


# git checkout -b 等价于
git branch dev 
git checkout dev

git branch如果后面跟着命令则会创建分支,但不会切换
git checkout 后面如果是分支名称则切换过去。

git切换分支

git checkout

当我们想切换分支可以使用git checkout来切换,如果我们刚刚创建了一个分支dev并切换了过去,现在切换回master

# git checkout master
Switched to branch 'master'
# git branch 
  dev
* master

git checkout的作用是检出,如果是文件的话,会放弃对文件的缓存区操作,但是要使用reset重置一下变更才行。

如果是分支的话会切换过去

git合并分支

git merge

当我们新建分支并做完工作后,想要把分支提交至master,只需要切换到master仓库,执行git merge分支名就可以了

如我们在分支中新建了一个f.c和test.c的文件

然后在使用git checkout master切换到master

在使用git merge dev将其合并

# 创建修改文件
# git checkout dev 
# touch f.c
# touch test.cc

# add添加到缓存区
# git add f.c
# git add test.cc

# 提交到dev分支仓库
# git commit -m "add f.c test.cc"

# 切换到master分支
# git checkout master

# 合并dev分支到master
# git merge dev

如果你在任何分支下创建文件,没有提交到仓库,那么它在所有仓库都是可见的,比如你在分支dev中创建了一个文件,没有使用git add和git commit提交,此时你切换到master,这个文件依然存在,因为你创建的文件在工作目录中,你切换仓库时git只会更新跟仓库有关的文件,无关的文件依然存放在工作区。

查看历史版本有分支提交的历史

# git log --pretty=oneline 
77f91a759731becc52147678173a82453c909829 (HEAD -> master, dev) add f.c test.cc
0bfefa4a340e687499e633bb013a078c2dd15350 add d.c
d6fd9e6bf88ed04b82717fb3bf3f4c9cc977080c rm min.c
279f5c66c2177e69227accef59a1353fb359865e add min.c
93c1a7665f20985ef4efc66bfd728916d7fc306d add new file test.c

git分支的重要性

在开发中,都建议使用分支,因为在团队开发中,master只有一个,合作开发里任何人都可以从master里拉取代码,拉取master后创建分支,分支名改为你要做的操作,比如修改XX文件,修改XXbug,单词以下划线做分割,然后提交一个版本。

分支名必须简介,和标题一样,提交到commit在简单描述一下就可以了。

如我们的master中有个内存泄漏的bug。

创建一个分支名为Memory_leak,然后在commit里简单描述一下修复了哪个模块的内存泄漏,不要写修复了XX代码,XX问题导致的,只需要简单描述一下就可以了

一般情况下,拉取master后,想要修改功能或者添加功能,都是创建分支,在分支里修改不影响master,如果修改错了代码或者误删之类的,在从master上拉取一份就可以了。

github的使用

giahub是一款使用git命令作为基础框架的网站,他是一款开源分享网站,把你要开源的源代码放到github上,然后让人来start给你小小星星,小星星越多代表你的项目越具有影响力。

可以把项目分享到github上保存,github上是无限制代码的。

1、首先到github上注册一个账号

2、在本地创建一个ssh的key,因为github是使用ssh服务进行通信的。

-t : 秘钥类型、默认是rsa,可以省略

-C:添加 注释信息

-f: 指定秘钥文件存储文件名,一般默认,默认路径和默认文件名

# ssh-keygen -t rsa -C "yannzhenguo@gmail.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:hRCaYgP4Aum+7CKQZSeasSjBO/nPo7uPSkRwgJutUpY yannzhenguo@gmail.com
The key's randomart image is:
+---[RSA 3072]----+
|*o.   o.         |
|*o   o . .       |
|+*+.o   . .      |
|B+Eo.    .       |
|o/oo    S        |
|%=               |
|=.+              |
|+o .o.           |
|+o.=*+.          |
+----[SHA256]-----+

.ssh]# ll
total 12
-rw-------. 1 root root  394 Dec  6 23:02 authorized_keys
-rw-------. 1 root root 2610 Dec 21 16:44 id_rsa
-rw-r--r--. 1 root root  575 Dec 21 16:44 id_rsa.pub
# 生成的三个文件
id_rsa 是私钥、id_rsa.pub是公钥,id_rsa不能告诉任何人,只有公钥可以,ssh采用的是非对称加密。

在github上添加公钥

这样就添加成功了

 你可以添加很多个ssh,比如多台电脑,每个电脑都配置ssh然后添加进来就可以了,git需要这个是要确定你是主人,确定是主人的机器推送的才可以推送到仓库中,但是你可以创建公开仓库,别人只能拉取不能推送到这个仓库,你可以给其他人权限。

创建存储库 

 创建完之后

 只有一个readme文件

github将本地仓库关联到远程仓库

git remote add origin

 

# mkdir githubtest
# cd githubtest/
# echo "# test" >> README.md 
# git init 
# ll
# ll -a
# git add README.md 
# git commit -m "first commit"
# git log
# git branch -M main
# 本地仓库推送到远程上去,使用git remote add origin命令,origin是github上仓库命令。
# git remote add origin git@github.com:yannzhenguo/atest.git
# git push -u origin main 
The authenticity of host 'github.com (20.205.243.166)' can't be established.
ECDSA key fingerprint is SHA256:p2QAMXNIC1TJYWeIOttrVc98/R1BUFWu3/LiyKgUfQM.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com,20.205.243.166' (ECDSA) to the list of known hosts.
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 212 bytes | 212.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To github.com:yannzhenguo/test.git
 * [new branch]      main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.

push:将本地仓库推送到远程仓库合并

-u : 将本地仓库分支与远程仓库分支一起合并,就是说将master的分支也提交上去,这样你就可以在远程仓库看到你在本地仓库的master中创建了多少分支,不加这个参数只讲当前的master与远程的合并,没有分支的历史记录,也不能切换分支。

origin:远程仓库的意思,如果仓库是远程的必须加这个选项

master:提交本地master分支仓库

 

# cd /gittest    # 把master推送到github
# git remote add origin git@github.com:yannzhenguo/atest.git
# git push -u origin master 

看到文件了,github上已经默认是main作为主仓库了,master也代表主人,github已经替换默认master为main。

git 将远程仓库关联到本地和拉取指定分支、切换远程分支

git clone

当我们远程有仓库时,想要关联到本地只需要git clone就可以了

新建一个空目录,不要git init

使用git clone会自动帮我们初始化

mkdir /xxxtest
cd /xxxtest

# git clone  git@github.com:yannzhenguo/test.git
Cloning into 'test'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
[root@centos8-129 test]# tree 
.
`-- test
    `-- README.md

1 directory, 1 file

 我们上传你的代码在远程仓库有一个默认的main和master,由于我们最初上传的分支是master,所以github给我们创建了一个新的分支叫master,并没有关联到main中国,我们拉取时,默认拉取的是main分支

所有我们使用git clone -b 分支名 仓库地址来指定分支

# git clone -b master git@github.com:yannzhenguo/atest.git
Cloning into 'atest'...
remote: Enumerating objects: 14, done.
remote: Counting objects: 100% (14/14), done.
remote: Compressing objects: 100% (8/8), done.
Receiving objects: 100% (14/14), done.
Resolving deltas: 100% (1/1), done.
remote: Total 14 (delta 1), reused 11 (delta 1), pack-reused 0
[root@centos8-129 test]# tree 
.
`-- atest
    |-- d.c
    |-- f.c
    |-- test.c
    `-- test.cc

1 directory, 4 files

 github提交本地仓库到远程仓库

git add 

git commit

git push

我们修改master上的分支代码,然后使用git add 提交到缓存区,在使用commit提交到本地仓库,在使用push推送到远程就可以了。

# cd atest/
# ls
d.c  f.c  test.c  test.cc

# echo aaaa >> f.c 
# echo test.c >> test.c

# git status
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   f.c
        modified:   test.c

no changes added to commit (use "git add" and/or "git commit -a")
# git add f.c 
# git add test.c 

# git commit -m "test" 

[master ff2ba45] test
 2 files changed, 2 insertions(+)

# git push origin master 

Enumerating objects: 7, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 306 bytes | 306.00 KiB/s, done.
Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
To github.com:yannzhenguo/atest.git
   77f91a7..ff2ba45  master -> master

git修改分支命令

git branch

-m 选项修改分支名

# git branch -m master aa
# git log
commit ff2ba45b12a4bf0c8a7244b86ef42d995a11f8a8 (HEAD -> aa, origin/master)
Author: hellogit <307555729@qq.com>
Date:   Tue Dec 21 17:33:34 2021 +0800

git保存当前工作切换分支

git stash

在你当前工作区修改了文件或者其他功能时,你想要切换或者创建到其他分区是不可能的,如

# echo d.c > d.c 
# git checkout main
error: Your local changes to the following files would be overwritten by checkout:
        d.c
Please commit your changes or stash them before you switch branches.
Aborting

分支修改了内容,想要切换到其他分区git会终止你这样操作,为的是防止丢失当前工作区内容。

我们可以使用git stash命令来保存当前工作状态

git stash

保存工作状态之后可以使用git stash list查看当前存储了多少工作状态。

git stash list

这时就可以切换到其他分支了

# git stash 
Saved working directory and index state WIP on master: ff2ba45 test

# git stash list
stash@{0}: WIP on master: ff2ba45 test

# git checkout main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.

在别的分支做完事情后,在切换回刚刚的分支,然后在刚刚的分支中将状态恢复

git stash pop

# git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

# git stash pop
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   d.c

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (ee6e9855c01907e306ca588afc667f491351f058)

一般情况下,在修改代码时,突然来了一个新需求,让我们先去做这个需求,但是此时我们正在写的代码还没有完成,是不可以提交的,所以我们先使用git stash保存当前工作状态,在拉取一个分支去这个分支里面干活,干完活之后回到之前的分支,在将工作内容恢复出来继续干活。

# git stash pop 会将list保存的列表也给删除掉
# git stash apply 不会删除列表里的内容会默认恢复第一个
# 如果想恢复指定内容可以使用git stash apply list 名称
# git stash drop list 名称 可以移除指定list
# git stash clear 移除所有list
# git stash show 查看栈中最新保存的stash和当前目录的差异。
# 注意stash 是以栈的方式保存的,先进后出。
# 准确的说,这个命令的作用就是为了解决git不提交代码不能切换分支的问题。

将别的分支修改转移到自己的分支

git cherry-pick

有的时候我们从别的仓库拉取分支下来,是有bug的分支,但是master修复了,我们分支仓库没有修复,但是我们难不成重复master操作区修改这个bug? 太繁琐,我们直接使用cherry-pick命令将改动copy到我们分支上就可以了,这个只会将master改动代码合并到我们分支上,不会修改我们的代码。

git会检查master做了哪些修改,然后同步到我们的分支上,此时我们的分支依然是我们自己的代码,且会生成一个版本仓库。

做这个操作之前建议提交一次,便于恢复。

使用git cherry-pick分支名即可合并分支修改,再此之前要保证你仓库代码是提交的,才可以进行这个步骤。

其次可以使用git log查看commit然后git cherry-pick也是可以的,合并分支的指定历史版本。

git远程删除分之后本地git branch -a 依然看的到的问题

git remote

这个问题是因为本地没有更新分支缓存

可以使用remote命令对远程仓库进行操作

使用git remote show origin命令查看远程仓库信息

[root@centos8-129 atest]# git remote show  origin 
* remote origin
  Fetch URL: git@github.com:XXXXguo/atest.git
  Push  URL: git@github.com:XXXXguo/atest.git
  HEAD branch: main
  Remote branches:
    main   tracked
    master tracked
  Local branches configured for 'git pull':
    main   merges with remote main
    master merges with remote master
  Local refs configured for 'git push':
    main   pushes to main   (up to date)
    master pushes to master (up to date)

如果里面没有看到删除后的分支就代表这个分支远程已经被删除了

使用prune参数刷新本地分支仓库

# git  remote prune origin 

git 强制合并分支

--allow-unrelated-histories

当我们在使用两个不同的分支时或此分支不是从原生仓库中分支出来的,想要合并不符合git规则,会弹出错误:fatal: refusing to merge unrelated histories ,比如我们在当地开发好了,但是并没有在一开始关联远程仓库,若想提交就会出现这样的错误,我们先拉取下来,合并分支命令后面加上这条语句

# git merge master --allow-unrelated-histories

git 拉取远程所有分支

git fetch

当我们在本地进行开发时,有时会发现一些分支看不到

可以使用git fetch 把远程全部分支拉取下来,同时也包括这些分支的仓库版本,log日志等,这些操作不会进行合并。

也可以拉取指定分支的最新内容

# git remote show origin  #使用git remote show origin命令查看远程仓库信息

# git fetch

# 拉取指定分支的最新内容
# git fetch XXXX

git 子模块管理

git submodule

当我们项目较大的情况下,都会使用模块化编程,把不同的业务功能分割成数个子模块,git也拥有对子模块进行管理的方法,submodule,可以使用它来添加子模块与管理子模块。

如添加一个子模块:

# git submodule add http://ip:port/scm/app/lib.git

使用如上命令会添加一个子模块,名为lib的子模块到自己的仓库中,这样我们就可以使用了

在添加时会添加仓库的最新版本,但是此模块不会自动更新,需要我们手动更新,当子模块的仓库进行了更新,我们需要进到此字模块文件夹中执行如下命令。

# git submodule update

这样就会自动化更新了模块到最新版本。

若我们想要使用指定版本的子模块也可以使用切换版本命令来完成子模块的版本切换,同时你也可以使用git log命令查看这个子模块的版本提交。

注意根据git版本不同的原因,你在添加时可能下来的子模块是空项目,这个时候可以使用如下命令:

首先进入到克隆下来的子模块目录,并执行如下命令:

# git submodule init
# git submodule update

最新的git在使用add添加子模块时会自动使用这两个命令。

这两个命令作用分别是初始化子模块仓库,更新远程子模块仓库到本地,最早的git添加子模块只是先在本地生成了映射关系,需要手动执行这两个命令。

最后别忘记使用git add与git commit提交一次。

在提醒一下,若你使用的子模块版本是0.17,最新版本是2.13,当别人拉取你仓库时子模块也会是0.17不会变动。


git分支开发步骤

一般情况下我们开发都要在不扰乱master代码的情况下进行开发的。

1.拉取分支,分支名简明摘要说要干什么,然后干活,在合并到master,合并之后在删除分支,这是基本步骤,不需要留额外分支,分支只是为了将来看代码时方便而已,能看到这个分支是干什么的。

git 强制删除分支

git branch

遇到无法删除的分支可以使用git branch -D 大写的D来强制删除分支。

git查看不同分支的文件差异

git diff

# git diff master main test.c
diff --git a/test.c b/test.c
deleted file mode 100644
index 609859b..0000000
--- a/test.c
+++ /dev/null
@@ -1 +0,0 @@
-test.c

git查看仓库信息

git remote

使用git remote 可以查看当前仓库名称

git remote

使用remote -v可以查看更详细的权限信息

# git remote
origin
# git remote -v
origin  git@github.com:XXXXguo/atest.git (fetch)
origin  git@github.com:XXXXguo/atest.git (push)

# fetch代表可以拉取仓库,push代表可以推送。

# 如果没有权限只能拉取的情况下不会显示push。

git 查看日志简介方法

git log

# git log --graph --pretty=oneline --abbrev-commit 
* ff2ba45 (HEAD -> master, origin/master) test
* 77f91a7 add f.c test.cc
* 0bfefa4 add d.c
* d6fd9e6 rm min.c
* 279f5c6 add min.c
* 93c1a76 add new file test.c

--abbrev-commit #id显示缩写

# git log  --pretty=oneline --abbrev-commit 
ff2ba45 (HEAD -> master, origin/master) test
77f91a7 add f.c test.cc
0bfefa4 add d.c
d6fd9e6 rm min.c
279f5c6 add min.c
93c1a76 add new file test.c


[root@centos8-129 atest]# git log  --pretty=oneline 
ff2ba45b12a4bf0c8a7244b86ef42d995a11f8a8 (HEAD -> master, origin/master) test
77f91a759731becc52147678173a82453c909829 add f.c test.cc
0bfefa4a340e687499e633bb013a078c2dd15350 add d.c
d6fd9e6bf88ed04b82717fb3bf3f4c9cc977080c rm min.c
279f5c66c2177e69227accef59a1353fb359865e add min.c
93c1a7665f20985ef4efc66bfd728916d7fc306d add new file test.c

注意前面的字符串是有效的,你可以用它来切换版本或者合并分支。

搭建本地git服务器

# 安装git
# yum install -y git

# 创建一个账户用来专门管理git服务。
# useradd gitadmin

# 创建了一个名为gitadmin账户
# su gitadmin
# 切换到这个账户下

# 生成公钥私钥对
# ssh-keygen        # ssh-keygen -t rsa -C "yzg@gmail.com"

# 在.ssh目录里
# ls
authorized_keys  id_rsa  id_rsa.pub 

# authorized_keys文件时用来存放别人的公钥的,就像上面配置github一样,你把别人机器上的ssh key的公钥输入到这个文件中,这样git才会开启权限免密登录,让其拥有推送以及拉取的权限。如果文件没有需要手工创建一下

# 把公钥文件分发给需要连接git服务器的团队成员。

# 创建一个名为app的仓库,通常仓库都是.git结尾的
# git init --bare app.git

$ tree app.git
app.git
|-- HEAD
|-- branches
|-- config
|-- description
|-- hooks
|   |-- applypatch-msg.sample
|   |-- commit-msg.sample
|   |-- fsmonitor-watchman.sample
|   |-- post-update.sample
|   |-- pre-applypatch.sample
|   |-- pre-commit.sample
|   |-- pre-merge-commit.sample
|   |-- pre-push.sample
|   |-- pre-rebase.sample
|   |-- pre-receive.sample
|   |-- prepare-commit-msg.sample
|   `-- update.sample
|-- info
|   `-- exclude
|-- objects
|   |-- info
|   `-- pack
`-- refs
    |-- heads
    `-- tags

9 directories, 16 files


# 修改app.git文件的属主和属组
# chown -R gitadmin:gitadmin app.git

# 为了安全考虑我们需要禁用ssh登录到我们的shell,修改/etc/passwd 文件git账户的shell类型为/usr/bin/git-shell

# git-shell 是git提供的shell程序。


克隆远程仓库

git clone git@ip:app.git

# git clone gitadmin@192.168.245.129:app.git
正克隆到 'app'...
gitadmin@192.168.245.129's password: 
warning: 您似乎克隆了一个空版本库。

git命令总结

1、仓库

# 在当前目录新建一个Git代码库
$ git init

#新建一个目录,将其初始化为Git代码库
$ git init [project-name]

# 下载一个项目和它的整个代码历史
$ git clone [url]

2、配置

# 显示当前的Git配置
$ git config --list

# 编辑Git配置文件
$ git config -e [--global]

# 设置提交代码时的用户信息
$ git config [--global] user.name "[name]"
$ git config [--global] user.email "[email address]"

3、增加、删除文件

# 添加指定文件到暂存区
$ git add [file1] [file2] ...

# 添加指定目录到暂存区,包括子目录
$ git add [dir]

# 添加当前目录的所有文件到暂存区
$ git add .

# 添加每个变化前,都会要求确认
# 对于同一个文件的多处变化,可以实现分次提交
$ git add -p

# 删除工作区文件,并且将这次删除放入暂存区
$ git rm [file1] [file2] ...

# 停止追踪指定文件,但该文件会保留在工作区
$ git rm --cached [file]

# 改名文件,并且将这个改名放入暂存区
$ git mv [file-original] [file-renamed]

4、代码提交

# 提交暂存区到仓库区
$ git commit -m [message]

# 提交暂存区的指定文件到仓库区
$ git commit [file1] [file2] ... -m [message]

# 提交工作区自上次commit之后的变化,直接到仓库区
$ git commit -a

# 提交时显示所有diff信息
$ git commit -v

# 使用一次新的commit,替代上一次提交
# 如果代码没有任何新变化,则用来改写上一次commit的提交信息
$ git commit --amend -m [message]

# 重做上一次commit,并包括指定文件的新变化
$ git commit --amend [file1] [file2] ...

5、分支

# 列出所有本地分支
$ git branch

# 列出所有远程分支
$ git branch -r

# 列出所有本地分支和远程分支
$ git branch -a

# 新建一个分支,但依然停留在当前分支
$ git branch [branch-name]

# 新建一个分支,并切换到该分支
$ git checkout -b [branch]

# 新建一个分支,指向指定commit
$ git branch [branch] [commit]

# 新建一个分支,与指定的远程分支建立追踪关系
$ git branch --track [branch] [remote-branch]

# 切换到指定分支,并更新工作区
$ git checkout [branch-name]

# 切换到上一个分支
$ git checkout -

# 建立追踪关系,在现有分支与指定的远程分支之间
$ git branch --set-upstream [branch] [remote-branch]

# 合并指定分支到当前分支
$ git merge [branch]

# 选择一个commit,合并进当前分支
$ git cherry-pick [commit]

# 将新分支推送到远程仓库中
$ git push origin [branch name]

# 删除分支
$ git branch -d [branch-name]

# 删除远程分支
$ git push origin --delete [branch-name]
$ git branch -dr [remote/branch]

6、标签

# 列出所有tag
$ git tag

# 新建一个tag在当前commit
$ git tag [tag]

# 新建一个tag在指定commit
$ git tag [tag] [commit]

# 删除本地tag
$ git tag -d [tag]

# 删除远程tag
$ git push origin :refs/tags/[tagName]

# 查看tag信息
$ git show [tag]

# 提交指定tag
$ git push [remote] [tag]

# 提交所有tag
$ git push [remote] --tags

# 新建一个分支,指向某个tag
$ git checkout -b [branch] [tag]

7、查看信息

# 显示有变更的文件
$ git status

# 显示当前分支的版本历史
$ git log

# 显示commit历史,以及每次commit发生变更的文件
$ git log --stat

# 搜索提交历史,根据关键词
$ git log -S [keyword]

# 显示某个commit之后的所有变动,每个commit占据一行
$ git log [tag] HEAD --pretty=format:%s

# 显示某个commit之后的所有变动,其"提交说明"必须符合搜索条件
$ git log [tag] HEAD --grep feature

# 显示某个文件的版本历史,包括文件改名
$ git log --follow [file]
$ git whatchanged [file]

# 显示指定文件相关的每一次diff
$ git log -p [file]

# 显示过去5次提交
$ git log -5 --pretty --oneline

# 显示所有提交过的用户,按提交次数排序
$ git shortlog -sn

# 显示指定文件是什么人在什么时间修改过
$ git blame [file]

# 显示暂存区和工作区的差异
$ git diff

# 显示暂存区和上一个commit的差异
$ git diff --cached [file]

# 显示工作区与当前分支最新commit之间的差异
$ git diff HEAD

# 显示两次提交之间的差异
$ git diff [first-branch]...[second-branch]

# 显示今天你写了多少行代码
$ git diff --shortstat "@{0 day ago}"

# 显示某次提交的元数据和内容变化
$ git show [commit]

# 显示某次提交发生变化的文件
$ git show --name-only [commit]

# 显示某次提交时,某个文件的内容
$ git show [commit]:[filename]

# 显示当前分支的最近几次提交
$ git reflog

8、远程同步

# 下载远程仓库的所有变动
$ git fetch [remote]

# 显示所有远程仓库
$ git remote -v

# 显示某个远程仓库的信息
$ git remote show [remote]

# 增加一个新的远程仓库,并命名
$ git remote add [shortname] [url]

# 取回远程仓库的变化,并与本地分支合并
$ git pull [remote] [branch]

# 上传本地指定分支到远程仓库
$ git push [remote] [branch]

# 强行推送当前分支到远程仓库,即使有冲突
$ git push [remote] --force

# 推送所有分支到远程仓库
$ git push [remote] --all

9、撤销

# 恢复暂存区的指定文件到工作区
$ git checkout [file]

# 恢复某个commit的指定文件到暂存区和工作区
$ git checkout [commit] [file]

# 恢复暂存区的所有文件到工作区
$ git checkout .

# 重置暂存区的指定文件,与上一次commit保持一致,但工作区不变
$ git reset [file]

# 重置暂存区与工作区,与上一次commit保持一致
$ git reset --hard

# 重置当前分支的指针为指定commit,同时重置暂存区,但工作区不变
$ git reset [commit]

# 回退到上一版
$ git reset --hard HEAD^

# 重置当前分支的HEAD为指定commit,同时重置暂存区和工作区,与指定commit一致
$ git reset --hard [commit]

# 重置当前HEAD为指定commit,但保持暂存区和工作区不变
$ git reset --keep [commit]

# 新建一个commit,用来撤销指定commit
# 后者的所有变化都将被前者抵消,并且应用到当前分支
$ git revert [commit]

暂时将未提交的变化移除,稍后再移入
$ git stash
$ git stash pop

10、 其他

# 生成一个可供发布的压缩包
$ git archive

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

皆往矣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值