Friday, 23 November 2012

git cheat sheet I

[1] git color configuration
git config --global color.ui auto


OR add the following [color] content to ~/.gitconfig
vi ~/.gitconfig  


[color]
  diff = auto
  status = auto
  branch = auto
  interactive = auto
  ui = true
  pager = true
[diff]
     tool = vimdiff


[2] git incoming
#using git fetch
$ git fetch && git log ..origin/master --stat

##using git diff
$ git diff master origin/master

[3] display paths that have diffs between the index file and the current HEAD commit

git status

In emacs, same thing is achieved with M-! git status. The following screenshot is for emacs.




[4] git diff (with vimdiff)
create a file named git_diff_wrapper, put it somewhere in your $PATH
$ vi /usr/local/bin/git_diff_wrapper
#!/bin/sh
vimdiff "$2" "$5"

add following code to ~/.gitconfig

[diff]
  external = git_diff_wrapper
[pager]
  diff =

add following code to bash conf file
$ vi ~/.bashrc
function git_diff() {
  git diff --no-ext-diff -w "$@" | vim -R -
}

Now, its baked, fire following command
$ git diff 
git diff HEAD^ HEAD src/js  (with previous commit)
git diff HEAD~2 HEAD src/js (with 2 previous commits from HEAD)
$ git diff c3b8159 HEAD src/js (with sha-id)

If vim-fugitive is used, fire following command in opened file
:Gdiff -- %  # shows commits
:copen

To exit :qa

[5] git outgoing

git log origin/master.. --stat

OR

$ git fetch && git log origin/master.. --stat

OR (git whatchanged)


$ git whatchanged origin/master.. --stat


[6] git reset/rollback a commit

$ git reset --soft 'HEAD^' 
How to undo the last Git commit?
# --soft would keep the changed files and keep them in the index as they were just before the changed commit

$ git rebase -i            
#git equivalent for hg rollback



[7] git merge from remote repo to local HEAD
Feature-NepalTweets is the branch in the following example; 
$ git stash 
#changes stashed can later be viewed with git stash show and recovered via git stash pop.
$ git pull origin Feature-NepalTweets
git stash pop
or 
git stash apply && git stash drop
$ git diff --name-only --diff-filter=U 
OR
$ git status --short | grep "^UU "
#Fix conflicts
$ git status --short | grep "^UU "
#add conflicted files using git add
#comit files
git commit -i grails-app/controllers/whotweetsnepal/NepTweetController.groovy -m "FIX:json" --author prayag.upd@gmail.com;
[NepalTweets 0d5bf9d] FIX:json

#Commit only modified files
git commit -a -m "FIX : ADD Staff on bootstrap assigning it a User, MAKE createdBy of Person nullable, ADD school to a user in controller, ADD getting schoolAccount via  logged in user in makeRegistration";

git push origin Feature-NepalTweets 
To https://github.com/iPrayag/WhoTweetsNepal.git
   ebe387f..0d5bf9d  NepalTweets -> NepalTweets


NOTE : The other method could be removing the locally changed files, if the changes are not important, and then firing git pull.

undo a git merge
Say you are in branch analytics/tracking-requests, merged changes from master.
But for some reasons you want to undo a merge before commit and push,
git reset --hard origin/tracking-requests
or 
git reset --merge ORIG_HEAD

[8] add git submodule
I am pointing repo facebook-android-sdk-3.0.1 at https://prayagupd@bitbucket.org/prayagupd/facebook-android-sdk-3.0.1.git to my app BirthdayForestGap.

prayag@prayag: BirthdayForestGap$ git submodule add https://prayagupd@bitbucket.org/prayagupd/facebook-android-sdk-3.0.1.git facebook-android-sdk-3.0.1.git

Cloning into 'facebook-android-sdk-3.0.1.git'...

Password for 'https://prayagupd@bitbucket.org':
remote: Counting objects: 165, done.
remote: Compressing objects: 100% (116/116), done.
remote: Total 165 (delta 44), reused 162 (delta 44)
Receiving objects: 100% (165/165), 565.66 KiB | 8 KiB/s, done.
Resolving deltas: 100% (44/44), done.



prayag@prayag: BirthdayForestGap$ ls -la
total 68
drwxrwxr-x 10 prayag prayag 4096 Mar 29 00:40 .
drwxrwxr-x  7 prayag prayag 4096 Mar 28 01:10 ..
-rw-rw-r--  1 prayag prayag 2273 Mar 28 01:00 AndroidManifest.xml
drwxrwxr-x  3 prayag prayag 4096 Jul 17  2012 assets
drwxrwxr-x  4 prayag prayag 4096 Mar 28 19:05 bin
-rw-rw-r--  1 prayag prayag  416 Jul 17  2012 .classpath
drwxr-xr-x  5 prayag prayag 4096 Mar 29 00:40 facebook-android-sdk-3.0.1.git
drwxrwxr-x  3 prayag prayag 4096 Mar 28 19:03 gen
drwxrwxr-x  9 prayag prayag 4096 Mar 29 00:40 .git
-rw-r--r--  1 root   root    226 Mar 27 13:53 .gitignore
-rw-rw-r--  1 prayag prayag  164 Mar 29 00:40 .gitmodules
drwxrwxr-x  2 prayag prayag 4096 Mar 28 00:56 libs
-rw-rw-r--  1 prayag prayag  781 Jul 17  2012 proguard-project.txt
-rw-rw-r--  1 prayag prayag  820 Mar 26 15:08 .project
-rw-rw-r--  1 prayag prayag  629 Mar 28 00:54 project.properties
drwxrwxr-x 11 prayag prayag 4096 Mar 24 22:26 res
drwxrwxr-x  4 prayag prayag 4096 Mar 28 01:02 src


[9] Recover file permissions

$ git diff -p -R \
    | grep -E "^(diff|(old|new) mode)"  \
    | git apply
OR 

add as an git alias (let's call it permission-reset)
$ git config --global --add alias.permission-reset '!git diff -p -R | grep -E "^(diff|(old|new) mode)" | git apply'


If you don't see any changes when modifying permission, you probably have a configuration in git which ignore file mode.


[core]
    filemode = false

If the above doesn't work, fire the following command manually
prayag@prayag:~/hacker_/doctorhere-servlet$ git config core.filemode false

Also check git-preserve-permissions.


[10] git log --graph
9.1 show file history with patch
$ git log --follow --all -p grails-app/controllers/eccount/StudentController.groovy


OR git short log with --patch
$ git log -p grails-app/controllers/eccount/StudentController.groovy


[11] git difftool
add [diff] section to ~/.gitconfig.

[color]
  diff = auto
  status = auto
  branch = auto
  interactive = auto
  ui = true
  pager = true

[diff]
     tool = vimdiff

OR 

$ git config --global diff.tool vimdiff

Diff a single file with following command
$ git difftool grails-app/conf/Config.groovy

For shortcuts, follow vimdiff commands.



References
Git 1 2 3 by Jordan Schatz,

My git gist, https://gist.github.com/iPrayag/4730500

Try git, http://try.github.io/levels/1/challenges/1

http://git-scm.com/documentation

Git asks me to commit or stash changes on checkout master, even though all changes were commited?,  http://stackoverflow.com/a/13204493/432903

Change commit author at one specific commit, http://stackoverflow.com/a/3042512/432903

25 Tips for Intermediate Git Users, http://andyjeffries.co.uk/articles/25-tips-for-intermediate-git-users

https://github.com/jerith666/git-graph

Git error on commit after merge - fatal: cannot do a partial commit during a merge, http://stackoverflow.com/a/8062976/432903

is it possible to `git status` only modified files?, http://stackoverflow.com/a/13479518/432903

Git: how to diff the same file between two different commits on the same branch?

http://stackoverflow.com/a/5741436/432903

No comments:

Post a Comment