Git - Quick Intro

Git

Resource states

  • Committed Modified file stored in the local database
  • Modified File changed but not committed yet
  • Staged Modified file marked to be committed

enter image description here

Setting up the local configuration

git config --global user.name "Tib Onu"
git config --global user.email "tib@byteguild.com"
git config --global merge.tool bc3
...
git config --list

Bringing over a remote repository

git clone git://github.com/schacon/grit.git mygrit

Unmodifying a file

git checkout -- README

Copies the previously committed version of the file over

Staging a file

git add README

Undoing a staged file:

git reset HEAD README

Compare working directory with staging area

git diff
git diff --staged 

Committing changes

git commit

To overwrite the previous commit:

git commit --amend

Changes go from the staging area to the local repository.

Renaming / moving files

git mv README README1

Showing the commit history

git log

Remotes

Adding a remote:

git remote add tib https://github.com/tibonu/test

Removing a remote:

git remote rm tib

Renaming a remote:

git remote rename tib tonu

Showing the remotes:

git remote -v

Fetching from a remote:

git fetch remote-name

Pulls the new versions from the remote to the local repository. Doesn't merge automatically the working area.

Pulling from a remote:

git pull remote-name

This is a combined fetch and merge.

Pushing to remotes:

git push origin master

origin is the remote master is the branch

Inspecting a remote:

git remote show origin

Tags

Tag types

  • Lightweight
  • Annotated

Listing the tags

git tag

Creating an annotated tag

git tag -a v1.01 -m 'Tib version 1.01'

Creating a lightweight tag

git tag v1.02

Tags can be created for older commits, after the fact.

Sharing tags

Tags aren't pushed by default to remotes, must be pushed specifically:

git push origin v1.5

Pushes tagged version 1.5 including the tag.

git push origin --tags

Pushes all the tags.

Aliases

git config --global alias.co checkout
git.config --global alias.last 'log -1 HEAD'
git config --global alias.unstage 'reset HEAD --'

Git filesystem

git add README

Git will:

  1. Compute the SHA1 of the README file: d6a6f0f84fe9bee72d5e5a547f5ed728661d348e
  2. Generate a BLOB of the README file (zlib) and store the file under objects/d6/a6f0f84fe9bee72...
  3. Add the reference to the README file to the index file
git commit -m 'my commit message'

Git generates and stores two more objects:

  • a (directory) tree object
  • a commit object
Generating the SHA1 of a file
git ls-files stagehash-object README

54126227aec78e482349aa07164ba3b5436523f0
Listing the index content (staged files)
git ls-files stage

100755 54126227aec78e482349aa07164ba3b5436523f0 0       README```
Dumping the content of an object
git show 54126227aec78e482349aa07164ba3b5436523f0 

Creating a branch

git branch testing

Creates the branch but doesn't switch to it.

Displaying the branches

git branch

* master
  testing

Verifying the current branch

git log --oneline --decorate

d6a6f0f (HEAD -> master, testing) initial commit of my project

Switching the branch

git checkout testing
git log --oneline --decorate

d6a6f0f (HEAD -> testing, master) initial commit of my project

HEAD now points to the testing branch

Making a change and committing while working on the branch

git commit -a -m 'made a change'
git log --oneline --decorate

2f27c95 (HEAD -> testing) made a change
d6a6f0f (master) initial commit of my project

HEAD points to the testing branch, but the current commit is different than the one on the master branch

Merge from a branch

git checkout master
git merge someotherbranch

Graphical merge

git mergetool

Rebasing

git checkout somebranch
git rebase master

The git rebase master command will generate a diff between the current version of master and the common ancestor commit of (master, somebranch) and will inject the changes as a new version in somebranch. After doing this, master can be merged by a fast forward.

Stashing

git checkout somebranch
vi README 
git checkout master

error: Your local changes to the following files would be overwritten by checkout: README
        Please, commit your changes or stash them before you can switch branches.
Aborting

You cannot switch the branch because you would lose your changes.

git stash 
git checkout master

This works.

git checkout somebranch
git stash list
git stash apply

git Open with Displaying git.