DEV Community

Git Branch Guides

The git branch command lets you create, update, delete git branches depending on the arguments. Enjoy these guides

This is a collection of top and trending guides written by the community on subjects related to Git Branch concepts. For all things git, check out the git tag! Please contribute more posts like this to help your fellow developer in need.

How to move a commit to another branch in git

We've all done it - sometimes you forget you haven't created a branch yet, or worse yet you're on the wrong feature branch, and you start adding some code and make a commit. Then you realise you've committed code to the wrong feature branch or straight to master, and now it's in the wrong place.


Git Branch Naming Convention

On my end, having to work on dozens of different projects like these, I came up with this branch naming convention.


Break Git Down: How To Create a Branch From Master and Make Your First Commit!

To put it in the simplest way, the Master branch contains all of the official code for a project (aka repository). In a typical workplace, we would NEVER directly push code directly to master as a way to reduce the chance of shipping bugs and unnecessary code to production (aka live site). Instead, the safest thing to do is to create additional branches that are separate from the Master branch. Think of them as different versions of your project.


Git'de Branch Nedir? Nasıl oluşturulur?

Branch oluşturmak kullanıcıya çalıştığı projenin farklı versiyonlarına erişmesini sağlar. Kullanıcı, projesine bir yenilik eklemek istediğinde, yaptığı değişiklik projenin çalışmasını olumsuz etkileyebilir. Bu gibi durumlarda projemizin o anki halini bozmamak için branch kullanabiliriz. Branch yardımı ile projemizin çalışır halini kaydedip, yeni eklenti üzerinde rahatlıkla çalışabiliriz. Projemizde herhangi bir sorun çıktığı takdirde geri dönüp önceki versiyona kolaylıkla erişebiliriz. Branch, sadece projenizi değiştirirken ya da güncellerken eski halini korumak ile kalmıyor. Projenize bir versiyon çıkarttığınız zaman her yeni versiyon için repository oluşturmak yerine her bir versiyon için farklı branchler açabilirsiniz. Böylece versiyonları bir arada kolayca takip edebilir ve erişebilirsiniz.


How to Fix Git fatal: The current branch has no upstream branch

The permanent fix if you always want to just create that new branch on the remote to mirror and track your local branch is:


Change Git's Default Branch From Master

Edited 17th October 2020 (a bit late, I know!), Git now lets you set a default branch for init, and GitHub now defaults to main.


Rebasing a Branch in Git

merge command performs a three-way merge between the two latest branch commits and base commit which is the most recent common ancestor of the two branches.


Git TIP - Why you should not keep a local master branch ?

When working with git, I have often made the mistake of maintaining a master branch locally updated using these commands.


Change branch name from master to main

By google-ing, we'd find a simple command as git branch -m master main which should rename the master branch to main.


Rename your master branch in Azure Repos

Renaming your master branch in Azure Repos could be as simple as a few clicks. But if you have complex policies or permissions in place, it may be a little more work.


Visualize Git Branch Tree

Okay but sometimes I only need branch names, since I create and add bunch of feature or fixing bugs — a.k.a another feature. Therefore I want to know which branch is the up-to-date one or where this branch comes from. I

wanna see the branch relations.


Renaming your master branch

While it is possible to rename your master branch just like any other branch, there's a few gotchas that you have to look out for.


How to create an orphan branch in git

Sometimes you may need to create a brand new branch on a git project that doesn't contain anything present on the other branches nor shares an history with them, called orphan branch.


Get Git Default Branch from the Command Line (Powershell or Bash/Zsh)

On occasion, one needs to know the default branch for a given Git repo. Below I have compiled methods that fit a variety of use cases, and cover specific platforms (Github and Gitlab) as well as methods that work universally regardless of remote platform.


Quickly move from "master" to "main" branch at Github

On this article, I'll show how to update your default branch from "master" to "main".


Create PR against release branch from merged into develop branch?

After the f branch is merged into develop we should create a PR to the new release branch.


A Simple Git Branch Workflow

