github操作:代码提交、编辑.gitignore文件、创建新分支、分支合并、合并到主分支

1. 普通的操作流程
① 从github上clone代码到本地
  • clone远程仓库到本地,不指定分支:

    git clone https://github.com/sunriseLe/MavenHadoopPlugin.git
    
  • clone远程仓库到本地,使用-b branchName指定具体分支:

    git clone -b test https://github.com/sunriseLe/MavenHadoopPlugin.git
    
② 本地修改代码后提交到远程仓库(github)
  • 添加修改,一般都是添加所有的修改:

    git add .
    
  • 将暂存区里的改动给提交到本地的版本库,可以将提交信息改为自己想要添加的说明信息:

    git commit -m "提交信息"
    
  • 将本地版本库的分支推送到远程服务器上对应的分支,这时会需要输入你的github账号和密码:

    $ git push -u origin master
    Username for 'https://github.com': sunriseLe 
    Password for 'https://sunriseLe@github.com': 
    ...
    

参考链接:
使用git将项目上传到github(最简单方法)

2. github进阶操作
① 编写.gitignore文件,忽略某些文件
  • 注意: 如果你想忽略的文件之前已经提交过了,说明这些文件已经被纳入了版本管理中了。而.gitignore只能忽略那些原来没有被track的文件,如果某些文件已经被纳入了版本管理中,则修改.gitignore是无效的。这时需要先把本地缓存删除(改变成未track状态),然后再提交。

  • 使用如下命令删除本地缓存,其中ignoreFile替换为自己不想上传的某些。比如,这是一个idea生成的工程,可以忽略.idea文件夹。

    git rm -r --cached ignoreFile
    
  • 编辑.gitignore文件,在文件中填写自己想要忽略的内容:

    gedit .gitignore
    
  • 我想要忽略已经提交过的readme.md文件,通过git rm -r --cached readme.md先删除缓存,然后.gitignore的文件内容如下:

    readme.md
    
  • 执行以下命令,重新提交。会发现github中没有了readme.md文件,多了我们刚刚添加的.gitignore文件。

    git add .
    git commit -m "ignore readme.md"
    git push -u origin master
    
  • 整个流程截图
    在这里插入图片描述
    参考链接:
    Git 忽略提交 .gitignore

② 新分支的创建
  • 创建新的分支:

    $ git checkout -b dev
    切换到一个新分支 'dev'
    
  • git checkout -b相当与先创建新分支,再切换到新分支:

    $ git branch dev
    $ git checkout dev
    
  • 查看当前分支,带*号的分支表示当前分支,对本地库所做的修改都会显示在当前分支上。

    $ git branch
     * dev
      master
      test
    
  • 对新分支的进行改动后,可以按照上面的方法提交改动,具体命令如下:

    git add .
    git commit -m "dev branch add readme.md"
    git push -u origin dev
    
  • 参考链接:
    创建与合并分支

③ 拉取远程分支与本地分支合并
  • 先使用git add .添加本地修改。

  • 通过git pull命令取回远程主机某个分支的更新,再与本地的指定分支合并:

    $ git pull https://github.com/sunriseLe/MavenHadoopPlugin.git dev
    
  • 已经git push了,提示文件冲突。解决办法,参考链接:git pull 时发生冲突之解决合并冲突

  • 自己的过程记录:

    $ git add .
    
    $ git commit -m "add log to show add tx history to db"
    [bft-test e08d6ef] add log to show add tx history to db
     1 file changed, 4 insertions(+), 2 deletions(-)
    
    $ git push -u origin bft-test
    Username for 'https://github.com': sunriseLe
    Password for 'https://sunriseLe@github.com': 
    To https://github.com/...
     ! [rejected]        bft-test -> bft-test (non-fast-forward)
    error: 推送一些引用到 'https://github.com/...' 失败
    提示:更新被拒绝,因为您当前分支的最新提交落后于其对应的远程分支。
    提示:再次推送前,先与远程变更合并(如 'git pull ...')。详见
    提示:'git push --help' 中的 'Note about fast-forwards' 小节。
    
    $ git pull https://github.com/... bft-test
    Username for 'https://github.com': sunriseLe
    Password for 'https://sunriseLe@github.com': 
    来自 https://github.com/...
     * branch            bft-test   -> FETCH_HEAD
    自动合并 peer/db/leveldb.go
    Merge made by the 'recursive' strategy.
     peer/channel/config.go | 3 +++
     peer/db/leveldb.go     | 8 ++++++--
     2 files changed, 9 insertions(+), 2 deletions(-)
    
    $ git push -u origin bft-test
    Username for 'https://github.com': sunriseLe
    Password for 'https://sunriseLe@github.com': 
    ...
    分支 'bft-test' 设置为跟踪来自 'origin' 的远程分支 'bft-test'
  • 技术流师兄教我的方法:

    git fetch origin
    git merge origin/xxx  # xxx表示自己想要合并的远端分支,比如bft
    # 遇到冲突,解决冲突
    git commit
    git push
    

参考链接:
git pull 命令

④ 提交了错误的commit
  • 原本自己在提交之前,应该git pull的,结果直接到了push这一步,提示有冲突。一下子方了,不知道为啥,做了2次多余的commit。

  • 现在想取消这两次多余的commit,命令如下。注意:git push -f不到万不得已不要轻易使用!

    # git reset --hard会彻底回退到某个版本, 本地的源码也会变为上一个版本的内容
    $ git reset --hard 970f341223f34136cd715d667a9d19910a5e2e74
    HEAD 现在位于 970f341 update peer3's config to solve read dirty data; add some log to find the reason of config's tx repeating
    $ git push -f -u origin bft-test
    Username for 'https://github.com': sunriseLe
    Password for 'https://sunriseLe@github.com': 
    总共 0 (差异 0),复用 0 (差异 0)
    To https://github.com/zebra-uestc/madledger.git
     + 519c1ce...970f341 bft-test -> bft-test (forced update)
    分支 'bft-test' 设置为跟踪来自 'origin' 的远程分支 'bft-test'
⑤ 合并当前分支到主分支
  • 目前我已经在自己的raft分支实现了某些功能,也编写了测试用例确定没有bug了,于是我想将自己的raft分支合并到master分支。
  • 这时需要使用git merge命令,将当前分支合并到主分支。方法如下:
  1. 首先,切换到mster分支。

    $ git checkout master
    切换到分支 'master'
    您的分支与上游分支 'origin/master' 一致。
    
  2. 进行分支合并:

    $ git merge raft
    更新 109e1c94..414997fc
    Fast-forward
    .... # 内容省略
    
  • 在切换到master分支时,遇到了本地文件的一些小改动,我没有提交。这些改动一般都是改了配置信息什么的,我不想进行提交。因此之前,并没有使用git add fileName将他进行添加。

    $ git checkout master
    error: 您对下列文件的本地修改将被检出操作覆盖:
    	consensus/raft/db_test.go
    请在切换分支前提交或贮藏您的修改。
    终止中
    
  • 使用git status查看,发现确实有些本地文件修改没有提交或取消:

    $ git status
    位于分支 raft
    您的分支与上游分支 'origin/raft' 一致。
    
    尚未暂存以备提交的变更:
      (使用 "git add <文件>..." 更新要提交的内容)
      (使用 "git checkout -- <文件>..." 丢弃工作区的改动)
    
    	修改:     consensus/raft/db_test.go
    
  • 通过git checkout -- fileName丢弃这些改动,然后在进行分支切换,成功切换到主分支!

    $ git checkout -- consensus/raft/db_test.go
    
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值