zoukankan      html  css  js  c++  java
  • Git常用命令速查

    配置全局 Git 账户:

    # 设置
    $ git config --global user.name "jack"
    $ git config --global user.email "jack@test.com"
    
    # 查看
    $ git config --global user.name
    $ git config --global user.email
    

    如果某个工程需要单独的账户提交代码,则需要进入该项目,进行手动配置:

    $ cd my-project
    
    # 设置
    $ git config user.name "john"
    $ git config user.email "john@test.com"
    
    # 查看
    $ git config user.name
    $ git config user.email
    

    显示远程仓库地址:

    $ git remote -v
    

    如果线上新建了仓库,本地提交文件的相应操作:

    git init
    git remote add origin git@github.com:scottliu2011/test.git
    git add .
    git commit -m "init files"
    git push -u origin master
    

    查看本地分支:

    git branch
    

    查看远程分支:

    git branch -a
    

    建立本地分支:

    git checkout -b new-feature origin/master
    

    推送到远程分支(冒号前是本地分支,冒号后是远程分支,如果不存在会自动创建):

    git push origin new-feature:new-feature
    

    切换到本地其他分支:

    git checkout hot-fix
    

    创建本地 Tag:

    git tag tag-v3
    

    推送本地 tag 到线上:

    git push origin tag-v3
    

    删除本地 Tag:

    git tag -d tag-v3
    

    添加到暂存区后撤销该操作:

    git add test.md test.txt
    
    git reset head
    
    # 或者只针对某个文件撤销
    
    git reset head test.txt
    

    本地提交后撤销该操作:

    # 撤销刚刚提交的代码 
    
    # 软撤销 恢复到暂存区
    $ git reset --soft head^
    
    # 硬撤销 丢弃代码修改
    $ git reset --hard head^
    
    $ git log
    
    # 找到需要撤销的COMMIT_ID
    $ git reset COMMIT_ID
    
    $ git reset --soft COMMIT_ID
    
    $ git reset --hard COMMIT_ID
    

    将本地 dev 分支合并到 master 分支:

    git checkout -b dev
    
    # add & commit
    
    git checkout master
    git merge dev
    git push
    

    更改远程仓库源:

    git remote rm origin
    git remote add origin git@github.com:scottliu2011/new-test.git
    

    有时候操作不当,导致代码合并失败,这个时候可以回滚当前状态,并重新合并:

    git merge --abort
    git reset --merge
    git pull
    

    配置代理 clone 代码:

    # 查看本地代理软件socks5端口
    git config --global http.proxy socks5://127.0.0.1:10000
    
    # 检查配置是否成功
    git config --global -e
    
    # 开始使用代理clone代码
    git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
    
    # 使用完成后取消代理配置
    git config --global --unset http.proxy
    

    配置 .bash_profile 常用简写指令:

    alias ga='git add'
    alias gf='git fetch'
    alias gp='git pull'
    alias gc='git commit -m'
    alias gps='git push'
    alias gck='git checkout'
    alias gb='git checkout -b'
    alias gm='git merge'
    
    function gmback() {
      git merge --abort
      git reset --merge
    }
    
    function gtag() {
      git tag $1;
      git push origin $1;
    }
    

    暂存操作:

    
    # 新添加一条stash条目
    $ git stash save 'test'
    # 或者这样
    $ git stash push -m 'test'
    
    # 列举所有stash条目
    $ git stash list
    
    # 应用指定位置的stash条目
    $ git stash apply 0
    
    # 移除指定位置的stash条目
    $ git stash drop stash@{0}
    
    # 或者这样
    $ git stash apply 0
    $ git stash drop 0
    
    # 应用最新的stash条目
    $ git stash apply
    
    # 应用并移除最新的stash条目
    $ git stash pop
    
    # 清空本地所有stash数据
    $ git stash clear
    
    欢迎转载,转载请注明出处。
  • 相关阅读:
    章节八、2-火狐的插件TryXPath
    章节八、1-如何使用火狐开发者工具来查找元素
    章节七、6-Map集合的区别
    章节七、5-Maps
    章节七、4-Sets
    章节七、3-ArrayList和LinkedList对比
    章节七、2-Linked List
    jQuery中$符号的作用
    jQuery基础的HTML与text区别
    推荐一些github上的免费好书
  • 原文地址:https://www.cnblogs.com/liuhe688/p/14716178.html
Copyright © 2011-2022 走看看