Git基础操作

Git初始配置
1
2
3
4
5
6
7
8
9
10
11
12
$ git config --global user.name "Zera7ul"
$ git config --global user.email "yefangshun@sina.com"
$ ssh-keygen -t rsa -C "yefangshun@sina.com" #生成ssh的rsa密钥对
$ git config --global http.https://github.com.proxy http://127.0.0.1:7890 #设置http代理
$ git config --global https.https://github.com.proxy http://127.0.0.1:7890 #设置https代理
$ git config --global http.https://github.com.proxy socks5://127.0.0.1:7890 #设置socks代理
$ git config --global https.https://github.com.proxy socks5://127.0.0.1:7890 #设置socks代理
$ git config --global --unset http.proxy
$ git config --global --unset https.proxy
$ git config --global --unset http.https://github.com.proxy
$ git config --global --unset https.https://github.com.proxy
$ git config --global -l

进入GitHub_Settings_keys](https://github.com/settings/keys) ,新建new SSH Key,复制C:\Users\Administrator\.ssh\id_rsa.pub(linux密钥在~/.ssh)的内容

1
$ ssh git@github.com #验证密钥是否成功
创建本地仓库
1
2
3
4
5
$  cd /D/Documents/GitHub
$ mkdir MyHexo
$ cd MyHexo
$ pwd
/D/Documents/GitHub/MyHexo
1
2
3
4
$ git init
Initialized empty Git repository in /D/Documents/GitHub/MyHexo/.git/
$ ls -ah
./ ../ .git/
提交文件到本地仓库
1
2
3
$ echo "# MyHexo" >> README.md #在Working Directory工作区新建README文件
$ git add README.md #将README文件添加到stage暂存区
$ git commit -m "first commit" #将暂存区的README文件提交到Master分支
同步到远程仓库

在Github新建同名远程仓库

1
2
$ git remote add origin git@github.com:Zera7ul/MyHexo.git #或 git remote  add origin https://github.com/Zera7ul/MyHexo.git
$ git push -u origin master
  • origin 并不是指得是远程的仓库,而是指得是远程仓库在本地的一个指针
  • 比如命令:git merge origin master指的就是将本地的远端分支与本地的master 分支进行合并
从远程库克隆到本地
1
$ git clone git@github.com:Zera7ul/MyHexo.git #或 https://github.com/Zera7ul/MyHexo.git
版本控制
1
2
3
4
5
6
7
8
9
10
$ git log #查看版本记录
$ git log --pretty=oneline #查看简要版本记录
$ git reset --hard HEAD^ #回退到上一个版本
$ git reset --hard 1094a... #回退到某个具体版本
$ git reflog #查看命令历史
$ git status #查看Git
$ git checkout -- README.md #丢弃README.md文件在工作区的修改,用版本库里的版本替换工作区的版本
$ git reset HEAD README.md #把README.md文件在暂存区的修改回退到工作区
$ rm <file> #删除工作区文件
$ git rm <file> #删除版本库内的文件
分支管理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ git checkout -b dev #-b参数表示创建并切换,等同于git branch dev + git checkout dev
Switched to a new branch 'dev'

$ git branch #查看分支
* dev
master

$ git checkout master
$ git merge dev #合并dev到当前分支
$ git branch -d dev #删除dev分支

$ git switch -c dev
$ git switch master
#使用新的git switch命令,比git checkout要更容易理解

$ git log --graph #查看分支合并图

删除本地仓库

1
2
3
$ ls -a
$ find . -name ".git" | xargs rm -Rf
$ rm -rf .git

安装GitLab社区版

1
2
sudo yum install gitlab-ce #自动安装最新版
sudo yum install gitlab-ce-x.x.x #安装指定版本

GitLab常用命令

1
2
3
4
5
6
7
8
sudo gitlab-ctl start # 启动所有 gitlab 组件;
sudo gitlab-ctl stop # 停止所有 gitlab 组件;
sudo gitlab-ctl restart # 重启所有 gitlab 组件;
sudo gitlab-ctl status # 查看服务状态;
sudo gitlab-ctl reconfigure # 启动服务;
sudo vim /etc/gitlab/gitlab.rb # 修改默认的配置文件;
gitlab-rake gitlab:check SANITIZE=true --trace # 检查gitlab;
sudo gitlab-ctl tail # 查看日志;

参考:廖雪峰的Git教程