Day 10 Task: Advance Git & GitHub for DevOps Engineers.


Git Branching

Git branching is a powerful feature that allows developers to work on different parts of a project simultaneously without interfering with each other's code. Branching is essentially a way to create a separate line of development within a Git repository. Here are some key concepts related to Git branching:

1. Main Branch:

- In many repositories, the default or main branch is where the latest stable version of the code is kept. It is often named "main" or "master."

2. Creating a New Branch:

- To create a new branch, you can use the following command:

git branch branch_name        

- This creates a new branch but does not switch to it. To switch to the new branch, you can use:

git checkout branch_name        

or, in more recent Git versions:

  git switch branch_name        


Alternatively, you can combine branch creation and switching with:

git checkout -b branch_name        

or

git switch -c branch_name        

3. Viewing Branches:

- To see a list of all branches in your repository and identify the current branch, you can use:

git branch        

- The branch with an asterisk (*) next to it is the currently active branch.

4. Merging Branches:

- Once you've completed work on a branch and want to incorporate those changes back into the main branch, you can merge the branches:

git checkout main  # Switch to the main branch (replace "main" with your branch name).        
git merge branch_name  # Merge the changes from the specified branch into the current branch.        

- This creates a new commit that combines the changes from both branches.

5. Deleting Branches:

- After merging a branch, you might want to delete it if it's no longer needed:

git branch -d branch_name        

This removes the branch, but if there are unmerged changes, Git will prevent the deletion. To force deletion, you can use -D:

  git branch -D branch_name        

6. Remote Branches:

- Remote branches exist on the remote repository (e.g., on GitHub, GitLab). To fetch remote branches to your local repository:

     git fetch origin        

- To create a local branch that tracks a remote branch:

git checkout -b local_branch_name origin/remote_branch_name        

- To push a new local branch to the remote repository:

 git push origin local_branch_name        

Branching in Git is a fundamental part of collaborative development, allowing multiple developers to work on different features or bug fixes simultaneously and merge their changes seamlessly.


要查看或添加评论,请登录

社区洞察

其他会员也浏览了