Project : Git Branch Strategy - Creating Multiple Branches While wating for Merges
#Foreword
I am going to discuss Git Branch Strategies that I have used for team projects, which have allowed me to track commits easily.
In team projects, we often need to create multiple branches while waiting for them to be merged. If your pull request(PR) working branch is awaiting team approval, you need to use appropriate branch strategies.
Initially, I was unsure about GitHub, branch strategies, PRs, or anything related to GitHub. So, Whenever I needed to do PR or create a new branch, I felt very nervous.
I want to create this document to mitigate my uncertainty and nervousness, and to provide a clear guide for my future reference, promoting effective management of Git branch strategies.
# git init, Linking yours to Remote repo
: Images below are added just for reference.
git init
: I already used git init
cmd, so it says Reinitialized ~~ , Anyway when you create your IntelliJ Pojrect, you can use git init
Check the current status
: to see if there're any changes or untracted files.
git status
Commit status image
Stage all changes:
git add .
+
: In some cases, If you face CRLS warning message. As for my experience, I don't need to fix that CRLF
warning messages. (google it if needed.)
Commit the changes:
: you can leave commit by using the following command.
git commit -m "Project Initial set-up"
After using cmd,
You can use Git Commit Template Plugin:
: You can use the Git Commit Template Plugin that helps you to leave commit messages like below.
Like below, You can choose the specific file changes, and then leave a commit message selectively.
Add the remote GitHub repository:
Organization -> Joo-CodingPractice
Repository -> JavaCodingTest
Copy JavaCodingTest Repository's URL.
use the following cmd.
git remote add origin https://github.com/Joo-CodingPractice/JavaCodingTest.git
Push the local repository to GitHub:
git push -u origin master
You can check it's successfully added to your remote repository like below.
:Create New Branch, While other working branch is waiting for being merged.
When my PR branch is waiting for the approvement by team members and I need to create additional branch.
#First Approach
-> Create new feature branch(/#13) based on feature/#9
- Checkout the develop branch:
shCopy codegit checkout develop
- Verify you are on the develop branch:
shCopy codegit branch
- Pull the latest changes from the develop branch to ensure it's up to date:
shCopy codegit pull origin develop
- Create and switch to the new feature/#13 branch from the develop branch:
shCopy codegit checkout -b feature/#13
- Pull the changes from feature/#9 into feature/#13:
shCopy codegit pull origin feature/#9
This way, the feature/#13 branch will have the latest changes from the develop branch as well as the changes from the feature/#9 branch.
(feature/#9 : SignUp API , based on feature/#9
-> create feature/#13 : will be Login API)
#Second Approach (including GUI)
:develop branch -> pull feature/#9 -> create new feature/#13 branch.
Feature/#13 branch can have this updated develop branch,
which means it has develop + remote feature/#9 branch's file changes
Checkout the develop branch:
shCopy codegit checkout develop
- Verify you are on the develop branch:
shCopy codegit branch
- Pull the latest changes from the develop branch to ensure it's up to date:
shCopy codegit pull origin develop
- Pull the latest changes from the feature/#9 branch:
shCopy codegit pull origin feature/#9
- Create and switch to the new feature/#13 branch based on the updated develop branch (which now includes changes from both develop and feature/#9):
shCopy codegit checkout -b feature/#13
This sequence will ensure that your feature/#13 branch includes the changes from both the develop branch and the feature/#9 branch, without merging feature/#9 into develop.
Check where you are.
On Pull requests tap, you can see the branch you did PR.
Verify you are on the develop branch.
Pull the latest changes of origin/feature/#9 into develop branch.
(feature/#9 : you did PR, waiting for merge.)
Pull
Remote : origin/feature/#9 -> Into Local : develop
Left click on develop -> New Branch
Create branch at : develop (based branch)
Branch name : feature/#13 (create new Branch)
When pushing feature/#13 :
Local Branch : feature/#13 -> To Remote Repository : origin/feature/#9
Image After PR for feature/#13
I pulled feature/#9 into local develop branch, and then I created feature/#13, File changes and Commits are all traced like below.
feature/#9's file changes -> signUp (Based)
created feature/#13 -> login (Created from feature/#9)
In conclusion,
various branch strategies can be chosed based on the needs of specific project. The important thing is to understand how to use them effectively and flexibly. There's no inherently wrong way to manage branches; rather, It's better to select an preferable approach that aligns with your team's convention and preferences.
Subscribe to my newsletter
Read articles from Byung Joo Jeong directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by