- 问题描述:对本地代码修改后,已经完成
git add
,git commit
后,使用git push
提示远程已被修改,需要pull下来。 - 解决问题:需要先
git pull
,获取到绑定的远程分支的最新commit,git pull
会自动merge远程分支到本地当前分支,如果出现冲突,会提示fix conflict的地方,修改后,重新git add,git commit,git push。这样就解决了。 - Git fetch和git pull的区别在于:
git fetch拉去远程分支的数据到本地仓库,并不会自动合并本地分支的commit和远程分支,我们需要自己手动合并。
git pull,自动获取远程分支的最新commit并且合并到本地仓库。要注意获取的远程分支是当前本地分支追踪的远程分支,如果是使用git clone创建的仓库默认是远程master分支。当然我们在创建branch后通过git branch --set-upstream-to=origin/remote_branch your_branch
,这样做法可以避免每次在git push时候指定远程关联的分支。
来自Git Pro:
关于git fetch
git fetch command pulls the data to your local repository—it doesn’t automatically merge it with any of your work or modify what you’re currently working on. You have to merge it manually into your work when you’re ready.
关于git pull
If you have a branch set up to track a remote branch (see the next section and Chapter 3 for more information), you can use the git pull command to automatically fetch and then merge a remote branch into your current branch.