![]() This step is optional, along with 2), when want to trash the changes in local commit. ![]() Now all the changes are safe and there's no divergence anymore.Ĥ) git stash pop or git stash apply to apply the last stashed changes, followed by a new commit, if wanted. For the next steps, I've used the interface in SourceTree, but I think the following commands should also work:Ģ) git stash to stash changes from 1). So here are all the steps that solved my situation (I had 1 local commit and 8 commits in the remote):ġ) git reset -soft HEAD~1 to undo local commit. If there is more than 1 commit, a variation of HEAD~x should work. The key is using -soft, instead of the harsh -hard. I think this is a more versatile solution than pull with rebase, because the local commit can be reviewed and even moved to another branch. I know there are plenty of answers here, but I think git reset -soft HEAD~1 deserves some attention, because it let you keep changes in the last local (not pushed) commit while solving the diverged state. Git reset -hard origin/my_remote_tracking_branch Now if you are reading this because you already are in a "diverged" scenario due to such rebase, you can get back to the last commit from origin (i.e. not yet pushed to any remote) you should definitely do a rebase (and your branch will not diverge in this case). On the other hand, if your branch is only a local branch (i.e. See this blog for a much more detailed explanation. But unless you are up for "un-diverging" branches, this will be a much smoother workflow than rebasing. ![]() Yes, you'll end up with extra commits on your branch. STOP NOW and save yourself some trouble! Instead, use merge as: Nothing to commit (working directory clean)Īnd now you are trying to rebase from master as: Let's say you are on branch my_remote_tracking_branch, which was branched from main In this scenario if you try to rebase, you'll most likely find your branch diverged and it can create a mess that isn't for git nubees! I found myself in this situation when I tried to rebase a branch that was tracking a remote branch, and I was trying to rebase it on main. This combines the above fetch and rebase steps into one command. The git pull command provides a shorthand way to fetch from origin and rebase local work on it: $ git pull -rebase This approach preserves the CVS-based workflow used previously and may ease the transition.Īn attempt to push C' into our repository will work (assuming you have permissions and no one has pushed while you were rebasing). We have chosen (for now) to allow only linear history in /cmake.git. Note that the history behind C' is still linear. Its content accounts for changes in both B and C it is the same as M from the merge example. ![]() It has a different history: B instead of A.Git just adds explicit separation between the commit and rebase steps.Ĭommit C' is a new commit created by the git rebase command. This tells Git to replay commit C (your work) as if you had based it on commit B instead of A.ĬVS and Subversion users routinely rebase their local changes on top of upstream work when they update before commit. Use the git rebase command: $ git rebase origin/main Note that the history behind M is now non-linear. The new merge, commit M, has two parents, each representing one path of development that led to the content stored in that commit. o - o - A - B origin/main (upstream work) The graph of history now looks like this. This tells Git to integrate the changes from origin/main into your work and create a merge commit. Use the git merge command: $ git merge origin/main See Pro Git: Git Branching - Rebasing for details. However, before you tried to push back to origin, someone else pushed the commit B.ĭevelopment history has diverged into separate paths. You based commit C on commit A because that was the latest work you had fetched from upstream at the time. If origin is up-to-date, then some commits have been pushed to origin from another repo while you made your own commits locally. "Your branch and 'origin/main' have diverged, # and have 1 and 1 different commit(s) each, respectively." The rest of the answer reflects that more recent convention. Note: since Git 2.28 (Q3 2020), the default branch is configurable, and now (2021+) set to main, no longer master. You can review the differences with a: git log HEAD.origin/mainīefore pulling it (fetch + merge) (see also "How do you get git to always pull from a specific branch?")
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |