Using Git Branches | SJB -->

This website uses cookies to ensure you get the best experience. More info...


Using Git Branches

Software Engineers deal with git branches pretty much every day. Git branches are an integral part of our everyday workflow. To keep it as a reference, we will talk about some every day required git commands for branching including how to create a branch, checkout branch and finally, how git merge can integrate the history of independent branches.

Git Branch
[ Color coded serial numbers represent Git Flow ]
1. List all of the branches in a repository.
     $ git branch
2. Create a new branch called "MyBranch".
     $ git branch {MyBranch}
     This following command tells Git to publish current local branch on the "origin" remote under the name "MyBranch". The "-u" flag establishes a tracking connection between that newly created branch on the remote and our local "MyBranch" branch.
     $ git push -u origin {MyBranch}
3. Rename "MyBranch" as needed.
     $ git branch -m {MyBranch}
4. Delete local "MyBranch". This is the safest option for deleting a local branch since branch doesn't get deleted if there is any unmerged changes exist.
     $ git branch -d {MyBranch}
5. This is for force delete. It deletes the branch with any existing unmerged changes. Watch out there is a shark!
     $ git branch -D {MyBranch}
6. Checkout "MyBranch". It makes "MyBranch" current/working branch.
     $ git checkout {MyBranch}
7. It creates new branch "NewBranch" from old "MyBranch". Now "NewBranch" is current/working branch.
     $ git checkout -b {NewBranch} {MyBranch}
     This following command creates a remote "NewBranch". [See #2 above]
     $ git push -u origin {NewBranch}
8. These two commands on this step is to commit whatever changes have done on "NewBranch". First, we need to add the edited file with the first command, and second command is to commit the change with appropriate commit message.
     $ git add {FileName}
     $ git commit -m "Commit Message"
     If we have more than one file changes on our current branch, we can you use following commands to stage files to commit. The second command stages deletions too.
     $ git add .
     $ git add -u
     The most convenient command is to stage files is the command mentioned below. It stages all file changes including deletions.
     $ git add -A
9. Once we are done fixing our code, we can merge two branches together. First, we need to checkout the old {MyBranch} with the first command, and then run the second command to merge the {NewBranch} into {MyBranch}.
     $ git checkout {MyBranch}
     $ git merge {NewBranch}
10. By the way, this is the ideal way to merge a branch since it always generates a merge commit.
     $ git merge --no-ff {NewBranch}

TIP: Run this command to list merged branches while on main branch.
     $ git branch --merged | grep -v "\*"
     Once merging is complete, we can delete {NewBranch} using delete command mentioned above. These are some useful commands for as needed branching that we need everyday at our work or to control our projects. We will remember more than these as time goes by but I hope this list will be helpful for a starter somewhere in this world. Keep coding and keep your code under control.

Check out Delete Git Branch.

No comments:

Post a Comment