Git remote branches: how to create, track, remove/delete
1. Here’s how to to create a remote branch in git::
git push origin origin:refs/heads/my_branch
… or even simpler and more straightforward:
git branch my_branch
# create branch locally
git push origin my_branch
# push it to server
2. To push your changes to the remote branch:
git push origin my_branch
3. To delete the remote git branch:
git push origin :my_branch
or the “formal” version
git push origin :heads/my_branch
NOTE: The difference between create/update and delete is 1 char – the colon (“:”), so be careful what you’re typing or you can easily end up deleting a remote branch instead of pushing changes into it.
NOTE: What the delete command does is tell the remote git machine to push “nothing” (the lack of any name before the “:”) into my_branch, which essentially removes it. A bit convoluted, but hey – that’s git here… 🙂
4. Tracking and changing code in remote branches.
If someone else has already created a branch on the remote machine and you now have to work with it and add changes to it, then you’ll need to create a local “tracking” branch, which will mirror or “track” the remote branch. You do all your changes on the local branch, then push those changes to update the remote server.
Here’s how:
$ git clone me@git-server:/path/to/repo.git
$ git branch -r # List all remote branches
$ git checkout --track -b their_branch origin/their_branch
$ ... # do your work
$ git fetch origin # sync to server before pushing.
$ git push origin their_branch # push your origin
Alternatively, instead of git clone.. you could do git fetch origin if you already have the repository downloaded. This will avoid re-downloading the entire tree, most of which you already have. Similarly, in the step before push you can do git pull origin their_branch to sync just their_branch.
5 Responses to Git remote branches: how to create, track, remove/delete
Leave a Reply 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
At times you may have local and remote branches which are named similarly or the same and you may be unsure if the local branch is tracking the remote one. In particular, using SmartGit it is very easy to locally create a branch and and push it to the remote and yet would not be a tracking branch.
To check whether your local branches are tracking or not you may use either of these two commands:
git config -l
or
git remote show origin
… and to create a remote repository:
———————————-
1. Setup the location on the remote server
ssh user@remote-git-server
mkdir new_project.git
cd new_project.git
git init –bare
exit
2. Setup the local git repository:
cd new_project
git init
git add *
git commit -m “Initial commit”
git remote add origin user@remote-git-server:my_project.git
3. Upload your code to the server:
git push -u origin master
… and if your “remote” repository is really a “local remote” e.g. you are storing the linux kernel git tree on a machine on your network which is not your developer machine but will serve as a git server for your team (IMPORTANT NOTE: in this case the name ‘origin’ is likely lalready taken so use another name for your ‘local remote’ server):
1. get the project onto your workstation:
cd my_projects_dir
git clone user@real-remote:/dir/to/the_project_dir
2. Ssh to your local-remote server and setup location for the project (same as above, use ‘–bare’ and so on)
3. on the workstation add a new remote (this will be your local-remote server):
git add remote our-git-server me@our-organization-git-server:/git/the_project_dir
4. upload
git push –all our-git-server
Your note says “The difference between create/update and delete is 1 char – the column, so be careful…”. I think you meant to say “colon”, not “column”. It took me a minute or two to figure out what you meant, but I see it now. Thanks for posting this. It’s just what I needed.
I think you’re right. Corrected. Thank you 🙂