linux下git安装 使用

参考git中文网
参考

安装

在 Ubuntu (我的是ubuntu,其他请参考文章开头的链接)这类 Debian 体系的系统上,可以用 apt-get 安装:

sudo apt-get install git

初步配置

用户信息
第一个要配置的是你个人的用户名称和电子邮件地址。这两条配置很重要,每次 Git 提交时都会引用这两条信息,说明是谁提交了更新,所以会随更新内容一起被永久纳入历史记录:

$ git config --global user.name "John Doe"
    $ git config --global user.email johndoe@example.com

如果用了 --global 选项,那么更改的配置文件就是位于你用户主目录下的那个,以后你所有的项目都会默认使用这里配置的用户信息。如果要在某个特定的项目中使用其他名字或者电邮,只要去掉 --global 选项重新配置即可,新的设定保存在当前项目的 .git/config 文件里。

文本编辑器
接下来要设置的是默认使用的文本编辑器。Git 需要你输入一些额外消息的时候,会自动调用一个外部文本编辑器给你用。默认会使用操作系统指定的默认编辑器,一般可能会是 Vi 或者 Vim。如果你有其他偏好,比如 Emacs 的话,可以重新设置:

$ git config --global core.editor emacs

差异分析工具
还有一个比较常用的是,在解决合并冲突时使用哪种差异分析工具。比如要改用 vimdiff 的话:

$ git config --global merge.tool vimdiff

Git 可以理解 kdiff3,tkdiff,meld,xxdiff,emerge,vimdiff,gvimdiff,ecmerge,和 opendiff 等合并工具的输出信息。当然,你也可以指定使用自己开发的工具,具体怎么做可以参阅第七章。

查看配置信息
要检查已有的配置信息,可以使用 git config --list 命令:

$ git config --list
    user.name=Scott Chacon
    user.email=schacon@gmail.com
    color.status=auto
    color.branch=auto
    color.interactive=auto
    color.diff=auto

有时候会看到重复的变量名,那就说明它们来自不同的配置文件(比如 /etc/gitconfig 和 ~/.gitconfig),不过最终 Git 实际采用的是最后一个。

也可以直接查阅某个环境变量的设定,只要把特定的名字跟在后面即可,像这样:

$ git config user.name
    Scott Chacon

想了解 Git 的各式工具该怎么用,可以阅读它们的使用帮助,方法有三:

$ git help <verb>
    $ git <verb> --help
    $ man git-<verb>

比如,要学习 config 命令可以怎么用,运行:

$ git help config

我们随时都可以浏览这些帮助信息而无需连网。

基本操作

创建版本库

$ git clone                      #克隆远程版本库
$ git init                       #初始化本地版本库

修改&提交

$ git status                     #查看状态
$ git diff                       #查看变更内容
$ git add .                      #跟踪所有改动过的文件
$ git add                        #跟踪指定的文件
$ git mv                         #文件改名
$ git rm                         #删除文件
$ git rm --cached                #停止跟踪文件但不删除
$ git commit -m "commit message" #提交所有更新过的文件
$ git commit --amend             #修改最后一次提交

查看提交历史

$ git log                        #查看提交历史
$ git log -p                     #查看指定文件的提交历史
$ git blame                      #以列表方式查看指定文件的提交历史

撤消

$ git reset --hard HEAD          #撤消工作目录中所有未提交文件的修改内容
$ git checkout HEAD              #撤消指定的未提交文件的修改内容
$ git revert                     #撤消指定的提交

分支与标签

$ git branch                     #显示所有本地分支
$ git checkout                   #切换到指定分支或标签
$ git branch                     #创建新分支
$ git branch -d                  #删除本地分支
$ git tag                        #列出所有本地标签
$ git tag                        #基于最新提交创建标签
$ git tag -d                     #删除标签

合并与衍合

$ git merge                      #合并指定分支到当前分支
$ git rebase                     #衍合指定分支到当前分支

远程操作

$ git remote -v                  #查看远程版本库信息
$ git remote show                #查看指定远程版本库信息
$ git remote add                 #添加远程版本库
$ git fetch                      #从远程库获取代码
$ git pull                       #下载代码及快速合并
$ git push                       #上传代码及快速合并
$ git push                       #删除远程分支或标签
$ git push --tags                #上传所有标签

操作例子

