Category: Git

  • git 压缩提交 squash merge

    分支里多次提交合并成一个提交

    示例

    将最近几个(number_of_commits) 提交合并成一个提交

    git rebase -i HEAD~[number_of_commits]

    然后进入交互式选择

    #
    # Commands:
    # p, pick = use commit
    # r, reword = use commit, but edit the commit message
    # e, edit = use commit, but stop for amending
    # s, squash = use commit, but meld into previous commit
    # f, fixup = like "squash", but discard this commit's log message
    # x, exec = run command (the rest of the line) using shell
    #
    # These lines can be re-ordered; they are executed from top to bottom. #
    # If you remove a line here THAT COMMIT WILL BE LOST.
    #
    # However, if you remove everything, the rebase will be aborted.
    #
    # Note that empty commits are commented out
    
    

    这里先进行 commit 方面的选择,把除去第一行的都改为 squash

    pick f7f3f6d changed my name a bit
    squash 310154e updated README formatting and added blame
    squash a5f4a0d added cat-file
    

    然后进行 commit msg 的界面,自己写一下合并后的 commit。

    分支的 push 强制提交会将本地操作生效

    参考:

    pro git

  • git 远端分支修剪 prune

    查看远端( origin ) 的时候,发现有些分支已经 stale 了

     git remote show origin 
     stale (use 'git remote prune' to remove)

    修剪

    git remote prune origin
  • git Your branch and ‘origin/master’ have diverged

    On branch master

    Your branch and ‘origin/master’ have diverged,

    and have 7 and 8 different commits each, respectively.

    处理方法

    git rebase origin/master

  • git branch tag

    创建 branch

    git branch cat

    切换到

    git checkout cat

    这时HEAD 已经到 cat

    在 master 上合并 cat

    git merge cat

    删除 cat 分支

    git branch -d cat

    创建分支并切换

    git checkout -b admin

    建立本地分支 develop

    git checkout -b develop

    查看本地各分支的最后一次提交

    git branch -v

    建立远端分支 remote branch

    git push origin develop

    当另一个人在本地

    git pull 的时候,会有提示多了一个分支,但是他本地并没有新建 develop

    git branch -r 查看远端分支

    检出这个分支

    git checkout develop

    查看本地和远程分支情况

    git remote show origin

    删除远端分支

    git push origin :develop

    删除本地分支,用 -D

    git branch -D develop

    对于已经删除的 develop 分支, 在本地的 develop 下 git push 后,会提示 Everything up-to-date

    git remote show origin

    这时要做

    git remote prune origin

    将远端清理

    tag 相当于 commit ,经常用于 release

    git tag
    git checkout v0.0.1

    添加新 tag

    git tag -a v0.0.3 -m "version 0.0.3"

    推送tag到远端

    git push --tags
  • git通过https方式访问时保存认证

    从 github 上更新代码,用的方式为 https方式。如果 git 客户端 >= 1.7.9,使用如下方式缓存密码

    git config --global credential.helper cache

    默认缓存 900s( 15 min),设置 1 小时

    git config --global credential.helper "cache --timeout=3600"

    如果 git 客户端 < 1.7.9 使用命令

    git config remote.origin.url https://you:[email protected]/you/example.git

    或直接编辑文件 .git/config, 在 username 后加上你的密码

    [remote "origin"]
            fetch = +refs/heads/*:refs/remotes/origin/*
            url = https://username:[email protected]/jpuyy/myproxy.git

    参考:
    http://stackoverflow.com/questions/5343068/is-there-a-way-to-skip-password-typing-when-using-https-github

  • git删除commit

    删除最近的一次 commit

    git reset --hard HEAD~1

    git rebase -i HEAD~1

    使用 git log 的时候看到的 sha1 的提交

    git reset --hard 

    如果不幸已经提交,则需要如下操作将提交还原

    git push origin HEAD --force

    将所有修改还原

    git reset --hard HEAD

    经过上面的操作, commit 被删掉了,你做的工作也废了

    如果你只是想要撤回提交,但并不想把工作废掉,可以使用 –soft

    git reset --soft HEAD~1

    我错删了一个文件并提交,现在需要将这次的提交恢复

    git revert c588349186b8dc3d074d64eca1408d2966a30cdc

    然后显示如下信息,我可以写下为什么要收回这次提交

    Revert "delete no use .vimrc"
    
    This reverts commit c588349186b8dc3d074d64eca1408d2966a30cdc.
    
    # Please enter the commit message for your changes. Lines starting
    # with '#' will be ignored, and an empty message aborts the commit.
    # On branch master
    # Your branch is ahead of 'origin/master' by 1 commit.
    #   (use "git push" to publish your local commits)
    #
    # Changes to be committed:
    #       new file:   ubuntu/home/jpuyy/.vimrc
    

    然后这就变成了一次新的提交,但是返回到了之前的版本.

    参考:
    http://stackoverflow.com/questions/1338728/delete-commits-from-a-branch-in-git