How to clone specific branch instead of master branch

Young Joe October 25, 2021

Hi, I am a real beginner on GIT and this is basic concept question with basic how to question.

My understanding is

1. Create main/master repository on cloud and clone/copy the repository as a branch(let's say A branch)

2. A branch has same files as Master branch

3. Using "git clone" and bring all files to local repo.

   In this step, I only can clone master branch.  Can I clone A branch?

4. Edit/create files in local.

5. Using "git add, git commit" and put changes to my local repo

6. Using "git push" and put it on A branch in cloud

   In this step, my changes go to master branch.

   Isn't this go to A branch and I follow PR, approve and merge steps?

My questions are

How can I push my local changes to A branch, not master.

How can I set master branch to deny direct push from local.

  

1 answer

1 accepted

0 votes
Answer accepted
Esther Strom
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 25, 2021

Hi, @Young Joe - welcome to the Community!

You use clone only once, generally - to get an existing remote repo onto your local machine. Once you've done that, all future work on that repo would be done using checkout.

To check out a new branch from a remote branch other than master (in this example, it's develop; change that to whatever your remote branch is):

 

git checkout -b <new branch name> origin/develop

 

 To push a local commit to a specific remote branch:

git push origin <remote branch name>

However, if the end goal is to get your changes into master, usually you'd just do git push, which would result in the remote repo having a copy of your local branch. When you create a pull request, the origin is the copy of your branch; the destination is master.

Here's an example - I have a repo called atlassianScripts. I don't have a develop branch, only master, but you can pretend I'm doing a pull from another branch if it helps :)

2021-10-25 11_01_52-MINGW64__c_repo_atlassianscripts.png

You can see from the first line of my bash window that the local branch I was previously working on was called estrom/jira-1551. It had already been committed and pushed, so it was safe to just do a new pull from the server (git fetch origin). 

I then checked out a new branch from origin/master called estrom/jira-1800 (this is where you'd choose another branch to pull from if you have a develop branch, or a release branch you need to work from.)

After making changes to my files, I do a git commit - in this case, since I didn't add any new files, there was no need for git add; I'm just committing changes to existing files. I include the jira ticket number as the first part of the commit message so the commit (and subsequent pull request) is linked to the Jira ticket in bitbucket and jira.)

I then just do a git push, no branch specified.

Now, I go into Bitbucket (or github if that's what you're using) and create a pull request.

2021-10-25 11_09_24-2021-10-25 11_03_04-phconnectchicago _ atlassianScripts _ Create pull request — .png

You can see that the branch I had created locally and pushed is now the origin of the pull request (the one on the left) and master is the branch the code will be merged to once the pull request is approved.

Hope that helps! Atlassian has a really great series of git tutorials that would be a great starting point.

Esther Strom
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 25, 2021

Sorry, missed your second question. You can prevent pushing directly to master (if you're the repo owner or admin) by updating repo settings. In Bitbucket, it looks like this (go to Repo Settings -> Branch Permissions):

2021-10-25 11_16_53-phconnectchicago _ atlassianscripts — Bitbucket - Vivaldi.png

Young Joe October 25, 2021

Wow, so fast response and it helps me a lot.
I really appreciate your help.
Thanks.  :)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events