How to move a branch in git?
“Moving a branch” may mean two things:
1. Move the contents (the commits, the changes) associated with a branch name somewhere else in the repository.
2. Move the branch name (not the code changes!) to point to some other commit.
Let’s examine the two possibilities in detail:
1. Move branch contents and name:
This case is more complicated. If that’s what you want to do then “git rebase” is your friend. This command is quite complex and I suggest you study the git rebase help page before using it (‘git help rebase’).
Just to give you a quick example:
If your tree looks like this:
---o---o---o---o master
\
o---o---o---o---o next
\
o---o---o topic
And you want to move topic (all 3 commits associated with this branch) to master, then this command will do it:
git rebase --onto master next topic
This basically means “put onto ‘master’ everything between ‘next’ (not inclusive) and ‘topic’ (inclusive)”
After doing this your tree will look like this:
---o---o---o---o master
| \
| o´--o´--o´ topic
\
o---o---o---o---o next
2. Move just branch name
To move just the branch name type:
git pull . +new_br:old_br
What the above will do is within our local repository (the .) move the branch old_br (just the branch name or label, not the contents) to the same location as branch new_br. In other words after this command old_br will point to the same commit as new_br.
Note that by doing this you may lose (easy) access to the commit where old_br used to point to and to all unreferenced commits below it – because you are essentially removing its association with that commit and if nothing else (another branch or tag) points to it AND if it is the last commit on that branch then the whole branch will disappear.
Here’s some examples to illustrate this:
---o---o---o---o master
\
o---o---o---o---o old_br
\
o---o---o some_br
OK, because there is another branch (some_br) above old_br.
---o---o---o---o master
\
o---o---o---o---o old_br | [some_tag]
OK – there is also a tag (some_tag) at the same commit as old_br.
---o---o---o---o master
\
o---o---o---o---o old_br
NOT OK – old_br is the last commit on this branch and the entire branch (all the red dots) will disappear if old_br is moved. If this is not your intention then add another branch or tag there before moving old_br.
2 Responses to How to move a branch in git?
Leave a Reply to lunette de soleil pas cher Cancel reply
-
Categories
- Android Development
- Bash
- C programming
- dpkg/apt-get
- drupal
- Emacs
- Git
- Java
- Linux administration
- Linux device drivers
- Linux Gaming
- Linux kernel
- Linux Networking
- Linux on Windows
- Linux printing
- Linux sound and ALSA
- Package Managers
- Programming
- RPM
- Shell and environment
- Tips and tricks
- Uncategorized
- VirtualBox
- Virtualization
- web development
- wine
- WMaker
- Wordpress Tips
- X Window System
- XFCE
-
Articles
- August 2020
- August 2019
- May 2019
- July 2017
- February 2017
- January 2017
- November 2016
- October 2016
- September 2016
- August 2016
- July 2016
- June 2016
- April 2016
- March 2016
- December 2015
- November 2015
- September 2015
- June 2015
- May 2015
- April 2015
- March 2015
- February 2015
- January 2015
- December 2014
- October 2014
- February 2014
- January 2014
- November 2013
- October 2013
- June 2013
- April 2013
- March 2013
- February 2013
- January 2013
- December 2012
- October 2012
- August 2012
- July 2012
- June 2012
- May 2012
- April 2012
- March 2012
- February 2012
- September 2011
- August 2011
- June 2011
- May 2011
- April 2011
- March 2011
- February 2011
-
Meta
Cloud
audio bash boot compile C programming cups drupal emacs etc Fedora git grep how to httpd init kernel libc linux linux partition localtime login make mount mp3 mysql networking oracle package managers password phpMyAdmin programming rpm shell sql vbox version control system virtual box vm web server wordpress www xargs xfce xwin yum
lunette de soleil pas cher…
Very great post. I simply stumbled upon your blog and wanted to mention that I’ve really loved browsing your blog posts. After all I’ll be subscribing in your feed and I am hoping you write again soon!…
Quite useful post specially the second one.