git使用

一、git基本使用

1.1、创建新仓库

1
2
cd target_path
git init

1.2、从远端仓库拉代码

  • 以创建一个本地仓库的克隆版本

    1
    git clone /path/to/repository
  • 远端服务器上的仓库

    1
    git clone username@host:/path/to/repository

1.3、添加与提交

1
2
git add <filename> #添加某个文件
git add * # 添加所有
1
git commit -m "代码提交信息" #提交到本地

你的改动现在已经在本地仓库的 HEAD 中了

1.4、添加远程仓库和推送

如果你还没有克隆现有仓库,并欲将你的仓库连接到某个远程服务器,执行如下命令:

1
2
git remote add origin <server>
git remote add origin git@github.com:yourname/code.git #示例

将这些改动提交到远端仓库

1
git push origin master

可以把 master 换成你想要推送的任何分支

1.5、分支

分支是用来将特性开发绝缘开来的。在你创建仓库的时候,master 是“默认的”。在其他分支上进行开发,完成后再将它们合并到主分支上。

  • 查看分支:

    1
    git branch
  • 创建分支:

    1
    git branch <name>
  • 切换分支:

    1
    git checkout <name>
  • 创建+切换分支:

    1
    git checkout -b <name>
  • 合并某分支到当前分支:

    1
    git merge <name>
  • 删除分支:

    1
    git branch -d <name>

1.6、更新与合并

更新最新代码

1
2
3
4
git checkout branch_one #切换到最新的分支branch_one
git pull # 更新最新代码
git checkout branch_two #切换到最新的分支branch_two
git merge branch_one #branch_two合并branch_one

这时候就需要你修改这些文件来人肉合并这些 冲突(conflicts) 了。改完之后,你需要执行如下命令以将它们标记为合并成功

1
2
3
git add .
git commit -m "冲突解决"
git push origin branch_two #更新合并后的代码到远端

在合并改动之前,也可以使用如下命令查看:

1
git diff <source_branch> <target_branch>

二、使用示例

  • 创建一个新的仓库

    1
    2
    3
    4
    5
    6
    git clone git@github.com:yourname/code.git
    cd code
    touch README.md
    git add README.md
    git commit -m "add README"
    git push -u origin master
  • 从已知文件夹上传到仓库

    1
    2
    3
    4
    5
    6
    7
    8
    cd existing_folder
    git init
    git add .
    git commit -m "Initial commit"
    git remote add origin git@github.com:yourname/code.git
    #获取远程库与本地同步合并(如果远程库不为空必须做这一步,否则后面的提交会失败)
    git pull --rebase origin master
    git push -u origin master
  • 已知的本地仓库

    1
    2
    3
    4
    cd existing_repo
    git remote add origin git@github.com:xiachi/code.git
    git push -u origin --all
    git push -u origin --tags
  • 合并分支

    1
    2
    3
    4
    5
    6
    7
    8
    9
    git add .
    git commit -m "合并前先提交代码"
    git push origin cur_branch #推送到远端
    git checkout branch_one #切换分支
    git pull #更新branh_one的代码
    git checkout cur_branch #切换到之前的分支
    git merge branch_one #合并
    #人肉解决冲突
    git push origin cur_branch

三、git链接多个账号

在很多情况下我们在公司使用git需要连接公司的账号,但是如果这个时候需要连接自己的github账号的时候,这个时候就需要在自己电脑上使用git链接两个账号或者多个账号

3.1、生产两个ssh-key

1
2
ssh-keygen -t rsa -C "one@gmail.com" #你的github账号
ssh-keygen -t rsa -C "two@gmail.com" #你的github账号

注意:

  • 不要一路回车,分别在第一个对话即需要输入的时候,输入文件名(如:id_rsa_one和id_rsa_two),这样会生成对应的文件名的公钥和私钥(如:id_rsa_one、id_rsa_one.pub、id_rsa_two、id_rsa_two.pub)
  • 如果执行的路径不在~/.ssh路径中执行的话,需要把文件拷贝到期目录下

3.2、添加私钥

打开ssh-agent

  • 如果你是github官方的bash:

    1
    ssh-agent -s
  • 如果你是其它,比如msysgit:

    1
    eval $(ssh-agent -s)

