branch – TecAdmin https://tecadmin.net How to guide for System Administrator's and Developers Thu, 04 Aug 2022 09:26:26 +0000 en-US hourly 1 https://wordpress.org/?v=6.1.1 How to Create Branch in Git Repository https://tecadmin.net/create-branch-in-git/ https://tecadmin.net/create-branch-in-git/#respond Sun, 24 Jul 2022 06:50:38 +0000 https://tecadmin.net/?p=8199 Git is an amazing tool, and it has multiple use cases. You can use Git to manage source code, user stories, bug reports, and so much more. As a result of its versatility, there are many different ways to use Git in different contexts. This article explains the usage of Git in the context of [...]

The post How to Create Branch in Git Repository appeared first on TecAdmin.

]]>
Git is an amazing tool, and it has multiple use cases. You can use Git to manage source code, user stories, bug reports, and so much more. As a result of its versatility, there are many different ways to use Git in different contexts.

This article explains the usage of Git in the context of a software project with multiple developers working on different features simultaneously. It will help you learn how to create a branch in a Git repository for better version control and collaboration among team members. Let’s get started!

What is a Branch in Git Repository?

A branch in Git is an independent line of development. They allow developers to work on multiple features or bug fixes in parallel without the risk of conflicting code when those features are eventually merged back into the main codebase. Branching in Git is used to see the progress of code with multiple features without interrupting the workflow.

Alternatively, a branch can be used to fix bugs on a specific feature without interrupting or changing the progress of other features. Branching in Git allows multiple developers to collaborate with each other with ease because they can work on different features or bug fixes and then merge them when they’re ready. This allows for seamless collaboration among team members and reduces the risk of conflicting branches and code.

Create a Branch in Git Repository

A branch can be created when you have a specific feature or fix in mind. This allows you and your team members to focus on a single change at a time to reduce the risk of conflicting and broken code. The steps below will explain how to create a branch in Git Repository. To create a branch, follow these steps.

  • Open the Terminal or Command Prompt and navigate to your git repository folder:
    cd /path/to/git_repository/ 
    
  • Create a new branch by entering the following command.
    git checkout -b stage1 
    
    Output
    Switched to a new branch 'stage1'
  • The above command will switch immediately to the new branch. Later you can switch branches by entering the following command
    git checkout stage1 
    

That’s it. You have successfully created a new branch in your local Git repository.

Push a Local Branch to Remote

Once you have created a new branch in your local repository, You may need to push it to remote also. Let’s push your newly created branch ‘stage1‘ to the remote git repository. To push make sure you are on the correct branch.

git checkout stage1 

Now use the following command to push your branch ‘stage1’ to the remote git repository.

git push origin stage1 
Output
Username for 'https://github.com': your_git_user_name Password for 'https://your_git_user_name@github.com': Total 0 (delta 0), reused 0 (delta 0) To https://github.com/tecrahul/firstrepo.git * [new branch] stage1 -> stage1

Merge Changes from One Branch to Another

Merge changes from one branch to another when you’re ready to combine the code from one branch with the code from another branch. This is done to combine the development of two or more independent branches into a single feature or fix. Merging branches together can be easy or difficult, depending on the situation. When branches have diverged significantly, it can be difficult to merge them without introducing bugs. When branches have diverged only a small amount, merging is easy. It’s important to be careful when merging branches together because changes can be overwritten.

When it comes to merging branches in Git, you can create pull_request (Github) or merge_request (Gitlab) or similar tools in other Git providers.

Creating a new branch in Git repository

Conclusion

In this article, you learned how to create a branch in Git Repository. You also learned that a branch is an independent line of development, and they allow developers to work on multiple features or bug fixes in parallel without the risk of conflicting code when those features are eventually merged back into the main codebase.

You also learned the steps to create a branch in Git Repository, commit changes on the branch, and merge changes from one branch to another. You can use branching to see the progress of code with multiple features without interrupting the workflow. Alternatively, a branch can be used to fix bugs on a specific feature without interrupting or changing the progress on other features.

Now that you know what a branch is and how to create one, you can better manage your code and collaborate with other developers. With branching, you can break down your code into smaller chunks, and team members can focus on one task at a time without breaking the entire project.

