Git: Efficient steps to create a new branch and push to remote
Asked Answered
P

4

8

I figured out the steps but seems cumbersome, take bitbucket for example, suppose i already have a project called prj

  1. I branch a new project from server side(bitbucket.com), called prj-bz
  2. From local i add add a remote git remote add prj-bz https://blah...
  3. At the same time from local i create a new branch called prj-bz
  4. From local i call git push prj-bz prj-bz to let local repo and remote one connected.

I checked out some git books but seem none cover this. Any more efficient way to do this?

Potamic answered 28/2, 2013 at 9:26 Comment(3)
no, there is no efficient way of doing this. How do you think this can be more efficient?Unclinch
Maybe the first step can be omitted?Potamic
Oh, I think I understood that wrong. In the 1 step, you mean you create a branch on bitbucket?Unclinch
E
49

Generally, people usually do one or the other Fork or Branch. It sounds like you are making a Fork of a repo, then making a branch in the fork with the same name. If you're using a Pull Request to put data back in to the main repo, you don't need to do both. Pick one of the two workflows:

  • Fork the repo on Bitbucket (or other site)
  • Clone the repo git clone https://bitbucket.org/username/repo-fork.git
  • Work in that fork git commit -m "some work done", git push -u origin master
  • Create a Pull request to request your changes to be placed back into the parent of the fork

OR

  • Clone the main repo git clone https://bitbucket.org/username/repo-fork.git
  • Create a new local branch git checkout -b my-branch
  • Work in that branch git commit -m "some work done"
  • Push up the branch git push -u origin my-branch
  • Create a Pull request

With the branch method, I'm assuming you have rights to write to the main repo. If not, you'll want to stick to the fork method. There are more workflows out there too. Bitbucket also has a doc explaining this as well as one on Atlassian's website with a bit more depth on Git workflows.

Epicarp answered 5/3, 2013 at 17:30 Comment(2)
Thank you Marcus, i use the Branch method you mentioned, and it worked perfectly well.Potamic
git push -u origin my-branch is the most important step hereSamples
A
2

For creating new branch we will use: (using this command A new branch will create and the branch status will also change with newly created branch)

git checkout -b branch-name

And for pushing the changes we can run following commands:

git add . 
git commit -m "with meaningful comments" 
git push origin branch-name
Aoristic answered 11/12, 2018 at 8:12 Comment(0)
O
0

Well, if creating a new repo instead of a new branch in an existing one, you could just git clone https://blah <target folder> to replace steps 2-4.

If not, your only real alternative is creating a simple script that accepts remote name, branch name and git url as arguments and executes steps 2-4 with that information.

Ovi answered 28/2, 2013 at 14:42 Comment(0)
S
0

Simple steps to create a 'dev' or 'prj-bz' branch and save all changes,

Step 1: Create a dev branch to save development changes,

git checkout -b dev
git add .
git commit -m “Your commit message”
git push -u origin dev

Step 2: Do all development and whenever you need to push changes on the dev branch,

git add .
git commit -m “Your commit message”
git push origin dev

Step 3: Merge changes from dev to main whenever changes are final for production,

git push origin dev
git checkout main
git merge dev
git push origin main

Step 4: Once merge is done, go back to the dev branch and do all development and repeat Step-2,

git checkout dev
Sketch answered 3/9, 2022 at 20:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.