git 保存当前修改, 切换分支, git 新建分支并提交到远程

Git工具使用

  • 如果开发着业务, 另一个分支有bug要改, 怎么办:

    1. 如果当前分支上有修改但是没有提交, 切换到其他分支就会报错: error: Your local changes to the following files would be overwritten by checkout
    2. 然后会有提示: Please commit your changes or stash them before you switch branches
    • **解决方法**也给了:
      1. 方法一: 直接将现在修改的内容add、commit一下,改完其他分支的bug后, 切回来就可以了.
      2. 方法二: 先把当前的任务 git stash save ‘#save info’ , 改完其他分支的bug后, git stash pop, 就可以了.
        1. 多次 git stash 后, 可以通过 git stash list 查看被stash的记录列表
        2. 多次git stash, 最后一次stash的在栈顶, 就是一个栈结构.
          在这里插入图片描述
        3. 然后, git stash pop stash@{0} 就会恢复最近一次stash的内容. git stash pop stash@{1} 就会恢复最近第二次stash的内容. 根据根据内容需要进行pop
  • 本地新建分支

    • git checkout [branch_name] 本地新建一个分支
    • git checkout -b [branch_name] 本地新建一个分支, 并自动切换到该分支上
  • 推送新建立的分支到远程

    • git push [remote_origin] [remote_branch]
    • 或者分为两步走:
      • 将本地的这个分支和远端的建立联系: git push --set-upstream [remote_origin] [remote_branch 这里远端没有, 这次相当于新建] 例如: git push --set-upstream origin new_code
      • 直接推送本地到远端, 会自动根据联系找到远端分支: git push
    • git push origin master: 如果远程分支被省略,如上则表示将本地分支推送到与之存在追踪关系的远程分支(通常两者同名),如果该远程分支不存在,则会被新建

猜你喜欢

转载自blog.csdn.net/weixin_40944062/article/details/113884421