添加私钥

1
2
ssh-add ~/.ssh/id_rsa_one
ssh-add ~/.ssh/id_rsa_two

3.3、创建或者修改config文件

在~/.ssh中如果没有config则使用touch config创建该文件,有的话则进行如下修改:

  • 网上demo示例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    # one(one@gmail.com)

    Host one.github.com

      HostName github.com

      PreferredAuthentications publickey

      IdentityFile ~/.ssh/id_rsa_one

      User one

    # two(two@ gmail.com)

    Host two.github.com

      HostName github.com

      PreferredAuthentications publickey

      IdentityFile ~/.ssh/id_rsa_two

      User two
  • 自己电脑的示例(配置了gitlab和两个github账号)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    Host github_one #和github的名字一致
    HostName github_one.github.com
    User git
    IdentityFile /Users/xiachi/.ssh/id_rsa_ucas

    Host mayexia #mayexia
    HostName mayexia.github.com
    User git
    IdentityFile /Users/xiachi/.ssh/id_rsa_350


    Host gitlab.com
    HostName gitlab.com
    User git
    IdentityFile /Users/xiachi/.ssh/id_rsa

最后想连接github的话,需要将对应的公钥添加到github的ssh秘钥中,添加后便可使用git连接到github了。

四、git基本命令大全

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
git add file_name                                     # 向暂存区添加文件
git branch # 查看目前git仓库中已有的分支
git branch new_branch_name # 创建分支,无分支起点默认在当前提交上创建分支
git branch -d branch_name # 删除分支
git branch -D branch_name # 强制删除分支
git checkout branch_name # 切换分支
git checkout -b branch_name # 新建并切换到该分支
git checkout --file_name # 丢弃工作区的修改
git commit -m "commit_log" # 保存暂存区记录
git commit -am "commit_log" # 保存目录中已被跟踪的文件的暂存区记录
git clone remote_repo_url [file_path] # 克隆远程仓库到本地
git diff # 比较工作目录和暂存区的差异
git diff HEAD^ # 比较工作目录与仓库中最近一次的提交间的差异
git diff -cached # 比较暂存区和仓库中最近一次的提交间的差异
git fetch remote_repo_name # 从远程仓库抓取最新数据到本地但不与本地分支进行合并
git init # 初始化仓库
git log # 查看提交日志
git log --pretty=short # 只显示提交信息的第一行
git log file_name # 只显示指定目录、文件的日志
git log -p # 显示文件的改动
git log --graph # 用图形查看
git log --pretty=oneline # 查看简要信息
git merge branch_name # 在 master 分支下进行,合并分支
git merge --no-ff -m "merge_log" branch_name # 禁用 Fast forward 模式,并生成一个 commit
git pull origin branch_name # 从远程仓库抓取最新数据并自动与本地分支进行合并
git pull --rebase origin branch_name # 第一次拉取的时候先将本地与远程同步
git push origin branch_name # 将本地仓库推送到远程仓库中
git push -u origin branch_name # 第一次推送时将本地与远程关联起来
git push -f origin branch_name # 强制同步远程与本地仓库
git push origin tag_name # 将标签推送到远程仓库
git rm file_name # 删除仓库文件
git reset --hard HEAD^ # 回退到上一个版本
git reset --hard commit_id # 回退到指定的版本
git reflog # 查看提交命令
git reset HEAD -- file_name # 撤销暂存区具体文件的修改
git remote # 查看本地已经添加的远程仓库
git remote -v # 可以一并查看远程仓库的地址
git remote show remote_repo_name # 查看远程仓库信息
git remote add origin remote_repo_url # 在本地添加远程仓库
git remote rm remote_repo_name # 删除本地添加的远程仓库
git remote rename old_name new_name # 重命名远程仓库
git status # 查看仓库状态
git stash # 隐藏工作现场
git stash list # 查看工作现场
git stash apply # 恢复工作现场
git stash drop # 删除 stash 内容
git stash pop # 恢复现场并删除 stash 内容
git show commit -commit_id # 查看指定 id 的提交信息
git show -all # 显示提交历史

五、参考