0

I have a few changes in my current branch called "foo".

I want to create a new branch called "bar" from my current branch.

But the new branch should not contain any changes (commited changes in my local system) which are in my current branch.

Example

$ git clone ssh://svc@somerepo.git
$ cd somerepo
$ ls
test1 test2 test3
$ cat test1 test2 test3
< no output, all these 3 files are empty >
$ echo "testing" >> test1
$ git commit -am "test"

Now I need to create a new branch called new-branch from my current branch, that branch should not contain any change - e.g. changes of "test" commit (mean all the above 3 text files contain nothing) and push to remote.

Is there any possiblity we can do?

kowsky
  • 12,647
  • 2
  • 28
  • 41
smc
  • 2,175
  • 3
  • 17
  • 30

3 Answers3

4

I want to create a new branch called "bar" from my current branch.

But the new branch should not contain any changes which are in my current branch.

Since you specified that you do not want you local commits in foo, but you want to include those on the remote branch, you should not create branch bar from your local branch foo, but from the remote foo branch:

git checkout -b bar origin/foo

This will create branch bar on the current state of foo in your origin repositoy, i.e. not containing local commits on foo.

Community
  • 1
  • 1
kowsky
  • 12,647
  • 2
  • 28
  • 41
  • I do not want to create branch based on master as well. because there are many changes we made in foo branch. I only don't want my local commits to be part of the new branch. – smc Feb 04 '19 at 13:35
2

Yes it's possible.

Here's your git tree:

----A---*            <---master

Commit A has 3 empty files committed. And currently you've made a change to file test1, but haven't committed yet. So in this scenario you just need to create a new branch using the command

git checkout -b "new-branch"

This will create:

      ---*            <---master
     /
----A                 <--- new-branch

This is basic git. You make branches from commit IDs. Its possible to even create a branch/tag/etc from a commit ID too. Check here

Once you've made your new branch you can push it using

git push --set-upstream origin new_branch
clamentjohn
  • 3,417
  • 2
  • 18
  • 42
  • No, if you see my example in my question, I have comitted the changes. I only don't want local commits which is not being pushed to remote branch, in my new branch. – smc Feb 04 '19 at 13:36
  • @smc Oh, my bad. Missed that – clamentjohn Feb 04 '19 at 14:22
1

enter image description here

On Github, type in the new branch name you want and you will be asked like- from which branch to create this new branch

jordan28
  • 113
  • 1
  • 5