The post How to Create Branch in Git Repository appeared first on TecAdmin.

]]>
https://tecadmin.net/create-branch-in-git/feed/ 0
How to Create Empty Branch in Git Repository https://tecadmin.net/git-create-empty-branch/ https://tecadmin.net/git-create-empty-branch/#respond Sat, 04 Jun 2022 16:34:39 +0000 https://tecadmin.net/?p=29751 I have an application code maintained in the Github repository. Now our requirement is to create the documentation for that application and save them under a separate branch in the same repository. I simply tried to create a new branch but it linked to the main branch. After googling it, I found an option --orphan [...]

The post How to Create Empty Branch in Git Repository appeared first on TecAdmin.

]]>
I have an application code maintained in the Github repository. Now our requirement is to create the documentation for that application and save them under a separate branch in the same repository. I simply tried to create a new branch but it linked to the main branch. After googling it, I found an option --orphan to create branch with no parents.

This tutorial will help you to create a new empty branch in the Git repository.

Git Create Empty Branch

We can use --orphan command line option to create a new branch with no parents.

git checkout --orphan new-empty-branch 

The above command will create a new branch with no parents. Now, you can delete files from the working directory, so they don’t commit to a new branch.

git rm -rf . 

Now, you can add new files to this new branch, commit and push them up to the remote git repository.

The post How to Create Empty Branch in Git Repository appeared first on TecAdmin.

]]>
https://tecadmin.net/git-create-empty-branch/feed/ 0
How To Rename a Local and Remote Git Branch https://tecadmin.net/git-rename-branch/ https://tecadmin.net/git-rename-branch/#comments Tue, 05 Feb 2019 14:43:49 +0000 https://tecadmin.net/?p=11651 Git is commonly used as a version control system for any application. Which is helpful for tracking changes in any set of source files. A branch in Git represents an independent line of development with a unique set of code changes with a unique name. This tutorial will help you to Rename a Git branch [...]

The post How To Rename a Local and Remote Git Branch appeared first on TecAdmin.

]]>
Git is commonly used as a version control system for any application. Which is helpful for tracking changes in any set of source files. A branch in Git represents an independent line of development with a unique set of code changes with a unique name.

This tutorial will help you to Rename a Git branch on a local repository. Also, we will apply the change to the remote git repository.

Rename a Git Branch in Local

You can use git branch -m followed by branch name to rename a git branch on your local repository.

  1. First, switch to the branch you want to rename.
    git branch <old_branch_name> 
    
  2. Next, check the current active branch
    git branch 
    

    An asterisk (*) symbol denotes the currently active branch.

  3. Finally rename a git branch with the following command. Before executing, make sure to replace the <new_branch_name> with your branch name.
    git branch -m <new_branch_name>
    

That’s it. You have successfully renamed the current branch in the local git repository. Again run git branch to list branch names.

Rename a Git Branch on Remote

First, rename the branch name in the local repository with the above instructions. Then push the new branch to the remote repository.

git push origin -u <new_branch_name>

Check your remote repository and you will find the new branch there. However, the old branch will still be visible. To fix this, remove the old branch by running the following command.

git push origin --delete <old_branch_name>

Conclusion

This tutorial describes you rename a Git branch on local and remote repositories.

The post How To Rename a Local and Remote Git Branch appeared first on TecAdmin.

]]>
https://tecadmin.net/git-rename-branch/feed/ 1
How to Checkout Remote Git Branch https://tecadmin.net/checkout-remote-git-branch/ https://tecadmin.net/checkout-remote-git-branch/#respond Fri, 09 Sep 2016 04:36:13 +0000 https://tecadmin.net/?p=10767 This tutorial will help you to check out a remote git branch that is not available on the local git repository. Use command git branch to list local branches available. Git Checkout Remote Branch Now use command git branch -a to list all available branches on local and remote git repository. After that run command [...]

The post How to Checkout Remote Git Branch appeared first on TecAdmin.

]]>
This tutorial will help you to check out a remote git branch that is not available on the local git repository. Use command git branch to list local branches available.

Git Checkout Remote Branch

Now use command git branch -a to list all available branches on local and remote git repository. After that run command git fetch command to update your remote-tracking branches under refs/remotes//. Now checkout new branch to your local system using git checkout branch_name.

git fetch
git checkout staging

Have Multiple Remotes ?

If you are using multiples remotes, you have to specify remote name with remote branch name like below command

Syntax:

git checkout -b local_branch_name remote_name/remote_branch_name

Command:

git checkout -b staging origin/staging

After completing the above commands again run ‘git branch’ command to view local branches and current active branch.

git branch

  master
* staging

