//

GIT – CLONING & BRANCHING

Imagine you are working in a team and one of your colleagues wants a copy of the your work, how do we start collaborating?

CLONING A REPOSITORY

Cloning into 'git-remote':

git clone https://github.com/myurl/git-remote.git

Cloning into 'git-local'

git clone https://github.com/myurl/git-remote.git git-local

Here git-local is the local folder name. And https://github.com/myurl/git-remote.git is the URL of the remote repository.

GIT CLONE

  • Downloads the entire repository into a new git-remote directory.
  • Adds the ‘origin’ remote, pointing it to the clone URL.
git remote -v
origin https://github.com/myurl/git-remote.git (fetch)
origin https://github.com/myurl/git-remote.git (push)
  • Checks out initial branch (likely master) - This sets HEAD.

BRANCHING OUT

Need to work on a feature that will take some time?

git branch cat
git branch
cat
* master

This will create a branch called cat, which is created from master. But HEAD still is on master.

SWITCHING TO A BRANCH

Time to jump on that new 'cat' branch.

git checkout cat

Switched to branch 'cat'. Now the HEAD is on 'cat'

WORKING ON A BRANCH

echo "Schrödinger" > cat.txt
git add cat.txt
git commit -m "Create quantum cat."
[cat ab48a3f] Create quantum cat.
1 file changed, 1 insertion(+)
create mode 100644 cat.txt

got commit will commit to the cat branch.

ls
README.txt cat.txt

BACK TO MASTER

Let's switch to branch 'master'

git checkout master
HEAD

And we're back on master. (The HEAD is on 'master' now)

ls
README.txt  

As you can see 'cat.txt' is no gone.

BACK TO CAT

Now let's switch to branch 'cat'

git checkout cat
ls
README.txt cat.txt

'cat' is still there.

TIME TO MERGE

Done with that feature branch? Time to merge it into 'master '.

git checkout master
Switched to branch 'master'
ls
README.txt

Here we can't see 'cat' as expected.

git merge cat
Updating 1191ceb..ab48a3f
Fast-forward
cat.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 cat.txt

Merge brings one branch’s changes into another.

FAST-FORWARD TO THE FUTURE

Conditions for a fast-forward merge:

The branch created should have something new on it and the master should NOT have any changes since the branch has been made to do a fast-forward merge.

BRANCH CLEAN UP

When you're done with a branch, you can safely remove it.

git branch -d cat
Deleted branch cat (was 957dbff).

NON-FAST-FORWARD

In this case, the history looks slightly different, there is an additional commit emphasizing the merge. This commit even has the right message informing us about the merged branch. Let’s work on a new admin feature.

git checkout -b admin
Switched to a new branch 'admin'
git add admin/dashboard.html
git commit -m 'Add dashboard'
git add admin/users.html
git commit -m 'Add user admin'

BUG FIXING ON MASTER

Time to put out the fire. We’ll get back to that admin branch later.

git checkout master master
Switched to branch 'master'
git branch
admin
* master
git pull
...
git add store.rb
git commit -m 'Fix store bug'
git add product.rb
git commit -m 'Fix product'
git push    

BACK TO OUR BRANCH

git checkout admin
Switched to branch 'admin'
...

When ready to bring in changes:

git checkout master
Switched to branch 'admin'
git merge admin

AND SUDDENLY...

Git uses Vi if no default editor is set to edit commit messages.

1 Merge branch 'admin'
2
3 # Please enter a commit message to explain why this merge is necessary,
4 # especially if it merges an updated upstream into a topic branch.
5 #
6 # Lines starting with '#' will be ignored, and an empty message aborts
7 # the commit.
:wq+ 

Hit Enter to write (save) & quit

Vi Commands

h left 
l right
j down 
k up 
ESC leave mode
i insert mode
:wq save & quit
:q! cancel & quit   

RECURSIVE MERGING

Git can’t fast-forward since changes were made in both branches.

Merge made by the 'recursive' strategy.
0 files changed
create mode 100644 admin/dashboard.html
create mode 100644 admin/users.html 

A commit was created to merge the two branches.

git log
A commit was created to merge the two branches.
commit 19f735c3556129279bb10a0d1447dc5aba1e1fa9
Merge: 5c9ed90 7980856
Author: Sean <sean@seanamarasinghe.com>
Date: Thu Dec 07 17:51:53 2014 -0400
Merge branch 'admin'    

If you want to learn about more follow my other articles on Git here: