By Julien Muetton / @themouette
Refer to online documentation
To get some help on a command.
$ man git-commit $ git help $ git help commit
Git Book is a golden mine too.
$ cd ~ $ ssh-keygen -t rsa $ cat id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAyyA8wePstPC69PeuHFtOwyTecByonsHFAjHbVnZ+h0dpomvLZxUtbknNj3+ c7MPYKqKBOx9gUKV/diR/mIDqsb405MlrI1kmNR9zbFGYAAwIH/Gxt0Lv5ffwaqsz7cECHBbMojQGEz3IH3twEvDfF6cu5p 00QfP0MSmEi/eB+W+h30NGdqLJCziLDlp409jAfXbQm/4Yx7apLvEmkaYSrb5f/pfvYv1FEV1tS8/J7DgdHUAWo6gyGUUSZ JgsyHcuJT7v9Tf0xwiFWOWL9WsWXa9fCKqTeYnYJhHlqfinZRnT/+jkz0OZ7YmXo6j4Hyms3RCOqenIX1W6gnIn+eQIkw== my computer
Then add the public key content to github, bitbucket, gitlab or any other server and test everything is all right.
$ ssh git@github.com Hi themouette! You've successfully authenticated, but GitHub does not provide shell access.
$ git config --global user.name "John Doe" $ git config --global user.email johndoe@example.com
Default is vi, but if you really want to change:
$ git config --global core.editor emacs
$ git config --global core.editor \ "'C:/Program Files/Notepad++/notepad++.exe' \ -multiInst -notabbar -nosession -noPlugin"
$ git config --global merge.tool vimdiff
Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later.
Everything is local, remote operations are only for sharing.
Create an empty git repository or reinitialize an existing one.
$ git init Initialized empty Git repository in ~/.git/
Running git init in an existing repository will not overwrite things that are already there.
A commit is a snapshot of your working tree and a pointer to previous commit.
Everything in GIT is in one of those following area.
Add file contents to the staging area.
$ git add README
Track a file for the first time.
Track changes on file since previous commit.
Checkout a branch or paths to the working tree.
$ git checkout README $ git checkout my-branch
Reset file/path to staging area's version.
Switch branch.
Create a commit from current staging area's status.
$ git commit [master 8a6e1a3] My awesome message.D 2 files changed, 10 insertions(+), 4 deletions(-) create mode 100644 img/01/branch.png
Returns the commit hash 8a6e1a3.
To edit your latest commit, use git commit --amend.
Show current working dirrectory and staging area.
Rename src into destination.
Delete file from tracking.
Show commit logs.
$ git log commit 8fbdc87e2f254c1c52ee9c2cae886985421fd3ef Author: Julien Muetton <themouette@gmail.com> Date: Mon Aug 26 13:30:52 2013 +0200 Init as an npm package commit f8db4d12eea0174b8e8476c21510133e3bbd27e3 Author: Julien Muetton <themouette@gmail.com> Date: Mon Aug 26 13:24:20 2013 +0200 First commit
$ git log --graph --decorate # Display network graph
$ git log --stat # show stats
$ git log --pretty=oneline --abbrev-commit # A commit per line
Show changes between two states (commits, staging, working directory...)
$ git diff diff --git a/index.html b/index.html index de3bfb6..b3d2ddf 100644 --- a/index.html +++ b/index.html @@ -36,10 +36,16 @@ some awesome content - some former content + some new content
git diff topic..master to compare branches.
git diff HEAD--8a6e1a3 to compare commits.
Stash the changes in a dirty working directory away.
$ git stash
git stash list to list stashs.
git stash apply to apply latest stash.
git stash pop to apply and delete latest stash.
Show what revision and author last modified each line of a file.
$ git blame README 2c2513c7 (Julien Muetton 2013-08-26 18:48:45 +0200 1) # GIT basics 2c2513c7 (Julien Muetton 2013-08-26 18:48:45 +0200 2) 2c2513c7 (Julien Muetton 2013-08-26 18:48:45 +0200 3) --- 2c2513c7 (Julien Muetton 2013-08-26 18:48:45 +0200 4) 2c2513c7 (Julien Muetton 2013-08-26 18:48:45 +0200 5) ## Local first
A Tag is a label on a commit.
The top of a linked list has a name: the BRANCH name.
List, create, or delete branches
(master) $ git branch feature (master) $ git branch * master feature (master) $ git branch -d feature
Only local branches can be managed
Switch working directory to my-branch
(master) $ git branch feature (master) $ git checkout feature (my-branch) $ git branch master * feature
You can use shortcut git checkout -b my-branch
Replay the current branch on top of master.
(feature) $ git rebase master
If merge is not obvious, git asks you to solve conflict manually.
Join two or more development histories together.
(master) $ git merge feature
(master) $ git merge feature
(master) $ git merge feature --no-ff
Using --no-ff keeps track of the feature branch.
Pick a commit and apply it on current HEAD.
(master) $ git cherry-pick 8a6e1a3
$ git merge iss53 Auto-merging index.html CONFLICT (content): Merge conflict in index.html Automatic merge failed; fix conflicts and then commit the result. $ git diff <<<<<<< HEAD:index.html <div id="footer">contact : email.support@github.com</div> ======= <div id="footer"> please contact us at support@github.com </div> >>>>>>> iss53:index.html
Git is verbose, but for your first conflict...
A remote is a mirror of your local repository.
Clone a repository into a new directory.
$ git clone git@github.com/themouette/fossil-core.git (master) $ git branch -a * master remotes/origin/HEAD -> origin/master remotes/origin/master
You can use http(s) or ssh to communicate with remotes.
Default remote is names origin.
Manage remote mirrors.
$ git remote origin $ git remote show origin * remote origin Fetch URL: git@github.com:themouette/fossil-core.git Push URL: git@github.com:themouette/fossil-core.git HEAD branch: master Remote branch: master tracked Local branch configured for 'git pull': master merges with remote master Local ref configured for 'git push': master pushes to master
You can add multiple remotes.
Download objects and refs from another repository.
$ git fetch origin
Note that nothing changes in your local direcory.
Fetch and merge or rebase remote branch.
$ git pull --rebase origin master
Equivalent to:
$ git fetch origin/master $ git rebase origin/master
Update remote refs along with associated objects.
$ git push origin serverfix Counting objects: 20, done. Compressing objects: 100% (14/14), done. Writing objects: 100% (15/15), 1.74 KiB, done. Total 15 (delta 5), reused 0 (delta 0) To git@github.com:schacon/simplegit.git * [new branch] serverfix -> serverfix
Only fast forward commits can be pushed
Avoid submodules
git submodule add https://github.com/hakimel/reveal.js.git reveal.js git submodule update --init
Extend Git behaviour
(when using git am)
Learn more on Git Hooks
Similar to centralized version control, everybody can edit central repository.
Developer create a request to merge it's changes.
Release masters chose which requests to merge.
In huge projects, lieutenants are trusted individuals in charge of reviewing requests.
.git |-- branches/ |-- hooks/ |-- info/ |-- logs/ |-- modules/ |-- objects/ |-- refs/ |-- COMMIT_EDITMSG |-- config |-- description |-- HEAD |-- index `-- packed-refs
Each FILE that you add to the repo is turned into a BLOB object.
Each DIRECTORY is turned into a TREE object.
Obviously, a tree object can contain other tree objects and blob objects, just like a directory can contain other directories and files.
A commit is a snapshot of your working tree and a pointer to previous commit.