Pro Git - Chapter 3

Chapter 3

Git Branching

When you commit in Git, Git stores a commit object that contains a pointer to the snapshot of the content you staged, the author and message metadata, and zero or more pointers to the commit or commits that were the direct parents of this commit: zero parents for the first commit, one parent for a normal commit, and multiple parents for a commit that results from a merge of two or more branches.





Git refers to stored files as blob.
 When you create the commit by running git commit, Git checksums each sub-directory and stores those objects in the Git repository.
 Git then creates a commit object that has the metadata and a pointer to the root project tree.

After two more commits, your history might look something like





A branch in Git is simply a lightweight movable pointer to one of these commits. The default branch name in Git is master.
  As you initially make commits, you’re given a master branch that points to the last commit you made. Every time you commit, it moves forward automatically.
 

 Creating a branch creates a new pointer at the same commit you’re currently on
 


How does Git know what branch you’re currently on? It keeps a special pointer called HEAD. HEAD file pointing to the branch you’re on.


The git branch command only created a new branch — it didn’t switch to that branch.

To switch to an existing branch, you run the git checkout command.
$ git checkout branch_name 
 
This moves HEAD to point to the branch_name.

If you do commit on the new branch, the branch that HEAD points to moves forward with each commit.


but your master branch still points to the commit you were on when you ran git checkout to switch branches.
Now, if you switch back to the master branch
 

The HEAD pointer points to the master branch, and it reverted the files in your working directory back to the snapshot that master points to.
If you make a few changes and commit again
 

Because a branch in Git is in actuality a simple file that contains the 40 character SHA-1 checksum of the commit it points to, branches are cheap to create and destroy. Creating a new branch is as quick and simple as writing 41 bytes to a file (40 characters and a newline).

Basic Branching and Merging

Basic Branching

Create a branch for temporary work:

$ git checkout -b branch_1
 
Merge it back into your master branch:

$ git checkout master

$ git merge branch_1

Delete the temporary working branch:

$ git branch -d  branch_1

Basic Merging

If the commit on the branch you’re on isn’t a direct ancestor of the branch you’re merging in, Git has to do some work.






Git automatically creates a new commit object that contains the merged work.


Basic Merge Conflicts

Occasionally, this process doesn’t go smoothly. If you changed the same part of the same file differently in the two branches you’re merging together, Git won’t be able to merge them cleanly , you’ll get a merge conflic.

If you want to see which files are unmerged at any point after a merge conflict, you can run git status.

After you’ve resolved each of these sections in each conflicted file, run git add on each file to mark it as resolved.

If you verify that everything that had conflicts has been staged, you can type git commit to finalize the merge commit.

Branch Management


Get a simple listing of your current branches:

$ git branch

To see the last commit on each branch,
 
$ git branch -v

To see which branches are already merged into the branch you’re on,

$ git branch --merged

To see all the branches that contain work you haven’t yet merged in,

$ git branch --no-merged

To delete a branch,

$ git branch -d branch_name
 
 

Branching Workflows

When you’re branching and merging, everything is being done only in your Git repository — no server communication is happening.

Remote Branches

If you have a Git server on your network at git.ourcompany.com . If you clone from this, Git automatically names it origin for you, pulls down all its data, creates a pointer to where its master branch is, and names it origin/master locally; and you can’t move it. Git also gives you your own master branch starting at the same place as origin’s master branch.
 
 Working locally and having someone push to your remote server makes each history move forward differently.

 
 To synchronize your work, you run a git fetch origin command. This command looks up which server origin is (in this case, it’s git.ourcompany.com), fetches any data from it that you don’t yet have, and updates your local database, moving your origin/master pointer to its new, more up-to-date position.
 


You can add another remote git server by running the git remote add command.
git remote add server_name URL

You can run git fetch server_name to fetch everything the remote server_name server has that you don’t have yet.

Pushing

When you want to share a branch with the world, you need to push it up to a remote that you have write access to.

git push (remote) (branch)

Tracking Branches

Checking out a local branch from a remote branch automatically creates what is called a tracking branch. Tracking branches are local branches that have a direct relationship to a remote branch.

When you clone a repository, it generally automatically creates a master branch that tracks origin/master. That’s why git push and git pull work out of the box with no other arguments.

Deleting Remote Branches

git push [remotename] :[branch]

Rebasing

In Git, there are two main ways to integrate changes from one branch into another: the merge and the rebase.

Merge performs a three-way merge between the two latest branch snapshots and the most recent common ancestor of the two, creating a new snapshot (and commit).

However, there is another way: you can take the patch of the change that was introduced in one branch and reapply it on another branch. In Git, this is called rebasing.

There is no difference in the end product between merging and rebasing, but rebasing makes for a cleaner history.

$ git checkout branch_name
$ git rebase master

More Interesting Rebases

To be continue...

The Perils of Rebasing

To be continue...















留言

熱門文章