Shawn's amazing git tutorial

Download Shawn's amazing git tutorial

Post on 15-Jul-2015

23 views

Category:

Software

0 download

Embed Size (px)

TRANSCRIPT

Git is composed of commits. When you commit code you are making a copy of the code that you can retrieve anytime no matter how old it is or how big or complicated the history gets.

A git commit is identified by a long random-looking string of characters called the SHA ID. It looks like this: a116bec2f74dbf3d0c5cf8322837043221b1b0d5

Each commit also has a comment associated with it that you write.

Commits are sequential, but can be reordered with a little work. (I wont cover that. If you need to reorder the commits let me know and Ill help)

ABCThis is how I will represent commits. Each circle is a revision and you can retrieve any of these three you want, change it, branch from it, move it around in the history, etc.

Each of these commits would have its own unique SHA ID and associated comment.OldestNewestGit commitsHead add and commitBranches checkout -b, mergereset vs. checkoutremote repopush vs pulldiffABCSworkIn addition to commits, git also has a staging area.

When you make changes to your code it is just lingering in the working area until you use this command:

git add -A .

to add it to the staging area.

The staging area is temporary and cannot be shared with other people. It is meant for you to keep tweeking the changes before youre ready to use this command:

git commit -m [comment]

to create a commit from the staging area.

You can always see the state of the working area and the staging area with this command:

git status

This command would not show any info about commits.

You can see the state of the commits using this command:

git log -5

Change the number 5 to see more or fewer commits.

ABCSwork}}ABCHEADGit also keeps track of which commit you are working from with a pointer called the HEAD (all caps).

If you want to work from a different commit you use this command to move the HEAD:

git reset --hard [B-SHA ID]

In this case perhaps you want to move the head to commit-B. You would issue a reset with the SHA ID for commit B.

ABCHEADABBut if you make changes now you risk losing commit C unless you write down the SHA ID for it.workC?HEADABCHEADIn addition to the HEAD pointer Git also has branches. You are always on a branch. The default branch is named master.

Here HEAD and master are pointing to the same commit.

You can create a new branch with this command:

git checkout -b NewBranch

Now HEAD pointer and both branches point to the C commit.

Then you can make changes and commit them to the new branch:

git add -A .git commit -m Branch on the new branch.

masterABCHEADmasterNewBranchABCHEADmasterNewBranchDAt some point youll want to merge your new branch in with the master or another branch. You use this command to do that:

git checkout [destination branch]git merge [source branch]

ABCHEADDestSourceDEFXYABCHEADDestSourceDEFXYABCHEADDestSourceDEFXY