git_study

安装git

1
sudo pacman -S git

配置git

1
2
3
4
5
6
7
8
9
10
11
12
13
#配置git的用户名 邮箱号
git config --global user.name "your_github_name"
git config --global user.email "your_github_email"

#配置默认使用的文本编辑器
git config --global core.editor vim

#查看配置
git config --list
vim ~/.gitconfig

#查看某个用户的配置
git config user.name

创建仓库

  • git init

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    #初始化一个仓库
    git init

    #初始化newrepo目录为仓库
    git init path/newrepo

    #添加跟踪的文件
    git add *.c
    git add README
    git commit -m "初始化项目版本"
  • git clone

    1
    2
    3
    4
    5
    6
    #克隆远程仓库
    git clone <repo>
    git clone <repo> <directory>

    git clone git://github.com/shcacon/grit.git
    git clone git://github.com/shcacon/grit.git
  • 配置

    1
    2
    3
    4
    5
    6
    7
    8
    #显示当前git配置信息
    git config --list

    #针对当前仓库
    git config -e

    #针对系统上的所有仓库
    git config -e --global

git基本操作

提交与修改

  • git add

    1
    2
    3
    4
    5
    6
    #添加文件到暂存区
    git add [file1] [file2] ...

    #添加目录到暂存区
    git add [dir]
    git add .
  • git status

    1
    2
    3
    4
    #查看git仓库当前状况
    git status
    # -s 显示简短的输出结果
    git status -s
  • git diff

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    #查看工作区和暂存区的差异
    git diff [file]

    #显示暂存区和上一次提交的差异
    git diff --cached [file]
    git diff --staged [file]

    #显示两次提交的差异
    git diff [first-branch]...[second-branch]

    - git commit
    ```shell
    #提交暂存区到本地仓库中
    git commit -m "message"

    #提交暂存区的指定文件到仓库区
    git commit [file1] [file2] ... -m [message]

    #参数设置修改文件后不需要执行git add 命令,直接来提交
    git commit -a

    #先设置提交代码时的用户信息
    git config --global user.name 'yourname'
    git config --global user.email 'test@youremail.com'

版本回退

  • git reset

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    #指定退回到某一次提交的版本
    git reset [--soft | --hard | --mixed] [HEAD]
    #--mixed 重置暂存区文件与上一次的提交保持一致
    #--soft 知识撤回上一次提交
    #--hard 重置工作区,与上一次的提交保持一致
    #HEAD
    HEAD 当前版本
    HEAD^ 上一次版本
    HEAD^^ 上上次版本
    HEAD^^ 上上上次版本

    HEAD-0 当前版本
    HEAD-1 上一次版本
    HEAD-2 上上一次版本
    HEAD-3 上上上一次版本
  • git rm

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    #将文件从工作区和暂存区中删除
    git rm <file>

    #强制删除
    git rm -f <file>

    #仅仅将文件从暂存区中移除
    git rm --cached <file>

    #可以递归删除
    git rm -r *
  • git mv

    1
    2
    3
    4
    5
    #移动或重命名
    git mv [file] [newfile]

    #新文件名存在,强制重命名
    git mv -f [file] [newfile]

分支切换

  • git checkout

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    #切换到指定分支
    git checkout <branch-name>

    #创建新分支 并切换到它
    git checkout -b <new-branch-name>

    #切换到前一个分支
    git checkout -

    #将指定文件恢复到最新的提交状态,丢弃所有未提交的更改,这对于撤销不需要的更改非常有用
    git checkout -- <file>

    #切换到特定提交,使用提交哈希值
    git checkout <commit-hash>

    #切换到标签
    git checkout tags/<tag-name>
  • git switch

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    #切换到制定分支
    git switch <branch-name>

    #创建一个新分支并切换到它
    git switch -c <new-branch-name>

    #切换到前一个分支
    git switch -

    #切换到标签指向的提交状态
    git switch tags/<tag-name>
  • git restore

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    #恢复文件到最新提交状态
    git restore <file>

    #还原文件到暂存区状态
    git restore --staged <file>

    #还原所有未提交的更改
    git restore .

    #还原文件到特定提交的状态
    git restore --source=<commit> <file>

提交日志

  • git log

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    git log  [options] [branch-name | commit-hash] 
    -p 显示提交的补丁 具体修改内容
    --oneline 简洁的一行格式显示提交信息
    --graph 以图形方式显示分支和合并历史
    --decorate 显示分支和标签指向的提交
    --author=<author> 显示特定作者的提交
    --since=<time> 显示指定时间之后的提交
    --until=<time> 显示指定时间之前的提交
    --grep=<模式> 显示包含指定模式的提交消息
    --no-merges 不显示合并提交
    --stat 显示简略统计信息,包括修改的文件和行数
    --abbrev-commit 使用短提交哈希值
    --pretty=<格式> 使用自定义的提交信息显示格式
    --reverse 逆向显示所有日志
  • git blame

    1
    2
    3
    4
    5
    6
    #逐行显示指定文件的每一行代码注释和相关信息
    git blame [option] [path]
    -L <起始行号>,<结束行号> 指定行号范围
    -C 对重命名或拷贝的代码进行溯源
    -M 对移动的代码进行溯源
    --show-stats 显示行数统计信息

git 远程操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#累出当前仓库总已配置的远程仓库
git remote

#列出当前仓库中以配置的远程仓库,显示出URL
git remote -v

#添加一个新的远程仓库
git remote add <remote-name> <remote-url>

#将已配置的远程仓库重命名
git remote rename <old_name> <new_name>

#从当前仓库中删除指定的远程仓库
git remote remove <remote_name>

#修改指定远程仓库的URL
git remote set-url <remote-name> <new-url>

#显示指定远程仓库的详细信息,包括URL和跟踪分支
git remote show <remote-name>
  • git fetch

    1
    2
    3
    4
    5
    #提取远程仓库更新的数据
    git fetch [alias]

    #从远程仓库提取数据,病尝试合并到当前分支
    git merge [alias] [branch]
  • git pull

    1
    2
    3
    4
    5
    6
    7
    8
    #从远程获取代码并合并到本地版本
    git pull <remote-host> <remote-branch-name>:<local-branch-name>

    #将远程主机origin的master分支拉取过来,与本地的brantest合并起来
    git pull origin master:brantest

    #与当前分支合并
    git pull origin master
  • git push

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    #将本地仓库的分支版本上传到远程并合并
    git push <remote-host> <local-branch>:<remote-branch>

    #将本地仓库当前分支master推送到远程origin主机的master分支上
    git push origin master

    #
    git push origin master:master

    #强制执行
    git push --force origin master

    #删除远程主机origin的master分支
    git push origin --delete master

分支管理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#列出分支
git branch

#创建分支
git branch <new-branch>

#切换分支
git checkout <branch-name>

#合并分支
git merge

#删除分支
git branch -d <branch-name>
  • git 标签

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    #创建带注解的标签
    git tag -a v1.0

    #查看所有标签
    git tag

    #指定标签信息
    git tag -a <tagname> -m "标签"

    #PGP签名标签命令
    git tag -s <tagname> -m "标签"

github

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#有远程仓库
git remote add [shortname] [url]

#没有仓库
#github注册 新建仓库
#生成SSH-KEY
ssh-keygen -t rsa -C "youremail@example.com"


#查看当前远程库
git remote

git remote -v

#提取远程库
git fetch

#合并远程库
git merge

#推送分支
git push [alias] [branch]

#删除远程仓库
git remote rm [别名]

git 服务器搭建

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
groupadd gi
useradd git -g git

cd /home/git
mkdir .ssh
chmod 755 .ssh
touch .ssh/authorized_keys
chmod 644 .ssh/authorized_keys
#收集所有需要登录的用户的公钥,公钥位于id_rsa.pub中

cd /home
mkdir gitrepo
chown git:git gitrepo/
cd gitrepo
git init --bare your.git
chown -R git:git your.git
#初始化仓库

#克隆仓库
git clone git@IP:/home/gitrepo/your.git