Andrey Kucherenko

EPAM Systems

Patch theory

Glossary

Local Repository

A subdirectory named .git

Upstream Repository

Version(s) of your project that are hosted on the Internet or network

Workspace

local checkout

Index

snapshot of the content of the working tree

Stash

A place to hide modifications while you work on something else

Branch

active line of development

HEAD

The current branch

Online Envs

  • https://c9.io/
  • https://codenvy.io
  • https://codeanywhere.com/

Workspace

git init

$ git init
Initialized empty Git repository in /home/apk/workspace/lab/git/.git/

git status

$ git status
On branch master

Initial commit

nothing to commit (create/copy files and use "git add" to track)

Displays paths that have differences between the index file and the current HEAD commit, paths that have differences between the workspace and the index file, and paths in the workspace that are not tracked by git.

git log


$ git log --graph --oneline

$ git log --graph --oneline --decorate --date=relative --all

$ git log 
commit e05dddd8650c0bc42b69d8db6801306baf8a009e
Author: Andrey Kucherenko <kucherenko.andrey@gmail.com>
Date:   Tue Jun 9 20:12:49 2015 +0300

    test2.txt

Show recent commits, most recent on top. 
 

git log


$ git log --graph --oneline

$ git log --graph --oneline --decorate --date=relative --all

$ git log 
commit e05dddd8650c0bc42b69d8db6801306baf8a009e
Author: Andrey Kucherenko <kucherenko.andrey@gmail.com>
Date:   Tue Jun 9 20:12:49 2015 +0300

    test2.txt
  • --author=“Andrey" – Only show commits made by a certain author
  • --name-only – Only show names of files that changed
  • --oneline – Show commit data compressed to one line
  • --graph – Show dependency tree for all commits
  • --reverse – Show commits in reverse order (Oldest commit first)
  • --after – Show all commits that happened after certain date
  • --before – Show all commits that happened before certain data
1

git log -p filename

 

Log actual changes in a file

1
$ git log  -p gulpfile.js 
commit f54b138cab79abaf3385bbdf38c819a2b1ba1925
Merge: 94382df f5f2ede
Author: Lev Maltsev <Lev_Maltsev@epam.com>
Date:   Tue May 12 15:45:01 2015 +0300

    POE-3383: Merge branch 'POE-3383-shop-the-look' into develop
    
    Conflicts:
        cq/marks-view/src/main/content/jcr_root/etc/clientlibs/marks/common/scss/_vars.scss

git log -L 1,1:file.js

 

Only Log changes for some specific lines in a file

1
$ git log -L 1,1:gulpfile.js
commit f54b138cab79abaf3385bbdf38c819a2b1ba1925
Author: Lev Maltsev <Lev_Maltsev@epam.com>
Date:   Tue May 12 15:45:01 2015 +0300

    POE-3383: Merge branch 'POE-3383-shop-the-look' into develop
    
    Conflicts:
        cq/marks-view/src/main/content/jcr_root/etc/clientlibs/marks/common/scss/_vars.scss

git log --no-merges master..

 

Log changes not yet merged to the parent branch

1
i$ git log --no-merges master..
commit 7ea2dadeba0e2a193d20a0a9f70c43b536093680
Author: Dmytro Golysh <Dmytro_Golysh@EPUAKYIW1870.kyiv.epam.com>
Date:   Thu Oct 1 12:22:58 2015 +0300

    [POE-0000] Refactor pos-focus directive tests.

commit be12e938d6c2e8ede908460dab78f86e45cdfdac
Author: Ihor Herasymenko <ihor_herasymenko@epam.com>
Date:   Thu Oct 1 11:37:51 2015 +0300

    POE-0000: Add another bonus card

git diff

$git diff
diff --git a/README.md b/README.md
index e51914f..19f483c 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,4 @@
 Hello!
-======
\ No newline at end of file
+======
+
+123
\ No newline at end of file

Displays the differences not added to the index.

git diff -w or git blame -w

 

Ignore the white space​

 

git diff <commit or brunch>

$git diff HEAD
diff --git a/README.md b/README.md
index e51914f..19f483c 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,4 @@
 Hello!
-======
\ No newline at end of file
+======
+
+123
\ No newline at end of file

View the changes you have in your workspace relative to the named commit. You can use HEAD to compare it with the latest commit, or a branch name to compare with the tip of a different branch

git add <file or folder>

$ git add test.txt

$ git add test1.txt --interactive
           staged     unstaged path
  1:        +0/-0      nothing test1.txt

