0

So I have a few different branches that I work on git. For example right now i'm on branch "test1" and I want to be able to do a pull and override my local work with a specific commit ID on branch "test1". I don't care about the local changes on my local branch of "test1"

Anyone know the commands for that.

Chapsterj
  • 6,483
  • 20
  • 70
  • 124
  • When you say 'local changes' on test1, do you mean changes you have made that you have not committed and/or changes you have made that have been committed locally? – MDrabic Oct 10 '13 at 15:58
  • Also, are you trying to 'pull' a commit into local branch test1 from another local branch or pull a commit from the remote branch test1 is tracking? – MDrabic Oct 10 '13 at 16:06
  • When I say local yes I mean changes I have committed and also changes I have not yet committed. I don't care about both locally. I just want to override my local copy with a specific commit on the same branch test1, using the commits ID. – Chapsterj Oct 10 '13 at 16:09
  • `git reset --hard ` will throw away current working directory changes and any staged changes, check out that particular commit, and make the current branch point to that commit. Or, `git reset --hard HEAD` will throw out all working-dir and staged changes and leave current branch where it is, and after that you can `git checkout ` to "detach your HEAD" (as it's called :-) ) and get that particular commit checked-out. The difference is that the two-command sequence leaves the current branch alone, and gets "off-branch" ("detached HEAD"). – torek Oct 10 '13 at 16:23
  • git reset --hard ? is meant to be where I put the ID of the specific commit. – Chapsterj Oct 10 '13 at 16:32
  • check out that particular commit, and make the current branch point to that commit ? How do I chackout the current commit and point the current branch to that commit – Chapsterj Oct 10 '13 at 16:34
  • `` here means anything that identifies (possibly indirectly) a specific commit SHA-1. You can use the literal SHA-1, or a unique abbreviation for it; a relative name like `HEAD~3`; a branch name (with or without modifier) like `dev^`; a tag name like `v1.7.3`; and so on. See [git-rev-parse docs](https://www.kernel.org/pub/software/scm/git/docs/git-rev-parse.html), under "SPECIFYING REVISIONS". (Nit: a branch name with no modifier, given to `git checkout`, will get you "on" that branch. A raw SHA-1 will get you "detached HEAD".) – torek Oct 10 '13 at 17:35

1 Answers1

1

I would first recommend resetting your local test1 branch to match its' remote tracking branch by performing:

//from the local test1 branch
git fetch origin
git reset --hard origin/test1

Note: This assumes the remote is named "origin" and the remote branch is named "test1".

Now that you have an up to date mirror of the remote test1 branch, you can create a new branch based on the specific commit you want to work off of. Example:

//from the local test1 branch
git checkout -b <new branch name> <commit id> 
//Example
git checkout -b crazy_idea_branch 06bb7167afbb9f399ea57f1cc5d0daead0dd6703

You can find more detail about git reset on this stack overflow answer.

Community
  • 1
  • 1
MDrabic
  • 917
  • 8
  • 15
  • Thanks MDrabic. how would I get the full branch names. right now I just did git branch and it's showing me all the branches but not the origin. After reset would it be possible to pull a specific commit to my local and then do a push back to test1 branch. This way I will have that specific commit locally and also it will be pushed to the remote branch also as the latest code. – Chapsterj Oct 10 '13 at 16:54
  • use `git branch -a` to show both local and remote branches. If you own your remote branch and **no one** else has pulled code from it, than you can use `git push origin test1 --force`. This will overwrite the remote branch with the contents of your local branch. Since you have a local mirror of the remote branch, you just `checkout` a specific commit from the local test1 branch instead of pulling a specific commit from the remote branch. – MDrabic Oct 10 '13 at 18:29