zoukankan      html  css  js  c++  java
  • 整理平时常用git命令

    git常用命令

    git创建分支

    #创建本地分支并切换到新创建的分支
    $ git checkout -b newbranch
    #将新创建的分支信息推送到github
    $ git push origin HEAD -u
    

    暂存已修改的代码

    # 暂存代码
    $ git stash
    # 一些别的操作,例如更新代码
    $ git pull
    # 操作完之后,还原被保存的代码
    $ git pop
    

    GIT 合并多个commit

    #commitID 为需要合并的commit之前的一个commitID
    $ git rebase -i commitID
    
    • pick 的意思是要会执行这个 commit
    • squash 的意思是这个 commit 会被合并到前一个commit

    只保留一个pick,剩下都改为squash,然后输入 wq 保存并退出。进入commit message 的编辑界面,删除之前的commitMessage,写入一个新的commitMessage,保存

    #查看 commit 历史信息,你会发现这两个 commit 已经合并了。
    $ git log
    #撤销修改
    $ git rebase --abort
    #推送到远程仓库
    $ git push  -f
    

    git 免密码操作

    #git bash进入你的项目目录,输入:
    $ git config --global credential.helper store
    #操作一次,这一次之后就不需要再次输入密码
    $ git pull
    

    强制覆盖本地代码(与git远程仓库保持一致)

    git fetch --all && git reset --hard origin/branch && git pull
    

    GIT 查看/修改用户名和邮箱地址

    #查看用户名和邮箱地址:
    $ git config user.name
    $ git config user.email
    
    #修改用户名和邮箱地址:
    $ git config --global user.name "username"
    $ git config --global user.email "email"
    

    同时提交到两个分支

    #bug修复提交到release/v1.0分支
    $ git commit -m "提交代码"
    $ git push origin release/v1.0    
    # 切换分支至release/v2.0
    $ git checkout release/v2.0
    # 将release/2.0代码更新至最新
    git pull origin release/v2.0
    # 将release/1.0的代码更新至最新
    git pull origin release/v1.0
    # 将commit提交至release/2.0
    git push origin release/v2.0
    

    合并子分支代码至主分支

    # 先切换至主分支,更新到最新代码
    $ git checkout master 
    $ git pull
    # 再切换回子分支
    $ git checkout  dev
    $ git rebase master
    # 此时出现冲突,详细冲突可用 git  status 查看,并解决冲突
    $ git add -u 文件
    $ git rebase --continue 
    # 此时出现第二个冲突,继续执行上述操作。
    # 所有冲突解决完之后,切换至主分支
    $ git checkout master
    $ git merage  dev
    #此时已完成子分支代码合并至主分支,可用 git log查看提交记录
    #另外如果rebase过程中,你想中途退出,恢复rebase前的代码则可以用命令 
    $ git rebase --abort
    

    git修改分支名称

    # 1. 本地分支重命名(还没有推送到远程)
    $ git branch -m oldName newName
    
    # 2. 远程分支重命名 (已经推送远程-假设本地分支和远程对应分支名称相同)
    	# a. 重命名远程分支对应的本地分支
    $ git branch -m oldName newName
    	# b. 删除远程分支
    $ git push --delete origin oldName
    	# c. 上传新命名的本地分支
    $ git push origin newName
    	# d.把修改后的本地分支与远程分支关联
    $ git branch --set-upstream-to origin/newName
    

    查看分支

    # 查看本地分支
    $ git branch
    # 查看所有分支,包括本地和远程
    $ git branch -a
    # 查看本地分支与远程分支对应关系
    $ git branch -vv
    # 关联本地分支与远程分支
    $ git checkout localBranchName && git branch --set-upstream-to origin/remoteBranchName
    

    切换至指定tag

    # 查看所有tag
    $ git tag
    # 切换至指定tag <v0.8.0-beta>
    $ git checkout -b  v0.8.0-beta v0.8.0-beta
    

    删除分支

    # 删除本地分支
    $ git branch -d <BranchName>
    # 删除远程分支
    $ git push--delete  origin  <BranchName>
    

    切换分支

    $ git checkout -b <BranchName>  origin/<BranchName>
    

    Git忽略规则及.gitignore规则不生效的解决办法

    $ git rm -r --cached .
    $ git add .
    $ git commit -m 'update .gitignore'
    

    删除本地所有未提交的更改

    $ git checkout . && git clean -xdf
    

    git权限控制

    角色 描述
    Owner Git 系统管理员
    Master Git 项目管理员
    Developer Git 项目开发人员
    Reporter Git 项目测试人员
    Guest 访客

    git用户权限一览表

    具体操作 Guest Reporter Developer Master Owner
    Create new issues ✔️ ✔️ ✔️ ✔️ ✔️
    Leave comments ✔️ ✔️ ✔️ ✔️ ✔️
    Pull the project code ✔️ ✔️ ✔️ ✔️
    Download a project ✔️ ✔️ ✔️ ✔️
    Create code snippets ✔️ ✔️ ✔️ ✔️
    Create new merge request ✔️ ✔️ ✔️
    Push changes to nonprotected branches ✔️ ✔️ ✔️
    Remove nonprotected branches ✔️ ✔️ ✔️
    Add tags ✔️ ✔️ ✔️
    Write a wiki ✔️ ✔️ ✔️
    Manage the issue trcker ✔️ ✔️ ✔️
    Add new team members ✔️ ✔️
    Push changes to protected branches ✔️ ✔️
    Manage the branch protection ✔️ ✔️
    Manage Git tags ✔️ ✔️
    Edit the project ✔️ ✔️
    Add deploy key to the peoject ✔️ ✔️
    Congigure the project hooks ✔️ ✔️
  • 相关阅读:
    洛谷P3406 海底高铁[差分 贪心]
    POJ3398Perfect Service[树形DP 树的最大独立集变形]
    POJ3928Ping pong[树状数组 仿逆序对]
    UVALive
    UVALive
    http协议进阶(二)URL与资源
    http协议进阶(一)HTTP概述
    http协议基础(十)实体首部字段
    http协议基础(九)响应首部字段
    http协议基础(八)请求首部字段
  • 原文地址:https://www.cnblogs.com/zooqkl/p/11177540.html
Copyright © 2011-2022 走看看