*** Commands ***
  1: status	  2: update	  3: revert	  4: add untracked
  5: patch	  6: diff	  7: quit	  8: help
What now> 

Adds the current content of new or modified files to the index, thus staging that content for inclusion in the next commit. Use add --interactive to add the modified contents in the workspace interactively to the index. Use git add -u for adding not new files changes to index.

git rm/mv <file (s)>

$ git rm test1.txt 
rm 'test1.txt'

$ git mv test.txt test.md

Remove a file from the workspace and the index. Move file in the workspace and the index.

git commit -a -m "msg"

$ git commit -a -m "message"
[master 023276e] message
 3 files changed, 3 insertions(+), 1 deletion(-)
 rename test.txt => test.md (100%)
 delete mode 100644 test1.txt

Commit all files changed since your last commit, except untracked files (ie. all files that are already listed in the index). Remove files in the index that have been removed from the workspace

git checkout <files or dir>

$ git status 
On branch master
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:   test2.txt

no changes added to commit (use "git add" and/or "git commit -a")
$ git checkout test2.txt
$ git status 
On branch master
nothing to commit, working directory clean

Updates the file or directory in the workspace. Does NOT switch branches.

git checkout <branch>

$ git checkout -b hello
Switched to a new branch 'hello'

$ git status
On branch hello
nothing to commit, working directory clean

$ git checkout master
Switched to branch 'master'

Switches branches by updating the index and workspace to reflect the specified branch, branch, and updating HEAD to be branch. For create and switch to new branch use git checkout -b <branch>

git reset --hard

$ git reset --hard                                                                                                                                                                                           
HEAD is now at 7ec78c4 merged conflicts with myNewBrunch

Matches the workspace and index to the local tree. WARNING: Any changes to tracked files in the working tree since commit are lost. Use this if merging has resulted in conflicts and you'd like to start over. Pass ORIG_HEAD to undo the most recent successful merge and any changes after.

git merge <commit or brunch>

$ git merge develop 
Merge made by the 'recursive' strategy.
 README.md | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Merge changes from branch name into current branch.
Use ‑‑no-commit to leave changes uncommitted.

git rebase <upstream>

$ git rebase develop
First, rewinding head to replay your work on top of it...

Reverts all commits since the current branch diverged from upstream, and then re-applies them one-by-one on top of changes from the HEAD of upstream. The golden rule of git rebase is to never use it on public branches.

git cherry-pick <commit>

$ git cherry-pick a0e1ccfbfbcbef2a7190bdc3d26041ec5f624410
error: could not apply a0e1ccf... fix readme
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'
$ git cherry-pick --continue 
U	README.md
error: commit is not possible because you have unmerged files.
hint: Fix them up in the work tree, and then use 'git add/rm <file>'
hint: as appropriate to mark resolution and make a commit, or use
hint: 'git commit -a'.
fatal: Exiting because of an unresolved conflict.
apk@apk-epam-laptop:~/workspace/lab/git-lessons$ git add -u README.md 
apk@apk-epam-laptop:~/workspace/lab/git-lessons$ git cherry-pick --continue 
[new 9635dfb] fixed readme
 Date: Wed Jun 10 19:02:47 2015 +0300
 1 file changed, 2 insertions(+), 2 deletions(-)

Integrate changes in the given commit into the current branch.

git revert <commit>

$ git revert 211bffb233d0a49388377da21f4214e00b667f03
error: could not revert 211bffb... added readme
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'
$ git revert --continue
On branch new
Your branch is ahead of 'origin/feature/myfeature' by 4 commits.
  (use "git push" to publish your local commits)
You are currently reverting commit 211bffb.

nothing to commit, working directory clean

Reverse commit specified by commit and commit the result. This requires your working tree to be clean (no modifications from the HEAD commit).

git clean

$ git clean
fatal: clean.requireForce defaults to true and neither -i, -n, nor -f given; refusing to clean
$ git clean -i
Would remove the following item:
  zzz.txt
*** Commands ***
    1: clean                2: filter by pattern    3: select by numbers
    4: ask each             5: quit                 6: help
What now> 1
Removing zzz.txt
$ git status 
On branch master
nothing to commit, working directory clean

Cleans the working tree by recursively removing files that are not under version control, starting from the current directory.

Index

git reset HEAD

$ git status 
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	modified:   test.md

$ git reset HEAD
Unstaged changes after reset:
M	test.md
$ git status 
On branch master
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:   test.md

