如何在Git中正确添加更改

时间:2014-09-11 13:54:45

标签: git commit

我对如何安全地将您的更改添加到Git感到有些困惑。我的程序如下。如果错误或需要修改,请告诉我:

  • 更新本地计算机中的代码
    • git pull& git reset
  • 建立一个新的分支
    • git branch XYZ& git checkout XYZ
  • 进行更改,然后添加到分支分段
    • git add * .txt& git commit - m" XYZ"
  • 返回本地计算机上的master分支
    • git checkout master
  • 因为远程仓库中的代码可能自上次拉动后发生变化,因此您的主人不是最新的:
    • git pull& git reset
  • 将最新的母版与您的分支合并
    • git merge XYZ
  • 将您的更改提交给远程仓库
    • git pull

毕竟我需要创建任何分支吗?那这个呢:  (1)更新本地计算机中的代码     * git pull  (2)进行更改,然后添加到分支分段     * git add * .txt& git commit - m" XYZ"  (3)因为远程仓库中的代码可能自上次拉动后发生了变化,因此您的主机不是最新的:     * git pull  (4)将最新的主人与你的分支合并     * git merge  (5)将您的更改提交给远程仓库     * git pull

2 个答案:

答案 0 :(得分:1)

关闭,但有一些命令你正在做不必要的事情 - 比如重置。从您描述的工作流程来看,这通常是您要做的事情。

// this part creates a new branch from the most updated master
git fetch origin
git checkout master
git pull origin master
git checkout -b XYZ // creates a new branch XYZ

// do your work

// this part adds your files and commits to your XYZ branch
git add *file* // I recommend that you add files one at a time
git commit -m "My Commit Message" // the message doesn't have to have a connection to branch XYZ

// before you push up to origin again make sure your XYZ branch has the latest master code
git fetch origin
git pull origin master // fix any merge conflicts after this step

// push your XYZ branch up to origin
git push origin XYZ

// at this point there is usually some type of peer code review or QA process before any new changes are merged into master.
// Assuming, the branch and its changes are approved then you would merge to master like so:

git fetch origin
git checkout master
git pull origin master //sync master again in case there have been new changes
git merge XYZ
git push origin master

有人可能会说git fetch origin命令对于此工作流程来说是多余的,但我认为让远程跟踪分支保持最新状态总是好的。

答案 1 :(得分:1)

这似乎非常过分,我不确定你为什么每次reset

我遵循的一般步骤是:

  • checkout分支我想继续(pull来确保它是最新的)
  • 进行我想要的更改
  • 如果远程分支在我工作时发生了变化,pull并合并任何更改
  • add更改为本地索引
  • commit本地更改
  • push更改为远程仓库