I have been using git for a while now, and I believe I have the the basic workflow under control. Committing, reverting, using local branches for major refactoring work: been there, done that!
However, I recently got some collaborators on my github-projects, I had to start working with other remote repositories and branches.
I found this blog post, which was really helpful. I am sharing some others things that helped me in the last couple of weeks. Hopefully, this saves other people some time Googling. If you know a better ways to accomplish these tasks, please let me know!
Things to remember about remote branches
Because I had some troubles discovering how to properly work with remote repositories, I am sharing what I found. The most important things to remember:
- Never forget to switch to the correct local branch (using
git checkout local-branch). This is easier if you setup your command prompt to include your current branch.
- The names of the local and remote do not need to match, but it is highly recommended if you do not want to go crazy
.
- Before you can push changes from a local branch to a remote branch, all the commits of the remote branch have to be included in your local branch. This can be done using by executing
git pull remote-name remote-branch in your local branch.
Checking out a newly created branch in a remote repository
Bart is implementing Merb log parsing for request-log-analyzer. He has put his progress in a separate branch of the github project. My local repository does not yet include this branch, but I want to check it out. Note that I am using a different name than the branch name on the github project.
$ git branch merb origin/generic_base
$ git checkout merb
Update: The same can be accomplished with a single command, which sets up remote tracking as well:
$ git checkout -b merb origin/generic_base
When the new functionality is finished, the following commands will merge the changes in the merb branch to the master branch.
# goto my local master branch and merge the merb-branch
$ git checkout master
$ git merge merb
# push the changes to the master branch on github
$ git push origin master
Merging back a fork
Wes Hays is helping me out on the scoped_search plugin. He implemented OR in the query language in his own fork. I wanted to merge his changes back to master branch:
# add a reference to the remote repository
# and fetch the latest data from that repository
$ git remote add gbdev git://github.com/gbdev/scoped_search.git
$ git fetch gbdev
# create a local branch for the fork to follow a remote branch
$ git branch gbdev-fork gbdev/master
$ git checkout gbdev-fork
Now, my local gbdev-fork branch contains Wes’s code. Because Wes’s repository was forked from my repository, git will know that most of the history of my master branch and gbdev-fork branch is the same.
After some testing, I was ready to include his changes by merging the gbdev-fork branch into my local master branch:
# go back to my master branch, and merge the changes
$ git checkout master
$ git merge gbdev-fork
# push the changes to the master branch at hithub
$ git push origin master
Update: I found out that you do not have to create a local branch when merging a remote branch. You can do read-only work directly on the remote branch:
# Add a new remote to your repository and fetch updates
$ git remote add gbdev git://github.com/gbdev/scoped_search.git
$ git fetch gbdev
# Checkout the remote branch for testing (read-only)
$ git checkout gbdev/master
# After successful testing, merge the branch into the master branch:
$ git checkout master
$ git merge gbdev/master
$ git push origin master
Pushing tags to a remote repository
You can create tags locally, but you probably want to send them to the remot repository as well:
# create a local tag "tagname" with the given message.
$ git tag -a "tagname" -m "message"
# send your tags to the remote repository "origin"
$ git push origin --tags
Deleting tags
Sometimes, you want to remove a faulty tag. If you already pushed your tags to a remote repository, you probably want to delete it from that repository, too,
# remove local tag
$ git tag -d tagname
# remove tag from remote using colon syntax
$ git push :tagname