zoukankan      html  css  js  c++  java
  • GIT命令

    常用操作说明, 查看配置

    # 查看仓库级的config,即.git/.config
    git config --local -l
    # 查看全局级的config,即/home/someone/.gitconfig
    git config --global -l
    # 查看系统级的config,即/etc/gitconfig
    git config --system -l
    # 查看当前生效的配置, 这个时候会显示最终三个配置文件计算后的配置信息
    git config -l
    # 有时候会看到重复的变量名,那就说明它们来自不同的配置文件(如/etc/gitconfig, ~/.gitconfig), 不过最终 Git 实际采用的是最后一个。
    

    编辑和增加配置, 格式:

    git config [–local|–global|–system] –add section.key value(默认是添加在local配置中) 注意add后面的section,key,value一项都不能少,否则添加失败

    # 添加local配置, 这两个都会新增 user.name, 但是如果已经存在, --add会再增加一行, 而前者则是编辑原值; 如果已经存在两个记录, 前者会报错
    git config --local user.name "someone"
    git config --local --add user.email someone@somewhere.com
    # 添加global配置, 同上
    git config --global --add user.name tom
    # 编辑local配置, 会打开一个vi编辑器
    git config --local --edit
    
    # 克隆远程仓库到本地, 不加本地目录则使用默认目录名
    git clone http://git.somewhere.cn/someproject/somerepository.git [local_dir]
    # 本地仓库中存在的远程仓库列表 git remote # 显示出远程仓库地址 git remote
    -v # 根据远程仓库名, 获取其最新文件 git fetch origin
    # 更新(update)使用pull
    git pull # 查看远程仓库的详细信息 git remote show origin # 查看本地仓库的文件状态 git status # 查看标签列表 git tag # 查看本地仓库中的分支列表 git branch # 创建新分支 testing git branch testing # 切换本地仓库到testing分支(或testing标签, 命令一样) git checkout testing # 删除本地仓库分支 branch1 git branch
    -d branch1 # 强制删除本地仓库分支 testing git branch -D testing # 查看本地仓库各个branch对应的起始版本 git branch -v
    # 查看所有分支
    git branch -a
    # 查看历史, 可以一直翻页
    git log # 添加文件到版本库 git add commons
    /pom.xml # 提交 git commit -m "commit top pom.xml" # 将本地仓库的 master分支, push到远程仓库origin
    # 可以把origin看成是url的别名
    git push origin master
    # 将本地仓库的 testing分支, push到远程仓库origin的branch1分支
    # 可以把origin看成是url的别名 git push origin testing:branch1 # 使用远程仓库origin的branch1分支, 在本地创建分支并切换工作区间到这个分支上
    # 注: 本地这个分支必须同名, 如果不同名会报错 git checkout
    -b branch1 origin/branch1
    # 这样创建的分支, 今后往远程的提交就可以直接用 git push 了

    对标签的操作

    # 在checkout前 fetch一下, 避免不同步
    # --all will fetch all the remotes, 
    # --tags will fetch all tags as well
    git fetch --all --tags --prune
    # 切换到指定标签
    git checkout tags/${TAG} -b ${TAG}
    # 这个只是临时切换, 不能修改
    git checkout ${TAG}
    
    # 以下是恢复代码原始版本, 清理本地所有修改
    # Restore the removed files
    git reset --hard
    # Remove the local changes & untracked files&folders
    git clean -f -x -d
    

    提交前的批量操作

    # 批量删除
    git rm folder -rf
    # 批量添加 所有有变更及未跟踪的, 使用 -A 或者 --all
    git add -A

    合并特定的commit: 先checkout到master, 然后merge对应的commit id

    being in master branch all you need to do is:

    git merge <commit-id>

    where commit-id is hash of the last commit from newbranch that you want to get in your master branch.

    如果需要合并dev分支最新的改动到master

    git checkout dev
    git pull
    git checkout master
    git merge dev
    git push origin master
    

    Update 2017-01-19 撤销本地未push到远端的commit
    经常在开发中会碰到这种情况, 就是本地commit之后, 在push的时候发现, 已经有人push了同样功能的改动, 这时候最好把本地的commit撤销掉, 然后再跟上远端的版本, 就需要使用 git reset 这个命令, 一般用法

    # 5a51663是恢复后所期望的HEAD
    $ git reset --hard 5a51663 HEAD is now at 5a51663

    Update 2017-09-22 撤销远端的commit

    # 5a51663 是恢复后所期望的HEAD
    $ git reset --hard 5a51663
    HEAD is now at 5a51663
    # 然后再使用git push --force
    $ git push origin <分支名> --force

    Update 2017-10-28

    # clean的预处理报告, 告诉你哪些文件会被删除. 不会真正的删除文件
    git clean -n
    # 删除当前目录下所有没有track过的文件. 不删除.gitignore文件里面指定的文件夹和文件
    git clean -f  
    # 删除当前目录下没有被track过的文件和文件夹. 不删除.gitignore文件里面指定的文件夹和文件
    git clean -df
    # 删除当前目录下所有没有track过的文件. 不管是否是.gitignore文件里面的文件夹和文件.
    git clean -xf
    # 删除指定路径下的没有被track过的文件.
    git clean -f <path>
    
    # 让工作目录回退到最近一次commit
    git reset --hard
    git clean -f

    Git代理的设置

    # on, http proxy
    git config --global https.proxy https://user:password@address:port
    
    # on socks proxy
    git config --global http.proxy 'socks5://127.0.0.1:1080' 
    git config --global https.proxy 'socks5://127.0.0.1:1080'
    
    # off
    git config --global --unset http.proxy
    git config --global --unset https.proxy
    
    # info
    git config --get http.proxy
    git config --get https.proxy

    Git子模块操作

    # 添加子模块, 最后一个参数为本地创建的用于放置子模块的目录
    # 对子模块的操作要cd到子模块目录里, 命令是和普通git项目一样的
    git submodule add <repository> <path>
    git submodule add git://github.com/somewhere/some.git rack
    
    # 导出后再初始化子目录, 在主项目根目录下
    git submodule init
    git submodule update
    
    # 如果是要一次性连子目录一起导出, 可以用 --recursive 参数
    git clone --recursive http://git.location
    
    # 工作中更新子项目
    git submodule foreach git pull
    

      

    修改Git提交历史

    在命令行, 通过git rebase -i [startpoint] [endpoint]进入界面选择要修改的commit, 在这个界面保存退出后, 紧接着在命令行通过 git commit --amend修改, 最后用 git rebase --continue 进入下一个修改或结束.

    -i --interactive, 即使用交互式的界面让用户编辑完成合并操作,
    [startpoint] [endpoint] 指定了一个编辑区间, 如果不指定[endpoint], 则区间终点默认是当前分支HEAD所指向的commit (注: 该区间是一个前开后闭的区间).

    # 从最近的N个提交中选择(N可以是commit id)
    git rebase -i N
    
    # 或者, 从所有的提交中选择
    git rebase -i --root
    
    # 或者, 从指定的版本号之后进行选择
    git rebase -i [commit id]
    
    # 在上面的界面保存退出后, 仅修改用户名和邮箱, 不改内容
    git commit --amend --author="Milton <milton@somewhere.com>" --no-edit
    
    # 继续下一个修改或退出
    git rebase --continue
    

    要注意的是, 这个修改只是让git log展示的信息产生变化, 原来的提交信息, 还可以在 git reflog中看到, 每一次git rebase -i 中包含的越多, 在git reflog中留下的信息也越多, 因为每一个未修改的, 被pick的commit都会被记录

    合并commit的话, 一般使用 s, squash -- 将该commit和前一个commit合并

    关于push -u参数的说明

    Push a branch and automatically set tracking

    $ git push -u origin master
    # pushes the "master" branch to "origin" remote and sets up tracking

    “Tracking” is essentially a link between a local and remote branch. When working on a local branch that tracks some other branch, you can git pull and git push without any extra arguments and git will know what to do.

    However, git push will by default push all branches that have the same name on the remote. To limit this behavior to just the current branch, set this configuration option:

    $ git config --global push.default tracking

    This is to prevent accidental pushes to branches which you’re not ready to push yet.

  • 相关阅读:
    Component 组件props 属性设置
    template 模版制作
    vue生命周期钩子函数
    Vue.set 全局操作 结构器外面修改数据
    Vue.extend 构造器的延伸
    vue.directive自定义指令
    实战笔记
    实战-第二讲 Vue-cli搭建开发环境
    实战-第一讲 画设计图
    webpack-第03节:配置文件:入口和出口
  • 原文地址:https://www.cnblogs.com/milton/p/4665492.html
Copyright © 2011-2022 走看看