Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace one local branch with another local branch... and preserving tracking

I have 2 local branches that are tracking different repositories:

  • local-live ( tracks our live repo )
  • local-staging ( tracks a staging repo )

We have "continous deployment" set up so when I push my local-staging up staging environment will be updated with the changes. I want staging to reflect the code that's on live( which isn't the case currenly).

How might I replace my "local-staging" branch with code from my "local-live" branch? I want to wipe all staging changes making staging reflect what's on live. I want my local-staging to still be tracking the staging repo( ie "git push" from staging will act as expected )

Hope this is clear. Thanks.

like image 941
RayLoveless Avatar asked Jul 21 '15 17:07

RayLoveless


People also ask

How do you set a branch to track another branch?

When you're publishing a local branch. You can tell Git to track the newly created remote branch simply by using the -u flag with "git push".

How do you push uncommitted changes from one branch to another?

Create a new feature branch, say feature, and then switch to that branch. Implement the feature and commit it to our local repository. Push to the feature branch to the remote repository and create a pull request. After other teammate's review, the new change can be merged into the master or release branch.


2 Answers

you want to do a hard reset.

git checkout local-staging
git reset --hard local-live
like image 194
Pankaj Singhal Avatar answered Oct 17 '22 06:10

Pankaj Singhal


Seems like you want to merge your live branch into your staging branch.

Here is a detailed guide to do that:

http://www.git-tower.com/learn/git/ebook/command-line/branching-merging/merging

Basically you have to do this:

$ git checkout local-staging
$ git merge local-live


Now, if you wanna put the changes of your live branch on top of staging branch you should use rebase. And do something like:
$ git checkout local-live
$ git rebase local-staging
$ git checkout local-staging
$ git merge local-live

Check this:

https://git-scm.com/book/en/v2/Git-Branching-Rebasing


Now, if you don't want to merge the branches you shoud do a destructive operation :O

You can do a "hard reset" and a "force push".

$ git checkout local-staging
$ git reset --hard <desired commit of local-live>
$ git push --force origin local-staging

More info:

https://es.atlassian.com/git/tutorials/undoing-changes/git-reset

like image 21
mayo Avatar answered Oct 17 '22 05:10

mayo