Overwriting my local branch with remote branch
I have completely fubar'd my local branch, and would like to start over. The version on the server is correct.
I don't want to start over, I would like to use my local history to fix my huge screwup. (I can if I have to.)
git fetch branchname
, and git pull branchname
don't work. The message I get is "up to date" however, my local version does not match that of the server.
git pull origin/branchname
gives me a "not found" error.
git
add a comment |
I have completely fubar'd my local branch, and would like to start over. The version on the server is correct.
I don't want to start over, I would like to use my local history to fix my huge screwup. (I can if I have to.)
git fetch branchname
, and git pull branchname
don't work. The message I get is "up to date" however, my local version does not match that of the server.
git pull origin/branchname
gives me a "not found" error.
git
Possible duplicate of Reset local repository branch to be just like remote repository HEAD
– Michael Freidgeim
Apr 1 '16 at 2:42
add a comment |
I have completely fubar'd my local branch, and would like to start over. The version on the server is correct.
I don't want to start over, I would like to use my local history to fix my huge screwup. (I can if I have to.)
git fetch branchname
, and git pull branchname
don't work. The message I get is "up to date" however, my local version does not match that of the server.
git pull origin/branchname
gives me a "not found" error.
git
I have completely fubar'd my local branch, and would like to start over. The version on the server is correct.
I don't want to start over, I would like to use my local history to fix my huge screwup. (I can if I have to.)
git fetch branchname
, and git pull branchname
don't work. The message I get is "up to date" however, my local version does not match that of the server.
git pull origin/branchname
gives me a "not found" error.
git
git
edited Nov 6 '17 at 7:55
Jeroen
35.6k23122195
35.6k23122195
asked Jun 3 '11 at 16:12
Sara ChippsSara Chipps
5,77494996
5,77494996
Possible duplicate of Reset local repository branch to be just like remote repository HEAD
– Michael Freidgeim
Apr 1 '16 at 2:42
add a comment |
Possible duplicate of Reset local repository branch to be just like remote repository HEAD
– Michael Freidgeim
Apr 1 '16 at 2:42
Possible duplicate of Reset local repository branch to be just like remote repository HEAD
– Michael Freidgeim
Apr 1 '16 at 2:42
Possible duplicate of Reset local repository branch to be just like remote repository HEAD
– Michael Freidgeim
Apr 1 '16 at 2:42
add a comment |
4 Answers
4
active
oldest
votes
first, create a new branch in the current position (in case you need your old 'screwed up' history):
git branch fubar-pin
update your list of remote branches and sync new commits:
git fetch --all
then, reset your branch to the point where origin/branch points to:
git reset --hard origin/branch
be careful, this will remove any changes from your working tree!
2
+1 but you might want to add a reminder to dogit fetch origin
before the reset
– Mark Longair
Jun 3 '11 at 16:22
I did this with one slight change and it didn't work: git fetch --all, git reset --hard SHA1OFANOLDCOMMIT, (some other stuff), git reset --hard origin/branch. The end result was that I was still at the old commit. So, this approach may work in some cases, but I think it doesn't work in all.
– greggles
Oct 18 '12 at 15:56
@greggles: Any errors? After the last command, HEAD must point at origin/branch.
– knittl
Oct 18 '12 at 18:10
fyi, i did this to revert the master branch and it cleared commits i had in other branches. luckily i thought to copy my web folder before doing this.
– Gavin
Oct 10 '13 at 21:41
1
@Gavin: no, this will under no circumstances affect other branches thanorigin/branch
. Never.
– knittl
Oct 11 '13 at 9:23
|
show 2 more comments
What I do when I mess up my local branch is I just rename my broken branch, and check out/branch the upstream branch again:
git branch -m branch branch-old
git fetch remote
git checkout -b branch remote/branch
Then if you're sure you don't want anything from your old branch, remove it:
git branch -D branch-old
But usually I leave the old branch around locally, just in case I had something in there.
4
This seems like the best answer. It helps create a backup copy just in case and seems very likely to result in the local branch being an exact copy of the remote one.
– greggles
Oct 18 '12 at 15:59
Great answer, useful to me. One Q: The official git-checkout documentation seems to say that your 3rd command should be:git checkout -b <branch> --track <remote>/<branch>
Does yours work equally well, without the--track
?
– Starman
Dec 12 '17 at 19:47
1
I think the config varbranch.autoSetupMerge
(which I think defaults to true) makes the--track
implicit. And, yes, in all my git setups I don't need to explicitly--track
when doing acheckout -b
, but YMMV.
– Casey Marshall
Dec 13 '17 at 20:54
add a comment |
Your local branch likely has modifications to it you want to discard. To do this, you'll need to use git reset
to reset the branch head to the last spot that you diverged from the upstream repo's branch. Use git branch -v
to find the sha1 id of the upstream branch, and reset your branch it it using git reset SHA1ID
. Then you should be able to do a git checkout
to discard the changes it left in your directory.
Note: always do this on a backed-up repo. That way you can assure you're self it worked right. Or if it didn't, you have a backup to revert to.
This feels like it would probably work, but given the simplicity and reliability of the approach of "make a copy of your work somewhere else, make a fresh copy of the remote branch" I fail to see how this is better.
– greggles
Oct 18 '12 at 15:55
add a comment |
git reset --hard
This is to revert all your local changes to the origin head
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6229764%2foverwriting-my-local-branch-with-remote-branch%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
first, create a new branch in the current position (in case you need your old 'screwed up' history):
git branch fubar-pin
update your list of remote branches and sync new commits:
git fetch --all
then, reset your branch to the point where origin/branch points to:
git reset --hard origin/branch
be careful, this will remove any changes from your working tree!
2
+1 but you might want to add a reminder to dogit fetch origin
before the reset
– Mark Longair
Jun 3 '11 at 16:22
I did this with one slight change and it didn't work: git fetch --all, git reset --hard SHA1OFANOLDCOMMIT, (some other stuff), git reset --hard origin/branch. The end result was that I was still at the old commit. So, this approach may work in some cases, but I think it doesn't work in all.
– greggles
Oct 18 '12 at 15:56
@greggles: Any errors? After the last command, HEAD must point at origin/branch.
– knittl
Oct 18 '12 at 18:10
fyi, i did this to revert the master branch and it cleared commits i had in other branches. luckily i thought to copy my web folder before doing this.
– Gavin
Oct 10 '13 at 21:41
1
@Gavin: no, this will under no circumstances affect other branches thanorigin/branch
. Never.
– knittl
Oct 11 '13 at 9:23
|
show 2 more comments
first, create a new branch in the current position (in case you need your old 'screwed up' history):
git branch fubar-pin
update your list of remote branches and sync new commits:
git fetch --all
then, reset your branch to the point where origin/branch points to:
git reset --hard origin/branch
be careful, this will remove any changes from your working tree!
2
+1 but you might want to add a reminder to dogit fetch origin
before the reset
– Mark Longair
Jun 3 '11 at 16:22
I did this with one slight change and it didn't work: git fetch --all, git reset --hard SHA1OFANOLDCOMMIT, (some other stuff), git reset --hard origin/branch. The end result was that I was still at the old commit. So, this approach may work in some cases, but I think it doesn't work in all.
– greggles
Oct 18 '12 at 15:56
@greggles: Any errors? After the last command, HEAD must point at origin/branch.
– knittl
Oct 18 '12 at 18:10
fyi, i did this to revert the master branch and it cleared commits i had in other branches. luckily i thought to copy my web folder before doing this.
– Gavin
Oct 10 '13 at 21:41
1
@Gavin: no, this will under no circumstances affect other branches thanorigin/branch
. Never.
– knittl
Oct 11 '13 at 9:23
|
show 2 more comments
first, create a new branch in the current position (in case you need your old 'screwed up' history):
git branch fubar-pin
update your list of remote branches and sync new commits:
git fetch --all
then, reset your branch to the point where origin/branch points to:
git reset --hard origin/branch
be careful, this will remove any changes from your working tree!
first, create a new branch in the current position (in case you need your old 'screwed up' history):
git branch fubar-pin
update your list of remote branches and sync new commits:
git fetch --all
then, reset your branch to the point where origin/branch points to:
git reset --hard origin/branch
be careful, this will remove any changes from your working tree!
edited Dec 2 '13 at 19:24
cregox
10.8k75998
10.8k75998
answered Jun 3 '11 at 16:20
knittlknittl
150k39227279
150k39227279
2
+1 but you might want to add a reminder to dogit fetch origin
before the reset
– Mark Longair
Jun 3 '11 at 16:22
I did this with one slight change and it didn't work: git fetch --all, git reset --hard SHA1OFANOLDCOMMIT, (some other stuff), git reset --hard origin/branch. The end result was that I was still at the old commit. So, this approach may work in some cases, but I think it doesn't work in all.
– greggles
Oct 18 '12 at 15:56
@greggles: Any errors? After the last command, HEAD must point at origin/branch.
– knittl
Oct 18 '12 at 18:10
fyi, i did this to revert the master branch and it cleared commits i had in other branches. luckily i thought to copy my web folder before doing this.
– Gavin
Oct 10 '13 at 21:41
1
@Gavin: no, this will under no circumstances affect other branches thanorigin/branch
. Never.
– knittl
Oct 11 '13 at 9:23
|
show 2 more comments
2
+1 but you might want to add a reminder to dogit fetch origin
before the reset
– Mark Longair
Jun 3 '11 at 16:22
I did this with one slight change and it didn't work: git fetch --all, git reset --hard SHA1OFANOLDCOMMIT, (some other stuff), git reset --hard origin/branch. The end result was that I was still at the old commit. So, this approach may work in some cases, but I think it doesn't work in all.
– greggles
Oct 18 '12 at 15:56
@greggles: Any errors? After the last command, HEAD must point at origin/branch.
– knittl
Oct 18 '12 at 18:10
fyi, i did this to revert the master branch and it cleared commits i had in other branches. luckily i thought to copy my web folder before doing this.
– Gavin
Oct 10 '13 at 21:41
1
@Gavin: no, this will under no circumstances affect other branches thanorigin/branch
. Never.
– knittl
Oct 11 '13 at 9:23
2
2
+1 but you might want to add a reminder to do
git fetch origin
before the reset– Mark Longair
Jun 3 '11 at 16:22
+1 but you might want to add a reminder to do
git fetch origin
before the reset– Mark Longair
Jun 3 '11 at 16:22
I did this with one slight change and it didn't work: git fetch --all, git reset --hard SHA1OFANOLDCOMMIT, (some other stuff), git reset --hard origin/branch. The end result was that I was still at the old commit. So, this approach may work in some cases, but I think it doesn't work in all.
– greggles
Oct 18 '12 at 15:56
I did this with one slight change and it didn't work: git fetch --all, git reset --hard SHA1OFANOLDCOMMIT, (some other stuff), git reset --hard origin/branch. The end result was that I was still at the old commit. So, this approach may work in some cases, but I think it doesn't work in all.
– greggles
Oct 18 '12 at 15:56
@greggles: Any errors? After the last command, HEAD must point at origin/branch.
– knittl
Oct 18 '12 at 18:10
@greggles: Any errors? After the last command, HEAD must point at origin/branch.
– knittl
Oct 18 '12 at 18:10
fyi, i did this to revert the master branch and it cleared commits i had in other branches. luckily i thought to copy my web folder before doing this.
– Gavin
Oct 10 '13 at 21:41
fyi, i did this to revert the master branch and it cleared commits i had in other branches. luckily i thought to copy my web folder before doing this.
– Gavin
Oct 10 '13 at 21:41
1
1
@Gavin: no, this will under no circumstances affect other branches than
origin/branch
. Never.– knittl
Oct 11 '13 at 9:23
@Gavin: no, this will under no circumstances affect other branches than
origin/branch
. Never.– knittl
Oct 11 '13 at 9:23
|
show 2 more comments
What I do when I mess up my local branch is I just rename my broken branch, and check out/branch the upstream branch again:
git branch -m branch branch-old
git fetch remote
git checkout -b branch remote/branch
Then if you're sure you don't want anything from your old branch, remove it:
git branch -D branch-old
But usually I leave the old branch around locally, just in case I had something in there.
4
This seems like the best answer. It helps create a backup copy just in case and seems very likely to result in the local branch being an exact copy of the remote one.
– greggles
Oct 18 '12 at 15:59
Great answer, useful to me. One Q: The official git-checkout documentation seems to say that your 3rd command should be:git checkout -b <branch> --track <remote>/<branch>
Does yours work equally well, without the--track
?
– Starman
Dec 12 '17 at 19:47
1
I think the config varbranch.autoSetupMerge
(which I think defaults to true) makes the--track
implicit. And, yes, in all my git setups I don't need to explicitly--track
when doing acheckout -b
, but YMMV.
– Casey Marshall
Dec 13 '17 at 20:54
add a comment |
What I do when I mess up my local branch is I just rename my broken branch, and check out/branch the upstream branch again:
git branch -m branch branch-old
git fetch remote
git checkout -b branch remote/branch
Then if you're sure you don't want anything from your old branch, remove it:
git branch -D branch-old
But usually I leave the old branch around locally, just in case I had something in there.
4
This seems like the best answer. It helps create a backup copy just in case and seems very likely to result in the local branch being an exact copy of the remote one.
– greggles
Oct 18 '12 at 15:59
Great answer, useful to me. One Q: The official git-checkout documentation seems to say that your 3rd command should be:git checkout -b <branch> --track <remote>/<branch>
Does yours work equally well, without the--track
?
– Starman
Dec 12 '17 at 19:47
1
I think the config varbranch.autoSetupMerge
(which I think defaults to true) makes the--track
implicit. And, yes, in all my git setups I don't need to explicitly--track
when doing acheckout -b
, but YMMV.
– Casey Marshall
Dec 13 '17 at 20:54
add a comment |
What I do when I mess up my local branch is I just rename my broken branch, and check out/branch the upstream branch again:
git branch -m branch branch-old
git fetch remote
git checkout -b branch remote/branch
Then if you're sure you don't want anything from your old branch, remove it:
git branch -D branch-old
But usually I leave the old branch around locally, just in case I had something in there.
What I do when I mess up my local branch is I just rename my broken branch, and check out/branch the upstream branch again:
git branch -m branch branch-old
git fetch remote
git checkout -b branch remote/branch
Then if you're sure you don't want anything from your old branch, remove it:
git branch -D branch-old
But usually I leave the old branch around locally, just in case I had something in there.
answered Sep 15 '12 at 17:55
Casey MarshallCasey Marshall
750611
750611
4
This seems like the best answer. It helps create a backup copy just in case and seems very likely to result in the local branch being an exact copy of the remote one.
– greggles
Oct 18 '12 at 15:59
Great answer, useful to me. One Q: The official git-checkout documentation seems to say that your 3rd command should be:git checkout -b <branch> --track <remote>/<branch>
Does yours work equally well, without the--track
?
– Starman
Dec 12 '17 at 19:47
1
I think the config varbranch.autoSetupMerge
(which I think defaults to true) makes the--track
implicit. And, yes, in all my git setups I don't need to explicitly--track
when doing acheckout -b
, but YMMV.
– Casey Marshall
Dec 13 '17 at 20:54
add a comment |
4
This seems like the best answer. It helps create a backup copy just in case and seems very likely to result in the local branch being an exact copy of the remote one.
– greggles
Oct 18 '12 at 15:59
Great answer, useful to me. One Q: The official git-checkout documentation seems to say that your 3rd command should be:git checkout -b <branch> --track <remote>/<branch>
Does yours work equally well, without the--track
?
– Starman
Dec 12 '17 at 19:47
1
I think the config varbranch.autoSetupMerge
(which I think defaults to true) makes the--track
implicit. And, yes, in all my git setups I don't need to explicitly--track
when doing acheckout -b
, but YMMV.
– Casey Marshall
Dec 13 '17 at 20:54
4
4
This seems like the best answer. It helps create a backup copy just in case and seems very likely to result in the local branch being an exact copy of the remote one.
– greggles
Oct 18 '12 at 15:59
This seems like the best answer. It helps create a backup copy just in case and seems very likely to result in the local branch being an exact copy of the remote one.
– greggles
Oct 18 '12 at 15:59
Great answer, useful to me. One Q: The official git-checkout documentation seems to say that your 3rd command should be:
git checkout -b <branch> --track <remote>/<branch>
Does yours work equally well, without the --track
?– Starman
Dec 12 '17 at 19:47
Great answer, useful to me. One Q: The official git-checkout documentation seems to say that your 3rd command should be:
git checkout -b <branch> --track <remote>/<branch>
Does yours work equally well, without the --track
?– Starman
Dec 12 '17 at 19:47
1
1
I think the config var
branch.autoSetupMerge
(which I think defaults to true) makes the --track
implicit. And, yes, in all my git setups I don't need to explicitly --track
when doing a checkout -b
, but YMMV.– Casey Marshall
Dec 13 '17 at 20:54
I think the config var
branch.autoSetupMerge
(which I think defaults to true) makes the --track
implicit. And, yes, in all my git setups I don't need to explicitly --track
when doing a checkout -b
, but YMMV.– Casey Marshall
Dec 13 '17 at 20:54
add a comment |
Your local branch likely has modifications to it you want to discard. To do this, you'll need to use git reset
to reset the branch head to the last spot that you diverged from the upstream repo's branch. Use git branch -v
to find the sha1 id of the upstream branch, and reset your branch it it using git reset SHA1ID
. Then you should be able to do a git checkout
to discard the changes it left in your directory.
Note: always do this on a backed-up repo. That way you can assure you're self it worked right. Or if it didn't, you have a backup to revert to.
This feels like it would probably work, but given the simplicity and reliability of the approach of "make a copy of your work somewhere else, make a fresh copy of the remote branch" I fail to see how this is better.
– greggles
Oct 18 '12 at 15:55
add a comment |
Your local branch likely has modifications to it you want to discard. To do this, you'll need to use git reset
to reset the branch head to the last spot that you diverged from the upstream repo's branch. Use git branch -v
to find the sha1 id of the upstream branch, and reset your branch it it using git reset SHA1ID
. Then you should be able to do a git checkout
to discard the changes it left in your directory.
Note: always do this on a backed-up repo. That way you can assure you're self it worked right. Or if it didn't, you have a backup to revert to.
This feels like it would probably work, but given the simplicity and reliability of the approach of "make a copy of your work somewhere else, make a fresh copy of the remote branch" I fail to see how this is better.
– greggles
Oct 18 '12 at 15:55
add a comment |
Your local branch likely has modifications to it you want to discard. To do this, you'll need to use git reset
to reset the branch head to the last spot that you diverged from the upstream repo's branch. Use git branch -v
to find the sha1 id of the upstream branch, and reset your branch it it using git reset SHA1ID
. Then you should be able to do a git checkout
to discard the changes it left in your directory.
Note: always do this on a backed-up repo. That way you can assure you're self it worked right. Or if it didn't, you have a backup to revert to.
Your local branch likely has modifications to it you want to discard. To do this, you'll need to use git reset
to reset the branch head to the last spot that you diverged from the upstream repo's branch. Use git branch -v
to find the sha1 id of the upstream branch, and reset your branch it it using git reset SHA1ID
. Then you should be able to do a git checkout
to discard the changes it left in your directory.
Note: always do this on a backed-up repo. That way you can assure you're self it worked right. Or if it didn't, you have a backup to revert to.
answered Jun 3 '11 at 16:18
Wes HardakerWes Hardaker
15.7k13058
15.7k13058
This feels like it would probably work, but given the simplicity and reliability of the approach of "make a copy of your work somewhere else, make a fresh copy of the remote branch" I fail to see how this is better.
– greggles
Oct 18 '12 at 15:55
add a comment |
This feels like it would probably work, but given the simplicity and reliability of the approach of "make a copy of your work somewhere else, make a fresh copy of the remote branch" I fail to see how this is better.
– greggles
Oct 18 '12 at 15:55
This feels like it would probably work, but given the simplicity and reliability of the approach of "make a copy of your work somewhere else, make a fresh copy of the remote branch" I fail to see how this is better.
– greggles
Oct 18 '12 at 15:55
This feels like it would probably work, but given the simplicity and reliability of the approach of "make a copy of your work somewhere else, make a fresh copy of the remote branch" I fail to see how this is better.
– greggles
Oct 18 '12 at 15:55
add a comment |
git reset --hard
This is to revert all your local changes to the origin head
add a comment |
git reset --hard
This is to revert all your local changes to the origin head
add a comment |
git reset --hard
This is to revert all your local changes to the origin head
git reset --hard
This is to revert all your local changes to the origin head
edited Nov 16 '18 at 9:03
Kjartan
13.1k115175
13.1k115175
answered Nov 16 '18 at 8:45
Karthikeyan VaradarajanKarthikeyan Varadarajan
112
112
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6229764%2foverwriting-my-local-branch-with-remote-branch%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Possible duplicate of Reset local repository branch to be just like remote repository HEAD
– Michael Freidgeim
Apr 1 '16 at 2:42