Remove the specified files from the next commit. Resets the index but not the working tree (i.e., the changed files are preserved but not marked for commit) and reports what has not been updated.

git reset --soft HEAD^

$ git reset --soft HEAD^
apk@apk-epam-laptop:~/workspace/lab/git$ git status 
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	new file:   test2.txt

Undo the last commit, leaving changes in the the index.

git commit --amend

$ git commit --amend
[master bc86c40] added test2.txt
 Date: Tue Jun 9 20:06:52 2015 +0300
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test2.txt

Modify the last commit with the current index changes.

Local Repository

git log

$ git log 
commit e05dddd8650c0bc42b69d8db6801306baf8a009e
Author: Andrey Kucherenko <kucherenko.andrey@gmail.com>
Date:   Tue Jun 9 20:12:49 2015 +0300

    test2.txt

Show recent commits, most recent on top. Options:
‑‑stat with stats (files changed, insertions, and deletions) 
‑‑author=author only by a certain author
‑‑after="MMM DD YYYY" ex. ("Jun 20 2008") only commits after a certain date
‑‑before="MMM DD YYYY" only commits that occur before a certain date 

--merges

git diff <commit> <commit>

$ git diff 3464790baf2f1def6cb05e59789498ed887a835e e05dddd8650c0bc42b69d8db6801306baf8a009e
diff --git a/README.md b/README.md
index e51914f..19f483c 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,4 @@
 Hello!
-======
\ No newline at end of file
+======
+
+123
\ No newline at end of file
diff --git a/test.md b/test.md
new file mode 100644
index 0000000..ef64a01
--- /dev/null
+++ b/test.md
@@ -0,0 +1 @@
+test1 test1 test1
diff --git a/test2.txt b/test2.txt
new file mode 100644
index 0000000..b8e352d
--- /dev/null
+++ b/test2.txt
@@ -0,0 +1 @@
+Modify the last commit with the current index changes.

View the changes between two arbitrary commits

git branch

$ git branch -a
  hello
* master

List all existing branches. Option -r causes the remote-tracking branches to be listed, and option -a shows both.

git branch -d <branch>

$ git branch -d hello
error: The branch 'hello' is not fully merged.
If you are sure you want to delete it, run 'git branch -D hello'.
$ git branch -D hello
Deleted branch hello (was 65bc955).

Delete an specified branch. Use -D to force.

Upstream Repository

git branch --track <n> <rb>

$ git branch --track new origin/feature/myfeature 
Branch new set up to track remote branch feature/myfeature from origin.

Create a new local branch that tracks a remote branch.

<n> - new branch

<rb> - remote branch

git remote add origin <path>

git clone <repo>

$ git clone git@github.com:kucherenko/git-classes.git
Cloning into 'git-classes'...
remote: Counting objects: 7, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 7 (delta 0), reused 0 (delta 0), pack-reused 3
Receiving objects: 100% (7/7), done.
Checking connectivity... done.

Download the repository specified by repo and checkout HEAD of the master branch.

git pull <remote> <ref>

$ git pull origin master
From github.com:kucherenko/git-classes
 * branch            master     -> FETCH_HEAD
Already up-to-date.

Incorporates changes from a remote repository into the current branch. In its default mode, git pull is shorthand for git fetch followed by git merge FETCH_HEAD.

git reset  --hard <remote> <br>

$ git reset --hard origin/master 
HEAD is now at 14d9d6e Merge pull request #1 from kucherenko/develop

Reset local repo and working tree to match a remote branch. Use reset ‑‑hard origin/master to throw away all commits to the local master branch. Use this to start over on a failed merge.

git fetch <remote> <ref>

$ git fetch origin master
From github.com:kucherenko/git-classes
 * branch            master     -> FETCH_HEAD

Download objects and refs from another repository.

Stash

git stash save [<msg>]

$ git stash
Saved working directory and index state WIP on my_cool_branch: e05dddd test2.txt
HEAD is now at e05dddd test2.txt

Save your local modifications to a new stash, and run git reset ‑‑hard to revert them. The msg part is optional and gives the description along with the stashed state. For quickly making a snapshot, you can omit both "save" and msg.

git stash apply

$ git stash apply 
On branch my_cool_branch
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	new file:   test3.txt

Move changes from the specified stash into the workspace. The latest stash is the default.

git stash pop

$ git stash pop
On branch my_cool_branch
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	new file:   test3.txt

Dropped refs/stash@{0} (8126a73996b9da537a6f1dad50e46b2a8dbf1825)

Applies the changes from the last (or specified) stash and then removes the given stash.