git到底是什么自己查,文章一大堆
在这里插入图片描述

  1. 初始化本地仓库

     初始化一个 Git 仓库,步骤如下:
     $ mkdir ~/myrepo
         $ cd myrepo
    
         $ git init      # 在 ~/myrepo 目录下初始化一个 Git 仓库
    
             Initialized empty Git repository in /home/xxx/myrepo/.git/
    
         $ ls -la .git
    
             total 12
             drwxrwxr-x. 7 tkuang tkuang 119 Nov  8 07:54 .
             drwxrwxr-x. 3 tkuang tkuang  18 Nov  8 07:54 ..
             drwxrwxr-x. 2 tkuang tkuang   6 Nov  8 07:54 branches
             -rw-rw-r--. 1 tkuang tkuang  92 Nov  8 07:54 config
             -rw-rw-r--. 1 tkuang tkuang  73 Nov  8 07:54 description
             -rw-rw-r--. 1 tkuang tkuang  23 Nov  8 07:54 HEAD
             drwxrwxr-x. 2 tkuang tkuang 242 Nov  8 07:54 hooks
             drwxrwxr-x. 2 tkuang tkuang  21 Nov  8 07:54 info
             drwxrwxr-x. 4 tkuang tkuang  30 Nov  8 07:54 objects
             drwxrwxr-x. 4 tkuang tkuang  31 Nov  8 07:54 refs
    

    执行 git init 命令后,Git 仓库会生成一个 .git 目录,该目录包含了资源的所有元数据。
    此时当前目录下多了一个 .git 的目录,这个目录是Git用来跟踪管理版本库的,没事千万不要手动修改这个目录里面的文件,不然改乱了,就把Git仓库给破坏了。
    【注意】如果你没有看到.git目录,那是因为这个目录默认是隐藏的,用 ls -ah命令就可以看见。也不一定必须在空目录下创建Git仓库,选择一个已经有东西的目录也是可以的。

  2. 添加文件到仓库暂存区
    在 ~/myrepo 目录下,创建 test_1.txt 和 test_2.txt 文件,步骤如下:

         $ cd ~/myrepo
    
         $ cat > test_1.txt << EOF
         test_1.txt for Git Repo.
         EOF
         
         $ cat > test_2.txt << EOF
         test_2.txt for Git Repo.
         EOF
    
    
     查看 ~/myrepo 目录下 Git 仓库的状态,命令如下:
    
    
         $ git status ./     
    
             # On branch master
             #
             # Initial commit
             #
             # Untracked files:
             #   (use "git add <file>..." to include in what will be committed)
             #
             #       test_1.txt
             #       test_2.txt
             nothing added to commit but untracked files present (use "git add" to track)
    
         注:branch master 表示当前在仓库的 master 分支上,Untracked files 表示未被仓库监控的文件(新增文件),要先用 git add 加入仓库暂存区。
    
             不需要的未被仓库监控的文件,可以运行 git clean -f 命令来清除,包含目录的用 git clean fd 命令。
    
     添加 test_1.txt 到仓库暂存区:
    
    
         $ git add test_1.txt
         $ git status ./
    
             # On branch master
             #
             # Initial commit
             #
             # Changes to be committed:
             #   (use "git rm --cached <file>..." to unstage)
             #
             #       new file:   test_1.txt
             #
             # Untracked files:
             #   (use "git add <file>..." to include in what will be committed)
             #
             #       test_2.txt
    
         注:test_1.txt 已在仓库暂存区,test_2.txt 依然是未被仓库监控的文件。可以运行 git rm --cached test_1.txt 命令把 test_1.txt 从暂存区移除。
    
  3. 提交暂存区的文件到仓库

     $ git commit -m "Add test_1.txt to myrepo"
    
         [master (root-commit) 38da491] Add test_1.txt to myrepo
         1 file changed, 1 insertion(+)
         create mode 100644 test_1.txt
    
     $ git log       # 查看 commit 信息,可以运行 git log --name-only 显示文件名
    
         commit 38da49184fc15ef28d9d3273c4f8dcfc3b0f550c
         Author: xxx <xxx@123.com>
         Date:   Tue Nov 8 08:36:53 2022 -0500
    
             Add test_1.txt to myrepo
    
  4. HTTPS 方式连接 Gitee

    1. 上传到远程仓库

      Git 远程仓库可以自己搭建,也可以使用 Git 托管平台,比如 GitHub、GitLab、Gitee 等。GitHub 是全球最流行的开源 Git 托管平台,开源项目较多,但是国内访问速度慢。Gitee 是一个国内 Git 代码托管平台,访问速度快。

      本文以 Gitee 为例,演示如何把 myrepo 仓库上传到 Gitee。

      (1) 注册 Gitee 账号

       访问 https://gitee.com/login,Gitee 支持 GitHub 账号、GitLab 账号、微信账号等多种账号登陆,也可以访问 https://gitee.com/signup 注册一个新的 Gitee 账号。
      
       假设注册的账号名为 xxx,本地仓库是 myrepo,则远程仓库地址如下:
      
           HTTPS 协议仓库地址:https://gitee.com/xxx/myrepo.git
           SSH 协议仓库地址:git@gitee.com/xxx/myrepo.git
      

      (2) 上传到 Gitee

       $ cd ~/myrepo
           
       $ git remote add origin https://gitee.com/xxx/myrepo.git
       $ git push -u origin master  # origin 是远程主机,master 表示是远程服务器上的 master分支
      
           Username for 'https://gitee.com': xxx
           Password for 'https://xxx@gitee.com':
           fatal: repository 'https://gitee.com/xxx/myrepo.git/' not found    
      
           注:需要在 Gitee 上创建一个空仓库 myrepo,再运行 git push -u origin master 命令。
      
               xxx 是 Gitee 账号名,下同。
      
               git push 带上 -u 参数,记录了 push 到远端分支的默认值,这样下次上传直接运行 git push 即可。
      
       # 在 Gitee 上新创建空仓库 myrepo 后,再运行如下命令
       $ git push -u origin master    
      
           Username for 'https://gitee.com': xxx
           Password for 'https://xxx@gitee.com':
           Counting objects: 3, done.
           Writing objects: 100% (3/3), 242 bytes | 0 bytes/s, done.
           Total 3 (delta 0), reused 0 (delta 0)
           remote: Powered by GITEE.COM [GNK-6.4]
           To https://gitee.com/xxx/myrepo.git
           * [new branch]      master -> master
           Branch master set up to track remote branch master from origin.
      
           注:在 Gitee 上看到 test_1.txt 文件上传成功。
      
    2. Clone 远程仓库

      使用 git clone 从现有 Git 仓库中拷贝项目,命令格式:

       git clone <repo>
      
       git clone <repo> <directory>
      

      参数说明:

       repo: Git 仓库。
       directory: 本地目录。
      

      在本地仓库 myrepo 所在主机的其它目录,Clone 远程仓库:

       $ cd ~/temp
      
       $ git clone https://gitee.com/xxx/myrepo.git
      
           Cloning into 'myrepo'...
           Username for 'https://gitee.com': xxx
           Password for 'https://xxx@gitee.com':
           remote: Enumerating objects: 3, done.
           remote: Counting objects: 100% (3/3), done.
           remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
           Unpacking objects: 100% (3/3), done.
      
       $ git clone https://gitee.com/xxx/myrepo.git myrepo_2
      
           Cloning into 'myrepo_2'...
           Username for 'https://gitee.com': xxx
           Password for 'https://xxx@gitee.com':
           remote: Enumerating objects: 3, done.
           remote: Counting objects: 100% (3/3), done.
           remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
           Unpacking objects: 100% (3/3), done.
      
       $ ls
      
           myrepo  myrepo_2
      
    3. 保存账号名和密码

      上文在一台主机上每次 Clone 都需要输入 Gitee 账号名和密码,而且每次操作远程仓库时,都需要输入账号名和密码,比如:git pull 操作。

       $ cd ~/temp/myrepo
       $ git pull      # 远程仓库同步到本地仓库
      
           Username for 'https://gitee.com': xxx
           Password for 'https://xxx@gitee.com':
           Already up-to-date.
      

      很显然,我们需要在一次正确输入账号名和密码后,把账号名和密码保存起来,提高工作效率。

      (1) 保存账号名和密码,当前仓库范围使用

       在一次正确输入账号名和密码后,运行如下命令。
      
      
       $ git config credential.helper store    # 保存到 ~/.git-credentials,修改当前仓库 .git/config
       $ git pull
       
           Already up-to-date.
      
           注:这次操作无需输入账号和密码。
      
       $ cd ~/temp/myrepo_2        # 进入 ~/temp/myrepo_2 仓库        
       $ git pull
      
           Username for 'https://gitee.com': xxx
           Password for 'https://xxx@gitee.com':
           Already up-to-date.
      
           注:保存的密码仅在 ~/temp/myrepo 仓库范围有效,所以访问 ~/temp/myrepo_2 仓库,仍然需要输入账号名和密码。
      

      (2) 保存账号名和密码,当前 Linux 用户范围使用

       在一次正确输入账号名和密码后,运行如下命令。
      
       $ git config --global credential.helper store   # 保存到 ~/.git-credentials,修改 ~/.gitconfig
      
       $ cd ~/myrepo
       $ git pull      # 这次操作无需输入账号和密码
      
           Already up-to-date.
      

      注:把账号名和密码明文保存到 ~/.git-credentials 存在很大的安全隐患。对于安全要求环境,这种方式不太适合,应该使用 SSH 方式。

  5. SSH 方式连接 Gitee

    1. 创建本地 SSH Key

      在本地仓库所在主机上,生成 SSH Key

      $ ssh-keygen -t rsa -C “xxx@123.com”

       Enter file in which to save the key (~/.ssh/id_rsa): gitee_rsa
       Enter passphrase (empty for no passphrase):
       Your identification has been saved in gitee_rsa.
       Your public key has been saved in gitee_rsa.pub.
       The key fingerprint is:
       SHA256:PRNU7CjaMdpjIxE8WLlQT5WUAqVuHB8eZ2falHs80zw xxx@123.com
       The key's randomart image is:
       +---[RSA 2048]----+
       |     =++oo+=.    |
       |    o =+..o ..   |
       |     .o++.+o=    |
       |     oo++*.B.o o |
       |      +*S+= o =Eo|
       |     .+ *  o . o.|
       |       o o       |
       |                 |
       |                 |
       +----[SHA256]-----+
      
       注:电子邮件 xxx@123.com 用于生成 Key,不需要与 Gitee 注册的电子邮件保持一致。passphrase 是 Key 的密码,不是账号密码,尽量不要用账号密码,也可以直接回车,不使用 Key 密码。
      

      启动 ssh-agent

      $ eval “$(ssh-agent -s)”

      将 Private Key 添加到 ssh-agent

      $ ssh-add ~/.ssh/gitee_rsa

      显示 Public Key

      $ cat ~/.ssh/gitee_rsa.pub

       ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC/mkkAYFKwOEEfarqWBivHtyAh+zYaQtw4dyBhmQ/tTUKqQ81NWfB9qbmYwKigYUHEYXmD961g2O+J34UzZ3v/J2hY6hDn++k3mC1XHG1m1/g5FIRbF7ls7+MeW2DbuZk8DMfZpNkMaHHW6IUsyrC1sbpBAHZb/hWhsfWAsbMKU2wF0EMU3Toddr62hLQ9K49KO3YgFWRMl9qs6uhafEFMksJROoZRoihMCIFqo8ro+bVD8PGHH+WCNZT35VPGzFoQpucQheWErXLCoC6xiLbGVO9wgh/LdU58wPjy+P7CyOtinJ4IDoGw+zVNWGzNv4t/Uh0nuvz1mU4f2vosy+31 xxx@123.com
      
    2. 在 Gitee 上添加 SSH 公钥

      Gitee 后台右上下拉菜单 -> 设置 -> 页面左侧菜单 (安全设置) -> SSH公钥页面-> 添加公钥

           标题: xxx@123.com public key
      
           公钥: gitee_rsa.pub 里的内容
      
       -> 点击 “确定” 按钮
      

      $ ssh -T git@gitee.com

       Hi xxx! You've successfully authenticated, but GITEE.COM does not provide shell access.
      
       注:表示 ssh 连接成功。
      
    3. 操作远程仓库

      $ cd ~/temp

      $ git clone git@gitee.com:xxx/myrepo.git myrepo_3

       Cloning into 'myrepo_3'...
       remote: Enumerating objects: 3, done.
       remote: Counting objects: 100% (3/3), done.
       remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
       Receiving objects: 100% (3/3), done.
      

      $ ls

       myrepo  myrepo_2  myrepo_3
      
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值