i had to delete the tracking branch as well
git branch -d -r origin/develop
Answer from webstrap on Stack Overflowi had to delete the tracking branch as well
git branch -d -r origin/develop
You can not pull because it's an other branch now.
Maybe you should start from scratch and fetch the remote develop branch as a new local branch!
git checkout -b new_develop --track origin/develop
After doing this, you can merge or change between branches on your local machine. Compare directory trees and other files.
If you are done editing, just remove your local develop branch, and rename the new_develop to develop.
git - how to reset develop branch to master - Stack Overflow
recreate my master branch in git - Stack Overflow
merge - How to replace master branch in Git, entirely, from another branch? - Stack Overflow
Can I destroy and recreate a Git remote branch in one command? - Stack Overflow
If you want all changes from master in dev_branch, then:
git switch dev_branch
git reset --hard master
If you have dev_branch pushed to a remote already, you have to do:
git push --force
To force-push the branch to the remote. Warning: This will break the history of the branch for people who cloned it before! Then, other people will have to do a git pull --rebase on the dev_branch to get the changes.
You can also rename the dev branch to something old and then make a new branch from master with the same name:
git branch -m dev_branch old_dev_branch
git branch -m master dev_branch
Or, use the ours strategy — not sure why it wouldn't work for you:
git checkout master
git merge -s ours dev_branch
git checkout dev_branch
git merge master
Why do that? You can delete the branch if it isn't needed anymore (But why? Branches cost next to nothing.). Or you can rename it:
git branch -m dev_branch obsolete_dev
Or you could do this to delete it:
git branch -D dev_branch
Now create a new branch off master (assuming you are on it):
git branch dev_branch
See git branch --help for further options (setting up remotes and all that jazz).
If you now have new branches, you'll have to synchronize with any peer repositories.
Best way to avoid hassle: Have an "active" development branch, if it goes stale, abandon it and create a new one. No history lost that way (could prove crucial sometime).
Have e.g. a branch for each major version, develop on branches off those to fix version bugs, master forges ahead. Use cherry-pick and perhaps merges to port fixes to older versions.
If you want the two branches to be the same then
// from Develop and assuming your master is up to date with origin/master
git reset --hard master
If you want to make develop be identical to master, the simplest way is just to recreate the pointer:
git branch -f develop master
Or, if you already have develop checked out:
git reset --hard master
Note however that both of these options will get rid of any history that develop had which wasn't in master. If that isn't okay, you could preserve it by instead creating a commit that mirrored master's latest state:
git checkout develop
git merge --no-commit master
git checkout --theirs master .
git commit
You should be able to use the “ours” merge strategy to overwrite master with seotweaks like this:
git checkout master
git pull
git checkout seotweaks
git merge -s ours master
git checkout master
git merge seotweaks
The first two steps are a useful precaution to ensure your local copy of master is up-to-date. The result should be that your master is now essentially seotweaks.
(-s ours is short for --strategy=ours)
From the docs about the 'ours' strategy:
This resolves any number of heads, but the resulting tree of the merge is always that of the current branch head, effectively ignoring all changes from all other branches. It is meant to be used to supersede old development history of side branches. Note that this is different from the -Xours option to the recursive merge strategy.
Update from comments: If you get fatal: refusing to merge unrelated histories, then change the fourth line to this: git merge --allow-unrelated-histories -s ours master
What about using git branch -m to rename the master branch to another one, then rename seotweaks branch to master? Something like this:
git branch -m master old-master
git branch -m seotweaks master
git push -f origin master
This might remove commits in origin master, please check your origin master before running git push -f origin master.
If you are allowed to rewrite the remote branch, you can use git push --force my_remote my_branch.
That's right, git push -f is appropriate in the example.
The reason being there's no need to re-create the entire branch, since all the commits pre-rebase remain the same.
We only need to overwrite our new rebased local commits to our remote repo that contains the non-rebased stale commits.
In my similar use case, git push -f wasn't viable as my remote repo got removed by mistake, so I created a git-create shell script in my PATH that let me easily:
git create my_branch
git push -f
Git supports this command:
git checkout -B master origin/master
Check out the origin/master branch and then reset master branch there.
UPDATE:
Or you can use new switch command for that
git switch -C master origin/master
As KindDragon's answer mentions, you can recreate master directly at origin/master with:
git checkout -B master origin/master
The git checkout man page mentions:
If -B is given, <new_branch> is created if it doesn’t exist; otherwise, it is reset. This is the transactional equivalent of
$ git branch -f <branch> [<start point>]
$ git checkout <branch>
Since Git 2.23+ (August 2019), since git checkout is too confusing, the new (still experimental) command is git switch:
git switch -C master origin/master
That is:
-C <new-branch> --force-create <new-branch>Similar to
--createexcept that if<new-branch>already exists, it will be reset to<start-point>.
This is a convenient shortcut for:$ git branch -f <new-branch> $ git switch <new-branch>
Originally suggested:
Something like:
$ git checkout master
# remember where the master was referencing to
$ git branch previous_master
# Reset master back to origin/master
$ git reset --hard origin/master
with step 2 being optional.
Well, first, branches don't really get deleted. Only branch names get deleted. But this gets us into a thorny question: What exactly do we mean by "branch"?
Let's take a fast look at the process of merging. We start with a series of commits in the commit graph (or "DAG"; see the linked question) that look like this:
...--o--*-----o <-- master
\
o--o--o--o--o <-- feature
We then run:
git checkout master
git merge feature
which somehow figures out what we've changed in both master and feature since the last time we merged them (which was actually never, but they were together at one time, at the point marked * here). Git then makes a new merge commit that points back to both of these branch tip commits:
...--o--*-----o---------o <-- master
\ /
o--o--o--o--o <-- feature
and we have "a merge": a commit, of type merge commit.
We can now erase the word feature and the arrow, i.e., remove the name. The graph remains intact, retained by the name master:
...--o--*-----o---------M <-- master
\ /
o--o--o--o--F
Should we wish to see what went into master via feature, all we have to do is find the commit I have labeled F (for Feature) here. Note that I have also labeled the merge commit M (for merge).
The way to find it is to start from master and work backwards until we find M. (It's right there at the tip of master right now, although later, it will be some number of steps back from the tip.) Then, we simply look at the second parent commit of M.
To find the second parent of a commit whose hash ID we know, we just tell Git: tell me the hash ID of the second parent of this other hash ID. The easy way to do this is with git rev-parse. Let's say the hash ID of M is badf00d:
git rev-parse badf00d^2
Git spits out the full hash ID of F. The hat-two suffix means "second parent" (hat-one, or just hat by itself, means "first parent").
Now we may also want to find commit *. That's the merge base of the commit that is the first parent of M, and this particular commit F that we just found. To find the merge base of two commits, we ask Git:
git merge-base badf00d^1 badf00d^2
We can then look at every commit in the range starting just after the merge base * and going up through and including commit F, using git log or git format-patch or whatever.
We can do this with the raw hashes, or we can point names (temporary or permanent, they will live exactly as long as you like) to commits M, F, and/or *, using git branch or git tag. Each name remembers the hash ID for you. The chief difference between a tag name and a branch name is that if you git checkout a tag name, you get a "detached HEAD" and are not on a branch, but if you git checkout a branch name, you get on that branch, and if you make new commits, they will cause that branch to advance:
$ git branch newname <hash-ID-of-commit-F>
...--o--*-----o---------M--o--o--o <-- master
\ /
o--o--o--o--F <-- newname
$ git checkout newname
... hack away ...
$ git commit ...
...--o--*-----o---------M--o--o--o <-- master
\ /
o--o--o--o--F--o <-- newname (HEAD)
This is all that branches are, in Git: the names just point to commits, while the branch structure, the history or DAGlet or whatever name you want, is formed by the permanent parts of the commit DAG. Branch names have the special feature that you can git checkout them and make them advance by running git commit.
Let's assume the following simplified history: The feature branch was based on commit N and contained the commits P and Q. In the meantime, commit O was added to the master branch. The feature branch was merged in commit R. After that, commits S and T were created, which is where master is now.
M -- N -- O -- R -- S -- T [master]
\ /
P --- Q
You want to find commit Q. I don't think there is a programmatic way to do this, so you need to use any repository browser, for example gitk.
Once you have found Q, you can easily recreate the feature branch:
$ git branch my_feature_branch <hash of commit Q>
I'm not sure if you are aware of this, but a key to understanding this is knowing that a branch is not much more than a "pointer" or "bookmark" to a commit.
I've made some commits B, C, D to a master branch after commit A but later realized that this is a bigger change deserving a new branch.
So I checked out A and created branch "new" there. But of course the commits B, C, D are still part of "master", not "new." I know rebase is probably my friend here but I don't know how to use it in this scenario.