The post How to Checkout Remote Git Branch appeared first on TecAdmin.

]]>
https://tecadmin.net/checkout-remote-git-branch/feed/ 0
How to Create a Branch in Remote Git Repository https://tecadmin.net/how-to-create-a-branch-in-remote-git-repository/ https://tecadmin.net/how-to-create-a-branch-in-remote-git-repository/#respond Tue, 01 Sep 2015 04:50:20 +0000 https://tecadmin.net/?p=8250 Branching makes efficient ways to manage versioning of your application code. The popular version management tools supported branches like Git, SVN etc. Development in branching make process easier by splitting code in branches per modules. Most of the Git providers (like: github.com, gitlab.com etc) provides option to create branch directly with web interface. But, in [...]

The post How to Create a Branch in Remote Git Repository appeared first on TecAdmin.

]]>
Branching makes efficient ways to manage versioning of your application code. The popular version management tools supported branches like Git, SVN etc. Development in branching make process easier by splitting code in branches per modules.

Most of the Git providers (like: github.com, gitlab.com etc) provides option to create branch directly with web interface. But, in case you don’t have web interface access, You can also do the same by creating a branch in local repository and push changes to remote.

This article will help you to create a branch on local repository and then push branch to the remote Git repository.

Create A Local Git Branch

First create branch on local git repository using following command. This command will create a branch named “stage1” and switch to it immediately.

  • Syntax:
    git checkout -b <BRANCH_NAME> 
    
  • Command:
    git checkout -b stage1 
    

You can created a branch on your local git repository. Use git branch command to view all the branches in local repository.

Push Branch to Remote Git Repository

Now push newly created branch to remote Git repository. Branch will automatically created on remote git repository.

  • Syntax:

    git push <REMOTE_NAME> <BRANCH_NAME>
    
  • Command:
    git push origin stage1
    

The above command creates branch on remote git repository with same name as local “stage1” and push all files there.

You can also create branch on remote branch with other name. To create remote branch with other name speicify the remote branch branch name just after local branch name seprated with colon (:). The syntax and command looks like below:

  • Syntax:
    git push <REMOTE_NAME> <LOCAL_BRANCH_NAME>:<REMOTE_BRANCH_NAME> 
    
  • Command:
    git push origin stage1:development 
    

This will create branch named “development” on remote git repository and push data from local branch “stage1

Conclusion

In this tutorial, you have learned to create a branch on remote git repository.

The post How to Create a Branch in Remote Git Repository appeared first on TecAdmin.

]]>
https://tecadmin.net/how-to-create-a-branch-in-remote-git-repository/feed/ 0
How to Delete Git Remote and Local Branch https://tecadmin.net/delete-git-remote-and-local-branch/ https://tecadmin.net/delete-git-remote-and-local-branch/#respond Tue, 25 Aug 2015 06:24:13 +0000 https://tecadmin.net/?p=8201 Generally we don’t recommend to remove any branch from git repository for production sites. But sometimes you may need to delete any existing git branch from your repository. For example git repository has corrupted files or usefulness files which is no more required in future. This article will help you to delete git remote and [...]

The post How to Delete Git Remote and Local Branch appeared first on TecAdmin.

]]>
Generally we don’t recommend to remove any branch from git repository for production sites. But sometimes you may need to delete any existing git branch from your repository. For example git repository has corrupted files or usefulness files which is no more required in future.

This article will help you to delete git remote and local branch in repositories.

  • Delete Remote Git Branch:

    – Use the following command to remove any branch from remote server. Following example will remote branch named “stage1” from remote git repository.

    $ git push origin --delete stage1
    
    Username for 'https://github.com': rahul
    Password for 'https://rahul@github.com':
    To https://github.com/tecadmin/firstrepo.git
     - [deleted]         stage1
    
  • Delete Local Git Branch:

    If you also want to remove git local branch. Use the following command to remove it.

    • List all local branch names using following command.
      $ git branch
      
        master
      * stage1
      
    • As per last command output, you can see that your are currently using “stage1“. So If you try to remove this branch, you will face following error.
      $ git branch -d stage1
      
      error: Cannot delete the branch 'stage1' which you are currently on.
      
    • Change to different branch to remove ‘stage1‘.
      $ git checkout master
      
      Switched to branch 'master'
      Your branch is up-to-date with 'origin/master'.
      
    • Now delete the stage1 branch using following command. This time it will delete branch successfully.
      $ git branch -d stage1
      
      Deleted branch stage1 (was cc8ebe7).
      

The post How to Delete Git Remote and Local Branch appeared first on TecAdmin.

]]>
https://tecadmin.net/delete-git-remote-and-local-branch/feed/ 0