1

I have 2 remote branches, dev and rls. We are working on the dev branch currently. My rls branch is empty/untouched. I want to move a specific commit(sha-1 hashes) from dev to the rls branch. Please guide me the best, safe and easy way to do the same in Git.

Thanks...

user265950
  • 467
  • 3
  • 9
  • 21

3 Answers3

2

One way would be to use git cherry-pick, in order to select the exact commits you want to report on rls branch.

See "Can anyone explain what git cherry-pick does?" for more.

Then you can push rls to the corresponding remote branch.

So:

  1. cherry-pick locally in order to have an rls local branch which looks like something you want to publish (push)
  2. push rls to the remote repo
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • let me phrase my question again. I want to move all the code till a specific commit from my dev to rls branch. Its like moving the code from dev branch at any point of time in to the rls branch. Thanks.. – user265950 May 09 '13 at 16:41
  • @user265950 sure, that is what git cherry-pick is for: it can report a range of commits. If you want those commits *gone* from dev though, then you have to do an interactive rebase. – VonC May 09 '13 at 17:33
  • what I observed is when I cherry pick a commit from one branch to another, it creates a new SHAID. But when I use merge on to other branch it uses the same SHAID. how is this explained? and why like this? – user265950 May 29 '13 at 19:05
  • 1
    @user265950 acherry-pick takes one commit and create a similar one on the other branch, hence the new SHA1. A merge will also create another one (merge commit) if a merge is needed, unless it is a fat-forward merge (HEAD is simply moved, no new commit). In short: new SHA1 equals new commit. – VonC May 29 '13 at 19:14
1

If you want to move all code until a specific commit to the rls branch, use git merge. For example,

git checkout rls
git merge [SHA1 of last commit you are interested in]
Brad
  • 5,492
  • 23
  • 34
  • Thanks Brad. So ideally whats the difference between hard reset and merge. After doing hard reset and merge i had to run push command. Seems like merge and hard reset helps me achieve the same thing that i require. please update me on the exact differences. – user265950 May 10 '13 at 17:49
  • May be I get it. With Hard reset i can move to any point of time, ahead and back. But with Merge its only moving forward linearly. But essentially for my requirements, both are helping me as of now. Is this correct? – user265950 May 10 '13 at 17:52
  • The big difference is that, with merge, you will pull in other commits in your history. So if you had other changes in rls, you would keep them with git merge, but would lose them with git reset. There are use cases where both are necessary. – Brad May 10 '13 at 18:56
1

If you are looking for rls to be a snapshot of the dev branch at some arbitrary commit, you can use git reset to move your branch pointer to the commit you are looking for. Make sure you have no unstaged changes when doing this.

git checkout rls  
git reset --hard <SHA-1 of commit in dev>

Example:
If dev looks like:

A - B - C - D

and you want rls to look like:

A - B

you would do

git checkout rls
git reset --hard <sha-1 of B>

However, if you want to choose a more specific range of commits that you want to push to rls, you'll want to use the git cherry-pick command.

git checkout rls
git cherry-pick <non-inclusive-starting-SHA1>..<inclusive-ending-SHA1>
joshtkling
  • 3,428
  • 2
  • 18
  • 15