Tuesday, April 1, 2014

Git Real - Level 3

Notes from Code School's Git Real Course - Level 3 (Cloning and Branching)

How to Start Collaborating

If you or someone else needs to get files, you can clone the repository from GitHub. Go to GitHub GUI and find the URL and copy. To download the files, type into your command line tool:

$ git clone <url>

e.g.
$ git clone https://github.com/helengutz/sample_app.git
$ git clone https://github.com/codeschool/git-real.git

This will create a local repository. It will create a repository locally with the same name as the remote, as in the examples above it would be 'sample_app' and 'git-real'. If you wanted to change the name of the repository, you add the new name at the end of the git clone command.

e.g.
$ git clone https://github.com/helengutz/sample_app.git sample_app_copy

$ git clone https://github.com/codeschool/git-real.git git-demo


Now the local versions of these repositories are sample_app_copy and git-demo.

What Cloning Does

1. - Downloads the entire repository into a local folder
2. - Adds a remote bookmark called origin pointing to that URL

To see that the remote was created, you type:
$ git remote -v

3. Checks out(switches to) the initial branch, usually 'master',  and sets the HEAD

Branching Out

how to use git branch command
new git branch called cat
If you need to work on a new project or feature that will take some time, it is best to create a new branch. For example, if you wanted to create a new branch called 'cat'.

$ git branch <branch_name>

e.g.s

$ git branch cat
$ git branch blog
$ git branch new_layout
$ git branch store



This will create a new branch called cat. Although this branch exists, you have not yet switched to it.

Switching to a Branch


First look to see what branch you are currently on

$ git branch
- this will output the name of the branch you are currently on, in this case lets assume 'master'.
how to switch to a newly created branch
how to switch to a newly created branch
$ git checkout cat

The generic form would look like this:
$ git checkout <branch_name>


- this will switch you to the new branch.
- You are switching timelines
- The HEAD will now be on the new branch



Working on a Branch


Now that you are switched to the new branch, let's say you create a new file called 'cat.txt' and you'd like to add and commit it.
First add to the staging area per usual.

$ git add cat.txt

The generic form would look like this:
$ git add <file_name>

And commit

$ git commit -m "add cat.txt"

The generic form would look like this:
$ git commit -m "your message goes here"

You have now added a commit to the cat branch.
how to add a commit on a branch
commit added to cat branch


Now, if you want to see all the files on the current branch, simply do:

$ ls

This will output, all files (both from 'master' AND from 'cat'). In this example, README.txt and cat.txt.

BUT, if we switch back to the master branch, type $ git checkout master, and run $ ls again. The output will only be README.txt. What happened to cat.txt? It is still on the cat branch, and has not been merged into the master branch. Also, if you run $ git log, while on the master branch, your cat commit will not show up.

Time to Merge

Okay, let's say you're done with the cat project and are ready to merge it with the master branch. First you must make sure you are on the master branch. To do so, run:

$ git branch

 If you are on the master branch, you'll see an output something like this:
cat
*master

If you are on the cat branch, your output would look like this:
*cat
master

If you're on the cat branch, switch to the master branch by typing:
$ git checkout master

Next run: 
$ ls

This will show you the files currently in your master branch. In our example, we would expect to see README.txt only (no cat yet!).

$ git merge cat

- This will merge the cat branch with the master branch in "fast-forward" mode. 
- Fast-forward is when changes are made on one branch, and NO CHANGES are made on the master branch during that time. This makes merging very easy. 

You can now delete your project branch

$ git branch -d cat

To create a branch and switch to it in one step do:
$ git checkout -b <branch_name>

Git Branch Commands:

Create a branch:
$ git branch <branch_name>

Switch to branch:
$ git checkout <branch_name>

Find out which branch you're currently on:
$ git branch

Merge with master:
$ git checkout master
$ git merge <branch_name>

Delete branch:
$ git branch -d <branch_name>

Create and switch to branch in 1 step:
$ git checkout -b <branch_name>

Resolving Conflicts

Let's say you are working on a new feature project for an app, a new admin panel, and you make some new commits on a new branch called 'admin'. Here are the commits:
$ git add admin/dashboard.html
$ git commit -m "add dashboard"

$ git add admin/users.html
$ git commit -m "add admin panel"

But, maybe there is a bug that needs to be fixed right away. You need to switch from your admin branch to the master branch to make these fixes. You type:
$ git checkout master 
This switches you back to the master branch, but just to be sure you're in the right place, you do:
$ git branch
-This will output a dialog showing which branch you are currently on. No worries, you're on master!

Run:
$ git pull
- This will make sure you have all the current files before you start working.

Now you make your changes to fix the bugs. Your bug fixes look like this:
$ git add store.rb
$ git commit -m "fix store bug"

$ git add product.rb
$ git commit -m "fix product bug"

Now you push your changes to the master branch up to the remote repository. You type:
$ git push

Its all good, now your timeline looks like this:
merging git branches
merging branches

Dealing with Merge Conflicts

Now that your bug fixes have been pushed, you can return to your admin branch work. So you type, $ git checkout admin to switch back to your admin branch. You do some admin branch stuff like more commits, then decide it is ready to be merged with the master branch. First you must switch back to the master branch:
$ git checkout master

Then you run, $ git merge admin, to merge your admin branch with the master. BUT, this will trigger a conflict message displayed in the VI editor. You have made changes to the master branch and the admin branch simultaneously (well not literally, but you can see the timeline above and its close).

For the conflict message Git uses VI text editor as the default. To exit this editor type :wq + hit enter to save and quit.

Basically Git is saying, I see that you are trying to merge these branches, if you want to write a more specific message, do so here. When you're done with VI editor, do :wq + hit enter to write(save) and quit to return to your command line.

Here are some more helpful VI Commands:

j = down
k = up
h = left
l = right
i = insert mode
:wq = save and quit
:q! = cancel and quit

Note: At this point I don't understand the VI editor stuff, but Codeschool seems to think its important.

Recursive Merging

What Git is doing is called a recursive merge. It happens when git cannot use the fast-forward mode, when changes have been made on both the branch master and the new branch. It means that when the branches are merged back together, git creates a "merge commit". It is a commit in the timeline that shows that a merge happened. It will show that no files were changed, and that 2 branches were merged. If you run $ git log, the log will show the commit with a message saying that the 2 branches were merged at this point in time.

About this blog: This blog is an attempt to summarize my learnings as I learn them. Expect inaccuracies, incompleteness and inconsistencies.

No comments:

Post a Comment