Notes from Code School's Git Real Course - Level 5 (Branching and Tags)
Remote Branching
Sometimes you need someone else to work on a branch, so you'll need to push it to a remote repository so that the other person can pull it down.
Another reason to use remote branching is to back up your work, especially important if you will be working on a branch for more than 1 day.
Creating a Remote Branch
For example, if you want to create a remote shopping cart branch.
$ git checkout -b shopping_cart
This will create and shopping_cart branch and switch you to it in one step.
$ git push origin shopping_cart
This will link your local shopping_cart branch to a remote branch on origin (assuming your remote repository is named 'origin'). This will automatically call your remote branch shopping_cart (same as local branch) and begin tracking it.
Posting to a Branch
Let's say you create a file called cart.rb that needs to be posted to your remote branch. First commit it to your local branch, then push to remote.
$ git add cart.rb
- adds cart.rb to staging area
$ git commit -m "Add cart"
$ git push
(Because you already set it up and its being tracked, git knows to push the local shopping_cart branch to the remote shopping_cart branch.)
Now, if you go to Github.com, look at the repository and click the button 'branch master':
You will see a list of all your remote branches. If you click the remote branch, for example shopping_cart, you'll see all the files and the last commit.
Pulling New Branches
Now, if someone else wants to pull a branch (assuming they are already working on the project and have the cloned repository on their machine and 'bookmarked' it, see LEVEL 2), they run:
$ git pull
The output dialog will show a new remote branch exists.
$ git branch -r
Will show all the remote branches available
$ git checkout <name of remote branch>
Will automatically setup the remote branch as a local branch of master with the same name.
e.g. $ git checkout shopping_cart
Running $ git remote show <remote repository name> will show all remote branches, plus it will show local branches (tracked or not), plus the remote branches these local branches point to(merge with). ALSO, running $ git remote show will trigger the server to go out to the remote and check to see if all the local branches are up to date.
E.g. $ git remote show origin
Removing a Branch (Remote and/or Local)
To remove a remote branch:
$ git push origin :<remote branch name>
e.g. $ git push origin :shopping_cart
But, this only deletes the remote branch. If you want to delete the local branch as well run this after the above command.
$ git branch -d <local branch name>
e.g. $ git branch -d shopping_cart
If you have any commits on your local branch that have not been pushed to the remote version, git will throw an error message telling you so. Maybe you forgot to push your commits to the remote repository? If you still want to delete the local branch at this point, run:
$ git branch -D <local branch name> (capital "D")
e.g. $ git branch -D shopping_cart
What happens when you do to push your commits to a remote branch, but that branch has been deleted by someone else?
Let's say you stage and commit a new file:
$ git commit -a -m "new cool feature that I created, but got canceled and no one told me"
$ git push
BUT NOTHING HAPPENS! What do you do? Run:
$ git remote show origin (assuming your remote repository is called 'origin')
Git will output a dialog showing a list of your remote branches and their status.
Remote branches:
master
refs/remotes/origin/shopping_cart "stale"
This message of "stale" means that someone else has deleted the branch. After having a stern word with your coworkers about canceling projects without telling you, you should go in and "prune" the stale branch. This will delete the branch from the list of available branches, and keep your repository clean.
$ git remote prune origin
It is a good idea to run this command every once and a while if you're working on a large project with many collaborators.
Working with Heroku and Branches
Lets say that you are using Heroku as a test or staging server. You create a branch called 'heroku-staging' (because you want to be painfully obvious about what the branch is for). But when you try to push this branch to Heroku, (the remote branch is called 'staging') nothing happens. Why? Because Heroku only deploys the branch named 'master'. How to fix this:
$ git push heroku-staging staging:master
This tells Heroku to set 'staging' as the 'master', and now deployment will work.
Tagging
A tag is a reference to a commit and is used most commonly for release versioning. I think of it as a snapshot of all your code at a given time. You can tag your code, make changes to your code, then go back later to that tag, and you will be able to see it exactly as it was before those post-tag changes were made.
$ git tag
Will show a list of existing tags (if any exist)
e.g. $ git tag might output v0.0.1 and v0.0.2
$ git checkout v0.0.1
This will let you switch to the version of the code as it existed at the time 'v0.0.1' was created.
How to Add a New Tag
$ git tag -a <tag> -m "a description of the tag goes here"
e.g.
$ git tag -a v0.0.3 -m "Version 0.0.3"
$ git tag -a beta -m "Beta Release"
To push your tags to a remote repository, you need to specifically push tags
$ git push --tags
If you want to see your tags in the remote repository on Github, go back to Github.com and click the 'branch master' button: 

There you can click the tags item to see a list of tags, and then click on the tag to see the code as it was at the time it was tagged.
![]() |
branches/tags menu from github |
No comments:
Post a Comment