Snapshot 1
Commit
Takes snapshot of your current work state
Snapshot 1
Commit
Commit
Snapshot 2
Snapshot 1
Commit 1
cd3 - sha
Snapshot 2
Commit 2
bc9 - sha
Uniquely Identify a work state
$ sudo apt-get install git
Boom! You are ready to go
Set name
$ git config --global user.name "Ankur Garg"
Set email
$ git config --global core.editor "vim"
Set editor
$ git config --global user.email "ankurgarg101@gmail.com"
Check config
$ git config --global --edit
Check status
$ git status
Add a file to staging area
$ git add .
Adds all file in repo to staging area
$ git add filename
Adds mentioned file to staging area
$ git commit -m 'Describe what you changed here, this is called commit message'
$ git commit --amend
$ git diff
$ git log
So HEAD is a pointer that points to snapshot we are at in repository
$ git log
commit 19f2a02d7caa83824dd774133e301a516cd0cd24
Author: Ankur Garg <ankurgarg101@gmail.com>
Date: Wed Jan 28 14:16:40 2015 +0530
Edits config
commit 51cd5f0a7cd9183a7b56958b1e400b495f103ffc
Author: Ankur Garg <ankurgarg101@gmail.com>
Date: Wed Jan 28 14:16:15 2015 +0530
Add main helper
commit 00d06580e52a54376c68440fbbfce5e09e35e81c
Author: Ankur Garg <ankurgarg101@gmail.com>
Date: Wed Jan 28 13:47:33 2015 +0530
Add index.md
Edits config - HEAD
Add main helper - HEAD^
Add index.md - HEAD~2
$ git reset --mixed HEAD
Restores current state of project to a desired state
Unstage files from index and reset pointer to HEAD
$ git reset --soft HEAD
Moves HEAD to specified commit reference, index and staging are untouched
$ git reset --hard HEAD
Removes all changes and reset pointer to HEAD
$ git status -s
M README
M hello.rb
$ git add .
$ git status -s
M README
M hello.rb
$ git reset HEAD -- hello.rb
Unstaged changes after reset:
M hello.rb
$ git status -s
M README
M hello.rb
$ git status -s
M hello.rb
$ git commit -am 'hello with a flower'
[master 5857ac1] hello with a flower
1 files changed, 3 insertions(+), 1 deletions(-)
$ git status
# On branch master
nothing to commit (working directory clean)
$ git reset --soft HEAD~
$ git status -s
M hello.rb
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: README
#
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: README
#
$ git reset --hard HEAD
HEAD is now at 5857ac1 hello with a flower
$ git status
# On branch master
nothing to commit (working directory clean)
Shows the list of branches
master is the default branch in a new repo
Creates a branch with specified branchname
$ git checkout master
$ git rebase branchname
Suppose there were 3 commits originally A, B and C:
Then developer Santa created commit D, and developer Banta created commit E:
Obviously, this conflict should be resolved somehow. For this, there are 2 ways:
We create commit R, in which actual file content is identical to that of merge commit M above. But, we get rid of commit E, like it never existed. So simply HEAD is moved forward to include E in R
Remotes
Default remote is named as origin by git
$ git clone git@github.com:sdsmdg/cognizance.git
$ git clone http://github.com/sdsmdg/cognizance.git
Can use various protocols like SSH, HTTP to clone
(Default to origin)
$ git fetch
$ git fetch remote_name
Can use various protocols like SSH, HTTP to clone
(Defaults to origin)
$ git pull
Updating 853a38b..30eb5d8
Fast-forward
config/config.php | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
$ git pull remote_name
(Defaults to origin and current branch)
$ git push
Counting objects: 25, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (25/25), done.
Writing objects: 100% (25/25), 2.43 KiB, done.
Total 25 (delta 4), reused 0 (delta 0)
To git@github.com:ankurgarg101/muzi-android.git
* master -> master
$ git push remote_name branch_name
$ git push origin master
To git@git.sdslabs.co.in:muzi.git
! [rejected] origin/master -> origin/master (non-fast-forward)
error: failed to push some refs to 'git@git.sdslabs.co.in:muzi.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
$ git pull
Updating 853a38b..30eb5d8
Fast-forward
config/config.php | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
$ git push
Updated origin/master -> origin/master
$ git remote -v
$ git remote add remotename some_url_here
List all remotes
Adds a remote
$ git clone git@git.sdslabs.co.in:muzi
Cloning into 'muzi'...
remote: Counting objects: 10058, done.
remote: Compressing objects: 100% (4088/4088), done.
remote: Total 10058 (delta 6184), reused 9482 (delta 5739)
Receiving objects: 100% (10058/10058), 28.49 MiB | 3.04 MiB/s, done.
Resolving deltas: 100% (6184/6184), done.
Checking connectivity... done.
$ ls
muzi
$ cd muzi
$ ls
admin ajax api.php chrome config css docs favicon.ico Gruntfile.js images js layout.html node_modules
package.json readme.mkd requests.php schema social stats swf tests vendor widget zune
$ git remote -v
origin git@git.sdslabs.co.in:muzi (fetch)
origin git@git.sdslabs.co.in:muzi (push)
$ git remote add gh git@github.com:apsdehal/muzi.git
$ git remote -v
gh git@github.com:apsdehal/muzi.git (fetch)
gh git@github.com:apsdehal/muzi.git (push)
origin git@git.sdslabs.co.in:muzi (fetch)
origin git@git.sdslabs.co.in:muzi (push)
git init
git clone
git add
git commit
git branch
git checkout
git remote
git merge
git rebase
git fetch
git pull
git log
git diff
git push
14