Git:版本控制工具简单使用

环境:
root@iZm2lq55rpe68hZ:~# cat /etc/issue
Ubuntu 18.04.1 LTS \n \l

root@iZm2lq55rpe68hZ:~# 
一、安装
1. 安装

Ubuntu:

apt install git

CentOS:

yum install git

查看一下:

root@iZm2lq55rpe68hZ:~# git
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           <command> [<args>]

These are common Git commands used in various situations:

start a working area (see also: git help tutorial)
   clone      Clone a repository into a new directory
   init       Create an empty Git repository or reinitialize an existing one

work on the current change (see also: git help everyday)
   add        Add file contents to the index
   mv         Move or rename a file, a directory, or a symlink
   reset      Reset current HEAD to the specified state
   rm         Remove files from the working tree and from the index

examine the history and state (see also: git help revisions)
   bisect     Use binary search to find the commit that introduced a bug
   grep       Print lines matching a pattern
   log        Show commit logs
   show       Show various types of objects
   status     Show the working tree status

grow, mark and tweak your common history
   branch     List, create, or delete branches
   checkout   Switch branches or restore working tree files
   commit     Record changes to the repository
   diff       Show changes between commits, commit and working tree, etc
   merge      Join two or more development histories together
   rebase     Reapply commits on top of another base tip
   tag        Create, list, delete or verify a tag object signed with GPG

collaborate (see also: git help workflows)
   fetch      Download objects and refs from another repository
   pull       Fetch from and integrate with another repository or a local branch
   push       Update remote refs along with associated objects

'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.
root@iZm2lq55rpe68hZ:~#
2. 生成SSH KEY
ssh-keygen

我们先通过git clone命令来下载码云上的项目

