美文网首页
常用Git命令梳理

常用Git命令梳理

作者: iLynn | 来源:发表于2017-04-05 13:58 被阅读0次

    获取远程分支

    git checkout -b local-branchName origin/remote_branchName
    

    本地分支推送到远程

    git push --set-upstream origin branchName
    

    删除本地分支

    git branch -D branchname
    

    提交代码

    git commit -a,然后编辑提交信息
    

    切换分支

    git checkout branchname 
    

    查看stash

    git stash list
    

    运用stash

    git stash pop stash@{number}
    

    删除stash

    git stash drop stash@{number}
    

    查看本次改动

    git diff 
    

    查看当前状态

    git status 
    

    查看当前分支历史提交

    git log 
    

    回滚到某次提交的时候

    git reset --hard commitID 
    

    更新远程分支列表

    git fetch origin --prune
    

    删除未跟踪的文件

    git clean -f
    

    连 untracked 的目录也一起删掉

    git clean -fd
    

    连 gitignore 的untrack 文件/目录也一起删掉 (慎用,一般这个是用来删掉编译出来的 .o之类的文件用的)

    git clean -xfd
    

    在用上述 git clean 前,建议加上 -n 参数来先看看会删掉哪些文件,防止重要文件被误删
    git clean -nxfd
    git clean -nf
    git clean -nfd

    将这个文件的改动丢弃

    git checkout -- ****/BalancePromotionShareViewController.xib
    

    删除远程分支

    git push origin --delete branchName
    

    更新远程分支

    git remote update origin --prune
    

    git pull时发生错误

    error: cannot lock ref 'refs/remotes/origin/newComerCode': ref refs/remotes/origin/newComerCode is at 5e24d51f75783b5c7fe92151b7cc3729d13d9f82 but expected 96e711592ea8660cd99f04e4fcdfe899a51c53d7
    From repositoryName ! 96e7115..e70743b  newComerCode -> origin/newComerCode  (unable to update local ref)
    

    运行:git gc --prune=now 清洗代码仓库,即可成功拉取

    安装zsh

    git clone git://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh
    cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc

    git push时,总是提示

    warning: push.default is unset; its implicit value has changed in
    Git 2.0 from 'matching' to 'simple'. To squelch this message
    and maintain the traditional behavior, use:
    
      git config --global push.default matching
    
    To squelch this message and adopt the new behavior now, use:
    
      git config --global push.default simple
    
    When push.default is set to 'matching', git will push local branches
    to the remote branches that already exist with the same name.
    
    Since Git 2.0, Git defaults to the more conservative 'simple'
    behavior, which only pushes the current branch to the corresponding
    remote branch that 'git pull' uses to update the current branch.
    
    See 'git help config' and search for 'push.default' for further information.
    (the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
    'current' instead of 'simple' if you sometimes use older versions of Git)
    

    运行:git config --global push.default simple
    之后就不会再提示了。

    备注:

    push.default可用的值 含义
    nothing 不推送任何东西并有错误提示,除非明确指定分支引用规格。强制使用分支引用规格来避免可能潜在的错误。
    current 推送当前分支到接收端名字相同的分支。
    upstream 推送当前分支到上游@{upstream}。这个模式只适用于推送到与拉取数据相同的仓库,比如中央工作仓库流程模式。
    simple 在中央仓库工作流程模式下,拒绝推送到上游与本地分支名字不同的分支。也就是只有本地分支名和上游分支名字一致才可以推送,就算是推送到不是拉取数据的远程仓库,只要名字相同也是可以的。在GIT 2.0中,simple将会是push.default的默认值。simple只会推送本地当前分支。
    matching 推送本地仓库和远程仓库所有名字相同的分支。这是git当前版本的缺省值。

    一般来说我们使用simple就可以进行正常的使用,如果严格一点儿可以用nothing。

    相关文章

      网友评论

          本文标题:常用Git命令梳理

          本文链接:https://www.haomeiwen.com/subject/krntattx.html