Flow

$ apt-get install git-flow

#or

$ git clone --recursive git://github.com/nvie/gitflow.git
$ make install prefix=$HOME
$ PATH=$PATH:$HOME/bin
$  . ~/.bashrc

$ git flow init

Which branch should be used for bringing forth production releases?
   - master
   - my_cool_branch
Branch name for production releases: [master] 

Which branch should be used for integration of the "next release"?
   - my_cool_branch
Branch name for "next release" development: [develop] 

How to name your supporting branch prefixes?
Feature branches? [feature/] 
Release branches? [release/] 
Hotfix branches? [hotfix/] 
Support branches? [support/] 
Version tag prefix? [] 
Hooks and filters directory? [/home/apk/workspace/lab/git/.git/hooks]
$ git flow feature start myfeature
Switched to a new branch 'feature/myfeature'

Summary of actions:
- A new branch 'feature/myfeature' was created, 
    based on 'develop'
- You are now on branch 'feature/myfeature'

Now, start committing on your feature. When done, use:

     git flow feature finish myfeature
$ git flow feature publish myfeature
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3),
 386 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:kucherenko/git-classes.git
 * [new branch]      feature/myfeature 
                            -> feature/myfeature
Already on 'feature/myfeature'
Your branch is up-to-date with 
'origin/feature/myfeature'.

Summary of actions:
- A new remote branch 'feature/myfeature'
     was created
- The local branch 'feature/myfeature'
     was configured to track the remote branch
- You are now on branch 'feature/myfeature'
$ git flow feature pull origin myfeature

Pulled origin's changes into feature/myfeature.

$ git flow feature track MYFEATURE
$ git flow release start RELEASE
Switched to a new branch 'release/RELEASE'

Summary of actions:
- A new branch 'release/RELEASE' 
    was created, based on 'develop'
- You are now on branch 'release/RELEASE'

Follow-up actions:
- Bump the version number now!
- Start committing last-minute 
    fixes in preparing your release
- When done, run:

     git flow release finish 'RELEASE'

$ git flow release publish RELEASE
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:kucherenko/git-classes.git
 * [new branch]      release/RELEASE -> release/RELEASE
Already on 'release/RELEASE'
Your branch is up-to-date with 'origin/release/RELEASE'.

Summary of actions:
- A new remote branch 'release/RELEASE' was created
- The local branch 'release/RELEASE' 
    was configured to track the remote branch
- You are now on branch 'release/RELEASE'
$ git flow release finish RELEASE
Branches 'master' and 'origin/master' have diverged.
And local branch 'master' is ahead of 'origin/master'.
Switched to branch 'master'
To git@github.com:kucherenko/git-classes.git
 - [deleted]         release/RELEASE
Deleted branch release/RELEASE (was 211bffb).

Summary of actions:
- Release branch 'release/RELEASE' 
    has been merged into 'master'
- The release was tagged 'RELEASE'
- Release branch 'release/RELEASE' 
    has been locally deleted; it has been 
    remotely deleted from 'origin'
- You are now on branch 'master'
$ git push --tags
Counting objects: 1, done.
Writing objects: 100% (1/1), 178 bytes | 0 bytes/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To git@github.com:kucherenko/git-classes.git
 * [new tag]         RELEASE -> RELEASE
$ git flow hotfix start VERSION
Branches 'master' and 'origin/master' have diverged.
And local branch 'master' is ahead of 'origin/master'.
Switched to a new branch 'hotfix/VERSION'

Summary of actions:
- A new branch 'hotfix/VERSION' was created, 
    based on 'master'
- You are now on branch 'hotfix/VERSION'

Follow-up actions:
- Bump the version number now!
- Start committing your hot fixes
- When done, run:

     git flow hotfix finish 'VERSION'
$ git flow hotfix finish VERSION
Branches 'master' and 'origin/master' have diverged.
And local branch 'master' is ahead of 'origin/master'.
Switched to branch 'develop'
Deleted branch hotfix/VERSION (was 211bffb).

Summary of actions:
- Hotfix branch 'hotfix/VERSION' 
    has been merged into 'master'
- The hotfix was tagged 'VERSION'
- Hotfix branch 'hotfix/VERSION' 
    has been locally deleted
- You are now on branch 'develop'
http://nvie.com/posts/a-successful-git-branching-model/

Thank you

Questions?

Advanced GIT

By Andrey Kucherenko

Advanced GIT

Slides for training about git

  • 1,220
Loading comments...

More from Andrey Kucherenko