Git 常用命令及含义

工具推荐:国产版的 GitHub https://gitee.com/

       使用了一段时间 GitHub 版本管理感觉甚是苦恼,无他,纯英文的界面阅读起来有点麻烦,那有没有其他的办法呢?有俩种办法:第一种办法,浏览器自带翻译功能或是下载翻译插件,翻译的马马虎虎。基本可以操作。但是有些页面还是无法翻译???第二种办法,浏览社区时无意发现国产版的 GitHub https://gitee.com/, 果断使用起来,管理版本方便多了。

但是一些源码下载,介意还是使用 GitHub

 

目录

一 设置配置信息

二 代码管理

2.1 上传文件

2.1.1 先GitHub 上创建仓库

2.1.2 本地目录初始化

2.1.3 添加版本控制

​2.1.4 提交至本地仓库

2.1.5 提交到远程仓库

2.2 修改文件

2.2.1 修改文件

2.2.2  添加版本控制

2.2.3 提交至本地仓库

2.2.4 提交至远程仓库

​ 2.3 删除文件

2.4 回滚文件

2.5  下载更新远程仓库文件

三 日志查看


一 设置配置信息

config 配置有三个级别, --global 全局配置;--system 系统配置; --local 存储库配置,使用 --配置文件 可以定位到文件

1.1 设置配置信息  (config只需要配置一次)

git config --global user.name  "baicun"      # "baicun"就是你的用户名

git config --global user.name "[email protected]        # "[email protected]"就是你的用户名

1.2 查看配置

git config --system --list  # 查看系统配置

git config --global --list    # 查看全局配置

git config --local --list      # 查看存储库配置,注意需要切换目录到你的仓库下,在执行查看命令

git config  # 查看 Git 信息,得到帮助 ,见下图。对于我们使用具体命令很重要 

$ git config
usage: git config [<options>]
Config file location
    --global              use global config file
    --system              use system config file
    --local               use repository config file
    -f, --file <file>     use given config file
    --blob <blob-id>      read config from given blob object

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)
    --default <value>     with --get, use default value when missing entry

1.3 修改配置

git config --global --replace-all user.name "[email protected]"   # "[email protected]"是你修改后的名字

1.4 删除配置

首先新增一个配置项:

git config --local --add alibaba.ss "beijing"

Git 常用命令及含义

开始删除

$ git config --local --unset alibaba.ss    # 删除配置项 alibaba.ss 

二 代码管理

2.1 上传文件

2.1.1 先GitHub 上创建仓库

Git 常用命令及含义

Git 常用命令及含义

创建完成以后,保留远程仓库URL:https://github.com/baicun/city.git

2.1.2 本地目录初始化

git init     # git 初始化

初始化以后,会在我们的目录下生成一个 .git 文件。默认是隐藏的,可以取消隐藏查看。该目录下就存放着 --local 的配置信息

通过命令查看该目录下状态

git status     # 查看状态

Git 常用命令及含义

可以看到,文件目录下的文本文档显示红色的,说明没有加到我们的版本控制中

2.1.3 添加版本控制

git add beijing.txt    # 添加一个文件,如果是多个,可以使用 "." 或 "*"

再次查看状态,应该变成绿色,绿色说明已经在版本中

Git 常用命令及含义
2.1.4 提交至本地仓库

git commit -m "first commit"     #  -m:message,后面跟提交的说明,也就是日后的日志信息

Git 常用命令及含义

2.1.5 提交到远程仓库

远程仓库建立别名,方便日后传输

 git remote add city https://github.com/baicun/city.git

说明:remote 远程 ;city 别名,自己定义,默认是 origin;https://github.com/baicun/city.git   远程仓库的地址

上传至远程仓库

git push -u city master

说明: city  上面我们起的别名;master 本地仓库名称

此时,可以在我们的GitHub上查看了。

2.2 修改文件

2.2.1 修改文件

beijing.txt,如下图:

Git 常用命令及含义

2.2.2  添加版本控制

查看本地仓库状态:我们修改的文件变色了。

Git 常用命令及含义

说明它变了,不是我们之前的那个 beijing.txt 了,

给它添加到版本控制中

Git 常用命令及含义

2.2.3 提交至本地仓库

Git 常用命令及含义

2.2.4 提交至远程仓库

Git 常用命令及含义
 然后在看一下网页:

Git 常用命令及含义
 2.3 删除文件

在 city 目录下在创建一个文本文档: hebei.xml,并上传至远程仓库,操作如下。

[email protected] MINGW64 /c/projects/city (master)
$ git status
On branch master
Your branch is up to date with 'city/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        hebei.txt

nothing added to commit but untracked files present (use "git add" to track)

[email protected] MINGW64 /c/projects/city (master)
$ git add hebei.txt

[email protected] MINGW64 /c/projects/city (master)
$ git status
On branch master
Your branch is up to date with 'city/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   hebei.txt


[email protected] MINGW64 /c/projects/city (master)
$ git commit -m "add a file hebei.txt"
[master 14e3be2] add a file hebei.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 hebei.txt

[email protected] MINGW64 /c/projects/city (master)
$ git push city master
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 276 bytes | 138.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/baicun/city.git
   5dce52e..14e3be2  master -> master

[email protected] MINGW64 /c/projects/city (master)
$

Git 常用命令及含义

git rm hebei.txt

执行完删除命令以后,还要提交至远程仓库才生效,故而继续commit/push,最后看结果!

Git 常用命令及含义

我们的 hebei.txt ,已经被删除了。

2.4 回滚文件

有时候我们我们发现文件改错了,并且提交至远程仓库了,这时候重新编辑可能会出现问题,直接回滚版本就完美

git log   # 查看版本日志,如果觉得难看,就使用下面的命令

git log --pretty=oneline  #  只显示第一行

Git 常用命令及含义

 git reset --hard 0a5b26f    # 回滚版本至 0a5b26f* 那一个版本,0a5b26f  唯一标识一个版本就行 

然后我们提交至远程仓库就好了。 

git push -f city master   # -f 强制提交

2.5  下载更新远程仓库文件

首次下载

git clone https://github.com/baicun/city.git

更新远程文件至本地

git pull

2.6 下载指定分支文件 

 git clone -b branchname git地址

三 日志查看

上面的 2.4 也讲了一下日志的命令

git log   # 查看版本日志,如果觉得难看,就使用下面的命令

git log --pretty=oneline  #  只显示第一行

HEAD 表示当前最新的版本: 

Git 常用命令及含义

比如上面版本回滚以后,通过上面的命令就看不到后面的提交日志信息,此时使用命令

git reflog  # 查看历史版本

 Git 常用命令及含义

可以看到,回滚版本以后,版本号变为回滚的那个版本号。