Tuesday, November 5, 2024
HomeDevOpsHow to Create a new branch with git and manage branches

How to Create a new branch with git and manage branches

In your github fork, you need to keep your master branch clean, by clean I mean without any changes, like that you can create at any time a branch from your master. Each time that you want to commit a bug or a feature, you need to create a branch for it, which will be a copy of your master branch.

When you do a pull request on a branch, you can continue to work on another branch and make another pull request on this other branch.

Before creating a new branch, pull the changes from upstream. Your master needs to be up to date.

Create the branch on your local machine and switch in this branch:

# git checkout -b [name_of_your_new_branch]

Change working branch:

# git checkout [name_of_your_new_branch]

Push the branch on GitHub:

# git push origin [name_of_your_new_branch]

When you want to commit something in your branch, be sure to be in your branch. Add -u parameter to set upstream.

You can see all branches created by using:

# git branch

Which will show:

* approval_messages
  master
  master_clean

Add a new remote for your branch:

# git remote add [name_of_your_remote]

Push changes from your commit into your branch:

# git push [name_of_your_new_remote] [name_of_your_branch]

Update your branch when the original branch from official repository has been updated:

# git fetch [name_of_your_remote]

Then you need to apply to merge changes, if your branch is derivated from develop you need to do :

# git merge [name_of_your_remote]/develop

Delete a branch on your local filesystem :

# git branch -d [name_of_your_new_branch]

To force the deletion of local branch on your filesystem :

# git branch -D [name_of_your_new_branch]

Delete the branch on github :

# git push origin :[name_of_your_new_branch]

If you want to change default branch, it’s so easy with github, in your fork go into Admin and in the drop-down list default branch choose what you want.

Deleting and restoring branches in a pull request

If you have write access in a repository, you can delete branches that are associated with closed or merged pull requests. You cannot delete branches that are associated with open pull requests.

  1. On GitHub, navigate to the main page of the repository.
  2. Under your repository name, click Pull requests.
  3. Click Closed to see a list of closed pull requests.
  4. In the list of pull requests, click the pull request that’s associated with the branch that you want to delete.
  5. Near the bottom of the pull request, click Delete branch.

Restoring a deleted branch

You can restore the head branch of a closed pull request.

  1. On GitHub, navigate to the main page of the repository.
  2. Under your repository name, click, Pull requests.
  3. Click Closed to see a list of closed pull requests.
  4. In the list of pull requests, click the pull request that’s associated with the branch that you want to restore.
  5. Near the bottom of the pull request, click Restore branch.

To Learn More: GIT Tutorial

RELATED ARTICLES
- Advertisment -

Most Popular

Recent Comments