Skip to content

Instantly share code, notes, and snippets.

@tonyc726
Last active January 8, 2019 17:35
Show Gist options
  • Select an option

  • Save tonyc726/8295260 to your computer and use it in GitHub Desktop.

Select an option

Save tonyc726/8295260 to your computer and use it in GitHub Desktop.

Revisions

  1. tonyc726 revised this gist May 19, 2014. 2 changed files with 180 additions and 4 deletions.
    179 changes: 179 additions & 0 deletions git_shell.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,179 @@
    # Git常用命令备忘

    ### Git配置
    > 用户的git配置文件** ~/.gitconfig **
    ```shell
    # 设置user
    git config --global user.name "robbin"
    git config --global user.email "[email protected]"
    # 打开配色
    git config --global color.ui true
    # 设置alias
    git config --global alias.co checkout
    git config --global alias.ci commit
    git config --global alias.st status
    git config --global alias.br branch
    # 设置Editor使用textmate
    git config --global core.editor "mate -w"
    # 列举所有配置
    git config -l
    ```
    > **注:以下命令全部基于此配置**
    -----

    ## Git常用命令

    ### 查看、添加、提交、删除、找回,重置修改文件
    ```shell
    git help <command> # 显示command的help
    git show # 显示某次提交的内容
    git show $id

    git co -- <file> # 抛弃工作区修改
    git co . # 抛弃工作区修改

    git add <file> # 将工作文件修改提交到本地暂存区
    git add . # 将所有修改过的工作文件提交暂存区

    git rm <file> # 从版本库中删除文件
    git rm <file> --cached # 从版本库中删除文件,但不删除文件

    git reset <file> # 从暂存区恢复到工作文件
    git reset -- . # 从暂存区恢复到工作文件
    git reset --hard # 恢复最近一次提交过的状态,即放弃上次提交后的所有本次修改

    git ci <file>
    git ci .
    git ci -a # 将git add, git rm和git ci等操作都合并在一起做
    git ci -am "some comments"
    git ci --amend # 修改最后一次提交记录

    git revert <$id> # 恢复某次提交的状态,恢复动作本身也创建了一次提交对象
    git revert HEAD # 恢复最后一次提交的状态
    ```

    ### 查看文件diff
    ```shell
    git diff <file> # 比较当前文件和暂存区文件差异
    git diff
    git diff <$id1> <$id2> # 比较两次提交之间的差异
    git diff <branch1>..<branch2> # 在两个分支之间比较
    git diff --staged # 比较暂存区和版本库差异
    git diff --cached # 比较暂存区和版本库差异
    git diff --stat # 仅仅比较统计信息
    ```

    ### 查看提交记录
    ```shell
    git loggit log <file> # 查看该文件每次提交记录
    git log -p <file> # 查看每次详细修改内容的diff
    git log -p -2 # 查看最近两次详细修改内容的diff
    git log --stat # 查看提交统计信息
    ```
    > **tig**
    > Mac上可以使用tig代替diff和log,brew install tig
    ----------

    ## Git 本地分支管理

    ### 查看、切换、创建和删除分支
    ```shell
    git br -r # 查看远程分支
    git br <new_branch> # 创建新的分支
    git br -v # 查看各个分支最后提交信息
    git br --merged # 查看已经被合并到当前分支的分支
    git br --no-merged # 查看尚未被合并到当前分支的分支

    git co <branch> # 切换到某个分支
    git co -b <new_branch> # 创建新的分支,并且切换过去
    git co -b <new_branch> <branch> # 基于branch创建新的new_branch
    git co $id #把某次历史提交记录checkout出来,但无分支信息,切换到其他分支会自动删除
    git co $id -b <new_branch> # 把某次历史提交记录checkout出来,创建成一个分支

    git br -d <branch> # 删除某个分支
    git br -D <branch> # 强制删除某个分支 (未被合并的分支被删除的时候需要强制)
    ```

    ### 分支合并和rebase
    ```shell
    # 将branch分支合并到当前分支
    git merge <branch>

    # 不要Fast-Foward合并,这样可以生成merge提交
    git merge origin/master --no-ff

    # 将master rebase到branch
    git rebase master <branch>
    # 相当于:git co <branch> && git rebase master && git co master && git merge <branch>
    ```

    ----------

    ## Git补丁管理(方便在多台机器上开发同步时用)
    ```shell
    git diff > ../sync.patch # 生成补丁
    git apply ../sync.patch # 打补丁
    git apply --check ../sync.patch # 测试补丁能否成功
    ```

    ----------

    ## Git暂存管理
    ```shell
    git stash # 暂存
    git stash list # 列所有stash
    git stash apply # 恢复暂存的内容
    git stash drop # 删除暂存区
    ```

    ----------

    ## Git远程分支管理
    ```shell
    git pull # 抓取远程仓库所有分支更新并合并到本地
    git pull --no-ff # 抓取远程仓库所有分支更新并合并到本地,不要快进合并
    git fetch origin # 抓取远程仓库更新
    git merge origin/master # 将远程主分支合并到本地当前分支
    git co --track origin/branch # 跟踪某个远程分支创建相应的本地分支
    git co -b <local_branch> origin/<remote_branch> # 基于远程分支创建本地分支,功能同上
    git push # push所有分支
    git push origin master # 将本地主分支推到远程主分支
    git push -u origin master # 将本地主分支推到远程(如无远程主分支则创建,用于初始化远程仓库)
    git push origin <local_branch> # 创建远程分支, origin是远程仓库名
    git push origin <local_branch>:<remote_branch> # 创建远程分支
    git push origin :<remote_branch> #先删除本地分支(git br -d <branch>),然后再push删除远程分支
    ```

    ----------

    ## Git远程仓库管理
    ```shell
    git remote -v # 查看远程服务器地址和仓库名称
    git remote show origin # 查看远程服务器仓库状态
    git remote add origin git@github:robbin/robbin_site.git # 添加远程仓库地址
    git remote set-url origin [email protected]:robbin/robbin_site.git # 设置远程仓库地址(用于修改远程仓库地址)
    git remote rm <repository> # 删除远程仓库
    ```

    ### 创建远程仓库
    ```shell
    git clone --bare robbin_site robbin_site.git # 用带版本的项目创建纯版本仓库
    scp -r my_project.git [email protected]:~ # 将纯仓库上传到服务器上

    mkdir robbin_site.git && cd robbin_site.git && git --bare init # 在服务器创建纯仓库
    git remote add origin [email protected]:robbin/robbin_site.git # 设置远程仓库地址
    git push -u origin master # 客户端首次提交
    git push -u origin develop # 首次将本地develop分支提交到远程develop分支,并且track

    git remote set-head origin master # 设置远程仓库的HEAD指向master分支
    ```

    > 也可以命令设置跟踪远程库和本地库
    ```shell
    git branch --set-upstream master origin/master
    git branch --set-upstream develop origin/develop
    ```
    5 changes: 1 addition & 4 deletions git_ssh_keygen.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,4 @@
    # Git 小记
    > 记录一些平时常用的git技巧
    -----------
    # SSHKeys

    ## 生成SSHKeys

  2. tonyc726 revised this gist May 19, 2014. 2 changed files with 63 additions and 0 deletions.
    63 changes: 63 additions & 0 deletions git_config.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,63 @@
    #### 1.同一台电脑可以有2个git账号(不同网站的)
    > 文件路径:~/.ssh/config
    首先不同网站,当然可以使用同一个邮箱,比如我的github,gitlab,bitbucket的账号都是monkeysuzie[at]gmail.com
    这时候不用担心密钥的问题,因为这些网站push pull 认证的唯一性的是邮箱
    比如我的windows 上 2个账号一个gitlab 一个github (用的都是id_rsa)

    host github
    hostname github.com
    Port 22
    host gitlab.zjut.com
    hostname gitlab.zjut.com
    Port 65095

    不需要指定key的位置。因为默认读取id_rsa了

    这样子使用起来没有任何区别,remote 也想平时一样操作即可。因为邮箱是相同的。

    #### 2.同一台电脑有2个github账号?咋办
    比如我服务器上模拟的2个用户

    # [email protected] 我在gitlab的第一个账号suzie
    host gitlab.zjut.com
    hostname gitlab.zjut.com
    Port 65095
    User suzie
    IdentityFile /home/suzie/.ssh/id_rsa
    # 我在gitlab的第2个账号test
    host gitlab-test.zjut.com
    hostname gitlab.zjut.com
    Port 65095
    User test
    IdentityFile /home/suzie/.ssh/id_rsa_second
    # [email protected] 我在github的账号
    host github-osteach.com
    hostname github.com
    Port 22
    User osteach
    IdentityFile /home/suzie/.ssh/id_rsa_second

    这种情况下,需要几点**注意**

    1.remote pull push的时候有问题,因为要设置邮箱问题了
    pull的时候识别的是邮箱,2个github账号,2个邮箱,我们自然不能使用global的user.email了

    1.取消global
    git config --global --unset user.name
    git config --global --unset user.email

    2.设置每个项目repo的自己的user.email
    git config user.email "[email protected]"
    git config user.name "suzie"

    之后push pull就木有问题了



    #### 备注

    生成ssh key

    ssh-keygen -m rsa -C "your mail" (当前目录)
    然后可以命名默认id_rsa 或者id_rsa_second 把对应的pub放到公共服务器上。
    File renamed without changes.
  3. tonyc726 renamed this gist Apr 25, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. tony revised this gist Jan 7, 2014. No changes.
  5. tony created this gist Jan 7, 2014.
    25 changes: 25 additions & 0 deletions note for git
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    # Git 小记
    > 记录一些平时常用的git技巧

    -----------

    ## 生成SSHKeys

    ````shell
    # 以 Linux Mint (Ubuntu) 为例,需要用到 ssh-keygen 命令:
    ssh-keygen -t rsa -C "***@gmail.com" -f ~/.ssh/csser-github
    ````

    ### 简单介绍下参数含义:
    - `-t` 指定密钥类型,默认即 rsa ,可以省略
    - `-C` 设置注释文字,比如你的邮箱
    - `-f` 指定密钥文件存储文件名,会生成 csser-github 和 csser-github.pub 两个密钥文件

    回车后,遇到提示输入 yes 即可,剩下一路回车,密钥文件就在指定路径下生成了。
    转自[csser](http://www.csser.com/board/4f53875c55bdcb545c000d05)

    -----------