root@iZm2lq55rpe68hZ:~# git clone [email protected]:onefine/test_project.git 
Cloning into 'test_project'...
The authenticity of host 'gitee.com (120.55.226.24)' can't be established.
ECDSA key fingerprint is SHA256:FQGC9Kn/eye1W8icdBgrQp+KkGYoFgbVr17bmjey0Wc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'gitee.com,120.55.226.24' (ECDSA) to the list of known hosts.
[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
root@iZm2lq55rpe68hZ:~#

提示不能读取远程的仓库,确保正确的访问权限???

公钥放在第三方平台里面,我们就可以访问了:

root@iZm2lq55rpe68hZ:~# cd ~/.ssh/
root@iZm2lq55rpe68hZ:~/.ssh# ls -al
total 12
drwx------ 2 root root 4096 Feb  6 17:34 .
drwx------ 9 root root 4096 Feb  6 17:34 ..
-rw------- 1 root root    0 Jan 16 18:21 authorized_keys
-rw-r--r-- 1 root root  444 Feb  6 17:34 known_hosts
root@iZm2lq55rpe68hZ:~/.ssh# ssh-keygen
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:146oHWtoWH1VnaZj3FuTP+r9NOWuLvj0o5I3IX4CwKE root@iZm2lq55rpe68hZ
The key's randomart image is:
+---[RSA 2048]----+
|               ..|
|      .       .o.|
|     o .    ..+ .|
|    E o    ..= +.|
|       oS ..o . *|
|      . oooo.  +o|
|     o .o+.+o...+|
|    . oo.o*.=ooo.|
|     ...o  *+*+++|
+----[SHA256]-----+
root@iZm2lq55rpe68hZ:~/.ssh# ll
total 20
drwx------ 2 root root 4096 Feb  6 17:43 ./
drwx------ 9 root root 4096 Feb  6 17:34 ../
-rw------- 1 root root    0 Jan 16 18:21 authorized_keys
-rw------- 1 root root 1675 Feb  6 17:43 id_rsa			# 私钥
-rw-r--r-- 1 root root  402 Feb  6 17:43 id_rsa.pub		# 公钥
-rw-r--r-- 1 root root  444 Feb  6 17:34 known_hosts
root@iZm2lq55rpe68hZ:~/.ssh# 
root@iZm2lq55rpe68hZ:~/.ssh# cat id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQChOG+tZdzwsFdnqLYQi62hHGW/b3n7lDf60OX+4v1qVJ52BEbLpGXcy2qDsNhOTKC5k71Dzd6hYiLXSGAdE8N+kQkhDJBTstwQRRrZ+Y0p432sxc3dESb3YP7u+3QhWKGNf6hZHZyj8G6Lt9s5aDu8I+u0YW7t+VJVzFQlipaXEtnPLh9aVe4yIUixvBzj7P42qFSTv7SZWJhCXUYBVmS/eCb4aGJdK8HbPE7U+GZ23+bgQnWQJPDq+TTq2DL6i+aAjC6/QUapiYmAPjGHTvDQ1dXy1PYYEjbgYz+SdQFe02Yz+AhLAH7lIVjlsr5rPckxcMR6EQ4NKwUg4MgB9mkt root@iZm2lq55rpe68hZ
root@iZm2lq55rpe68hZ:~/.ssh# 

将生成的SSH KEY复制到git平台。
我们接着通过git clone命令来下载码云上的项目:

root@iZm2lq55rpe68hZ:/tmp# git clone [email protected]:onefine/test_project.git
Cloning into 'test_project'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
root@iZm2lq55rpe68hZ:/tmp# ll | grep test_project 
drwxr-xr-x  3 root  root  4096 Feb  6 18:17 test_project/
root@iZm2lq55rpe68hZ:/tmp# 
3. 访问码云( https://gitee.com/ ),配置SSH KEY
4. 基本命令
最常用的 git 命令有:
   add        添加文件内容至索引
   bisect     通过二分查找定位引入 bug 的变更
   branch     列出、创建或删除分支
   checkout   检出一个分支或路径到工作区
   clone      克隆一个版本库到一个新目录(将远程代码克隆到本地)
   commit     记录变更到版本库
   diff       显示提交之间、提交和工作区之间等的差异
   fetch      从另外一个版本库下载对象和引用(将远程的更新拉取到本地)
   grep       输出和模式匹配的行
   init       创建一个空的 Git 版本库或重新初始化一个已存在的版本库
   log        显示提交日志
   merge      合并两个或更多开发历史
   mv         移动或重命名一个文件、目录或符号链接
   pull       获取并合并另外的版本库或一个本地分支
   push       更新远程引用和相关的对象(将本地的代码推送到远程)
   rebase     本地提交转移至更新后的上游分支中(将本地的分支进行合并)
   reset      重置当前HEAD到指定状态
   rm         从工作区和索引中删除文件
   show       显示各种类型的对象
   status     显示工作区状态
   tag        创建、列出、删除或校验一个GPG签名的 tag 对象
  1. clone
    上面已经介绍。

  2. remote

root@iZm2lq55rpe68hZ:/tmp# cd test_project/
root@iZm2lq55rpe68hZ:/tmp/test_project# git remote -v
origin  [email protected]:onefine/test_project.git (fetch)
origin  [email protected]:onefine/test_project.git (push)
root@iZm2lq55rpe68hZ:/tmp/test_project# git remote --help
...
root@iZm2lq55rpe68hZ:/tmp/test_project# 
root@iZm2lq55rpe68hZ:/tmp/test_project# git remote add bd [email protected]:onefine/test_project.git
root@iZm2lq55rpe68hZ:/tmp/test_project# git remote -v
bd      [email protected]:onefine/test_project.git (fetch)
bd      [email protected]:onefine/test_project.git (push)
origin  [email protected]:onefine/test_project.git (fetch)
origin  [email protected]:onefine/test_project.git (push)
root@iZm2lq55rpe68hZ:/tmp/test_project# git remote rm bd
root@iZm2lq55rpe68hZ:/tmp/test_project# git remote -v
origin  [email protected]:onefine/test_project.git (fetch)
origin  [email protected]:onefine/test_project.git (push)
root@iZm2lq55rpe68hZ:/tmp/test_project# 
  1. 1
root@iZm2lq55rpe68hZ:/tmp/test_project# ls -al
total 16
drwxr-xr-x  3 root root 4096 Feb  6 18:17 .
drwxrwxrwt 13 root root 4096 Feb  6 18:28 ..
drwxr-xr-x  8 root root 4096 Feb  6 18:52 .git
-rw-r--r--  1 root root 1310 Feb  6 18:17 README.md
#...修改README.md 
root@iZm2lq55rpe68hZ:/tmp/test_project# vim README.md 
root@iZm2lq55rpe68hZ:/tmp/test_project#
root@iZm2lq55rpe68hZ:/tmp/test_project# 
root@iZm2lq55rpe68hZ:/tmp/test_project# 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 checkout -- <file>..." to discard changes in working directory)

        modified:   README.md

no changes added to commit (use "git add" and/or "git commit -a")
root@iZm2lq55rpe68hZ:/tmp/test_project# 
root@iZm2lq55rpe68hZ:/tmp/test_project# git commit -am "onefine -- modify readme" 

*** Please tell me who you are.

Run

  git config --global user.email "[email protected]"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'root@iZm2lq55rpe68hZ.(none)')
root@iZm2lq55rpe68hZ:/tmp/test_project#
root@iZm2lq55rpe68hZ:/tmp/test_project# git config --global user.email "[email protected]"
root@iZm2lq55rpe68hZ:/tmp/test_project# git config --global user.name "onefine"  
root@iZm2lq55rpe68hZ:/tmp/test_project# git commit -am "onefine -- modify readme"           
[master fc381e4] onefine -- modify readme
 1 file changed, 2 insertions(+), 2 deletions(-)
root@iZm2lq55rpe68hZ:/tmp/test_project# 
root@iZm2lq55rpe68hZ:/tmp/test_project# git push origin master:master
Counting objects: 6, done.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 576 bytes | 576.00 KiB/s, done.
Total 6 (delta 2), reused 0 (delta 0)
remote: Powered By Gitee.com
To gitee.com:onefine/test_project.git
   40e6137..1277b9f  master -> master
root@iZm2lq55rpe68hZ:/tmp/test_project#

将远程的更改推送到本地:

root@iZm2lq55rpe68hZ:/tmp/test_project# git fetch
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From gitee.com:onefine/test_project
   1277b9f..ae71b36  master     -> origin/master
root@iZm2lq55rpe68hZ:/tmp/test_project# 

和origin源中的master进行合并:

root@iZm2lq55rpe68hZ:/tmp/test_project# git rebase origin/master
First, rewinding head to replay your work on top of it...
Fast-forwarded master to origin/master.
root@iZm2lq55rpe68hZ:/tmp/test_project# 

推荐学习Git基本命令网址: https://www.runoob.com/git/git-tutorial.html

二、git命令自动补全设置

附Git 常用命令

参考: https://gitee.com/all-about-git

仓库

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

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

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

配置

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

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

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

增加/删除文件

# 添加指定文件到暂存区
$ 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]

代码提交

# 提交暂存区到仓库区
$ 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] ...

分支

# 列出所有本地分支
$ 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 branch -d [branch-name]

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

标签

# 列出所有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]

查看信息

# 显示有变更的文件
$ 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

远程同步

# 下载远程仓库的所有变动
$ 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

撤销

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

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

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

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

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

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

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

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

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

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

其他

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

以上常用命令来自阮一峰老师的博客文章《常用 Git 命令清单》,感谢阮老师!

猜你喜欢

转载自blog.csdn.net/jiduochou963/article/details/86773443