git init initializes the local directory as a new, empty git repo ($ denotes that we're executing this command in the terminal). This command generates an invisible .git file and endows the directory space with all the powers that Git affords. git remote add adds a remote (i.e. server) of the name origin at the address git@github.com:USERNAME/REPO-NAME.git. This command links the local repository to a server that will also host the repository. git push -u origin master pushes, or uploads, the contents in the master branch of the directory to the origin server. -u sets this remote repository as the default upstream reference, meaning origin will be the default remote repository from which things are pulled and to which things are pushed.


Pick a commit from another branch to the current branch (cherry-pick)

Checkout to the branch that has the required commit. Then get git hash of that commit by using CLI or Github site


Display current git branch in bash shell prompt

A pet peeve according to Google is something that a particular person finds especially annoying. A pet peeve according to me is when I need to type git status or git branch to double check I'm not accidentally mucking up the remote master.


How to check commit message and branch name with git hooks without any new installation

Hi All !

I'm a tech lead, and I'm on charge to check Pull Request/Merge Request on my team. And also to create release notes (CHANGELOG.md) on each release.

So, my first problem was to resolve the commits of the developers, that they always have some mistake, or have errors into the commit message (without correct format), or errors in the branch name.

I searched and I found different solutions. A lot of them need to use an external software, like node (npm library), or php composer library, etc. And the projects are in different technologies, like Android, PHP, .NET, etc.


Do you Git Branch in personal projects?

Even if you work alone, I still see them as useful, especially if you develop new features and have a master branch in production.


Move commits from one branch to another

You encountered a production issue. Immediately you stopped doing whatever you were working on and started fixing the bug. After fixing the bug you observe that you fixed the bug on a different branch. Now you want to move a certain number of commits from a branch A to branch B.


How to rename your master branch to something else

There are many people who might be looking to rename their branch to something more clearer and better.


Git - Renaming A Branch Locally & Remote

Similar to the mv command in Bash, the -m flag is for moving. Since you’re on a branch, Git infers the branch that’s being moved, however, you can be explicit and consolidate steps 1 and 2 with git checkout -m <oldbranch> <newbranch>.


How to list files changed in current branch (and run prettier on them)

I needed to run prettier on only the files changed in one branch of my git repo. This solution is a bit hacky but it did the trick!


Display Git Branch Info In Bash Prompts

I can't even count how many times I run git branch during an average workday. With multiple tickets assigned to me across multiple projects, keeping track of exactly where I am in our codebase can be a real challenge -- especially in my case, where my short-term memory is affected by ADHD. But with just a few extra lines added to .bashrc, my current branch info is now right in my Bash prompt!


Git: Rename default branch from master to main on existing repositories

I renamed default branch of all my existing repositories in Github and Gitea several months ago.

Here shows how I did it.


Git alias trick: Comparing your local branch to the upstream version

Hello! I would like to share a small Git alias that has made my life so much better: The ability to compare my local branch to its upstream equivalent. I often want to see exactly what changes I’m about to push before I pull the trigger, so I use this a lot.


Add Git branch name to each commit

This story is about how to utilize git hooks in order to add branch name to each commit. I have used the script from this article and modified it to my needs to minimize the restrictions.


How to See Which Branch Your Teammate is on in PhpStorm

“What branch are you on?” is often the first question you ask when a teammate says “The tests aren't passing” or “The build failed”. Here’s how you can get an answer to that question without even needing to ask.


How to restore local branch in GitHub?

It will provide all the HEADs of all branches. After this, simply run the command given below to recreate a branch using sha :


Protect git branch and prevent master push

I've only really ever developed application by myself but I work in a way that I hope to be able to work in a team. I still have plenty to learn but the basic idea is that I have 2 main branches master and develop. I setup develop as my default branch and the master branch is what is deployed as Production by merging changes from develop. So this is just a note to myself how to create this setup and it might be of some use to anyone else trying to organise their process a bit without making things to complicated.


Local Git Branch Prune/Delete Against Remote in Python

Automation is a programmer's best friend. Its an investment of time and skill in order to make your life and job easier. Hey, we have all been there. You are working on a project and you are attempting a major change. So you create a new branch off of the master and make your changes. Publish/Commit/Push. Now you have to look into a bug but you don't want to merge your changes in without testing yet. You create a new branch off of the master and resolve your bug there. Tested and merged into master. After weeks of doing this, you realize that your local branches have piled up and are most likely dead (assuming that you delete your branches in remote once they are merged). So you will need to run a command to prune, then run a command to get branches including remote, then run another command to delete old branches...bleh.


Delete a Git branch both locally and remotely [Shell aliases + functions] #OneDevMinute

Deleting a git branch locally is pretty easy, just do a git branch -D branch_name and you're done. But when you have already pushed that branch to remote, it becomes a bit more complex. You can't just delete the local branch and do a git push to be done with it. Instead, you have to do a git push origin --delete branch_name to delete it from the remote.


Automating pull request creation from one branch to another using javascript, bitbucket pipelines and bitbucket api

These commands include installing packages, making api requests using curl, storing variables, running javascript tasks and many more. Now that we have the advantages of storing variables and installing packages, making api requests and running javascript tasks we can create pull requests from one branch to another in your bitbucket repository.


Resolve common branch conflict

Dev branch is sort of a playground/sandbox for developers. A superior and more stable is a Test for testers - trying to break our code and raise first bugs/issues. Stage is for testing again, but this time by our end-client's Quality-Assurance team. Finally Prod is THE prod :)


Changing the default branch name on GitHub

Git still calls the default branch of repositories 'master', a holdover from BitKeeper, which had both master and slave branches.


How To Create A New Branch With Git Using Terminal

In this post I will show how to create a new branch with git using VS Code.


Get Git and Github/GitLab in sync on default branch naming (master | main)

Last July, some of the git hosting platforms (GitHub, GitLab, and Bitbucket) changed the default branch name to main (from master). However, Git did not!


Push to a forked pull request branch on GitHub—Am I doing this right?

It's kind of straightforward but requires me to add his repo, copy and paste his branch name, and just doesn't seem elegant.


Atualizando um branch com git rebase

O comando git rebase pode se usado para fazer vários ajustes de histórico, desde reescrever a árvore de commits, reescrevendo assim o histórico, até mesmo empurrar commits para um ponto mais a frente como se o branch fosse criado no futuro.


How to test your branch in an Arch Linux package

In the previous post, we learned how to apply a git patch to a package. In this entry, we'll try to package a specific branch and run our code installed on our system.


Easily get current branch name in git

One day I used that command for the branch with really long name. I just typed a few fisrt letters, pressed tab and branch name was auto-completed (I personally prefer fish shell, but it should also work for all other modern shells). I asked myself: "What if I am using bash and I don't have the auto-completion, but I also don't want to use git branch and copy-and-paste branch name?".


Automating to copy a set of commits from one branch to another

This article will describe how to copy a set of contiguous commits from one branch to another branch in a repository


Git Branch + Commit Templates

Interested in free branch and commit templates?

Check them out and use them!

Link : https://github.com/juricaKenda/git-templates


Happy Git Branch coding!