git-command

介绍

Git 是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目。

Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件。

Git 与常用的版本控制工具 CVS, Subversion 等不同,它采用了分布式版本库的方式,不必服务器端软件支持。

Git基本架构

git-command

  • Workspace:工作区
  • Index / Stage:暂存区
  • Repository:仓库区(或本地仓库)
  • Remote:远程仓库

安装

MacOS:

1
$ brew install git 

Ubuntu:

1
$ sudo apt-get install git

Centos:

1
$ sudo yum install -y git

配置

Git 系统配置文件位置: /etc/.gitconfig,用户配置文件位置:~/.gitconfig

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 配置密钥
$ ssh-keygen -t rsa -C "your_email@example.com"
# 打开Github Account Settings > Add SSH Key
$ ssh -T git@github.com

# 查看当前生效的配置信息
$ git config [--list | -l]

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

# 配置日志打印格式,别名 lga / lg
$ git config --global alias.lga "log --graph --date=short --pretty=format:'%C(auto)%h %d %s (%Cgreen%an%Creset) %Cblue%ad%Creset' --all"
$ git log --graph --abbrev-commit --decorate --all --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(dim white) - %an%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n %C(white)%s%C(reset)'
$ git config --global alias.lg "log --graph --abbrev-commit --decorate --all --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(dim white) - %an%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n %C(white)%s%C(reset)'"

使用

1
2
3
4
5
6
7
8
9
10
11
12
13
# 在命令行上创建新的存储库
echo "# notes" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main # 分支改名
git remote add origin <URL> # 绑定
git push -u origin main # 推送到远程 main

# 从命令行推送现有存储库
git remote add origin <URL>
git branch -M main
git push -u origin main

文件状态

Git 文件有三种状态: 已提交(Committed)已修改(Modified)已暂存(Staged)。在加一种未跟踪(Untracked)

  • **未跟踪(Untracked)**表示文件没有加入到git库, 不参与版本控制. 通过git add命令将文件状态变为Staged
  • **已修改(Modified)**表示修改了文件,但还没保存到数据库中。
  • **已暂存(Staged)**表示对一个已修改文件的当前版本做了标记,使之包含在下次提交的快照中。
  • **已提交(Committed / Unmodified)**表示数据已经安全地保存在本地数据库中。

git1