Monday, 10 March 2014

Very Useful git commands - devised over a period of time

If one wants to see the commits that are in the 'dev' branch that are not in the 'master' branch, we can do dev ^master, or vice versa. Note that the Windows command-line treats ^ as a special character, in which case you'll need to surround ^master in quotes.
$ git log --oneline dev ^master
fg23456 debug flag added
5890bha memory corruption bug fixed
$ git log --oneline master ^dev
945f70c reusing variable
 
List all commits for a specific file
git log —  <path name/file name>
git log –stat <path> so that we can see the impact of each commit on the file.
git log –pretty=oneline HEAD~3…HEAD~7 | sed -s ‘s/^[a-z0-9 ]\{41\}//g’
Undo a commit and redo
$ git commit …
$ git reset –soft HEAD^      (1)
$ edit                        (2)
$ git add ….                (3)
$ git commit -c ORIG_HEAD     (4)
This is most often done when you remembered what you just committed is incomplete, or you misspelled your commit message, or both. Leaves working tree as it was before “reset”.
Make corrections to working tree files.
Stage changes for commit.
“reset” copies the old head to .git/ORIG_HEAD; redo the commit by starting with its log message. If you do not need to edit the message further, you can give -C option instead
Untrack a directory/file w/o actually deleting it
Th e following will cause git to untrack your directory and all files under it without actually deleting them:
git rm -r –cached <your directory>
List all commits for a specific file
git log —  <path name/file name>
git log –stat <path> so that we can see the impact of each commit on the file.
Git tagging and using commit numbers as build #s
Use tags to mark commits with version numbers:
git tag -a v2.5 -m ‘Version 2.5′
Then use the describe command:
git describe –tags –long
This gives you a string of the format:
v2.5-0-deadbeef
^    ^ ^
|    | |
|    | SHA of HEAD
|    |
|    number of commits since last tag
|
last tag
Remove empty directories in git
Suppose you want to remove empty directories in git but wanna make sure what git will delete run
“git clean -fdn ” to confirm then run git clean -fd to delete the untracked directories.
Create dummy commits in git
Sometimes you may just want to create a dummy commit in the git without actually adding or modifying files.
git commit –allow-empty -m ‘Dummy commit to create the first tag in UI code ‘

Rename a tag in git
I wanted to apply the same tag name to two diffrent branches in the same repository  but git doesn’t allow that.
so I prepended the tag names with a string like MYSQL,MSSQL,ORACLE and it worked
git tag “MSSQL_2.30.0″ “2.30.0″ -f -m “Prepending DB type to the existing tag name”
usage :
git tag <new_tag> <old_tag> -f -m <message>
-f is for forceful renaming
svn -v equivalent for git
SVN’s log has a “-v” mode that outputs filenames of files changed in
each commit, like so:Try one of the following, as git equivalent:git log –name-status
or
git log –name-only
or
git log –stat
To check git messages between two commits/sha1s
git log –pretty=oneline HEAD~3…HEAD~7 | sed -s ‘s/^[a-z0-9 ]\{41\}//g’
we are stripping the sha1 with sed here.
Switch to a new branch
How to switch to a different branch in your repository when you actually cloned a branch (with -b option)
git checkout -b <new_branch_name> remotes/origin/<remote_branch>
eg:
git checkout -b graham_2.30_STABLE remotes/origin/graham_2.30_STABLE
How to check list of files with conflict in git. This is needed while merging.1) git diff –name-only –diff-filter=M
2) grep -rH ‘<<<<<<< HEAD’ * | grep -v .git | cut -f1 -d : | sort | uniq
where
  M: modification of the contents or mode of a file
Other possible values are :
A: addition of a file
C: copy of a file into a new one
D: deletion of a file
R: renaming of a file
T: change in the type of the file
U: file is unmerged (you must complete the merge before it can be committed)
X: “unknown” change type (most probably a bug, please report it)
Omit those commits from  master which are in staging(even the ones which have been cherry-picked) or are patch-equivalent to a commit in staging
git log staging…master –cherry-pick –right-only –no-merges
 

Monday, 3 March 2014

Install Git's manual pages

This is for you if you 

- Have downloaded the latest tarball of git manual pages and wish to install it to learn more about the latest exciting stuff in the released git version.

- Have compiled git's source code from scratch, installed it and now wish to install the relevant manual pages that have come with the source so that you know what you have in your git armoury.


Assuming you have source, how to generate the git manual pages and install them on your linux box.
Run:

Run the target "dist-doc" to generate the manual page in the following way:
# make dist-doc

Above will create a tarball of manual pages that will contain the following directories :
man1,man5 and man7

You can obtain this information if you hack the Makefile. Copying an excerpt of the target code below:

manpages = git-manpages-$(GIT_VERSION)  # This is what your git manual pages tarball will be named as
dist-doc:
        $(RM) -r .doc-tmp-dir
        mkdir .doc-tmp-dir
        $(MAKE) -C Documentation WEBDOC_DEST=../.doc-tmp-dir install-webdoc
        cd .doc-tmp-dir && $(TAR) cf ../$(htmldocs).tar .
        gzip -n -9 -f $(htmldocs).tar
        :
        $(RM) -r .doc-tmp-dir
        mkdir -p .doc-tmp-dir/man1 .doc-tmp-dir/man5 .doc-tmp-dir/man7
        $(MAKE) -C Documentation DESTDIR=./ \
                man1dir=../.doc-tmp-dir/man1 \
                man5dir=../.doc-tmp-dir/man5 \
                man7dir=../.doc-tmp-dir/man7 \
                install
        cd .doc-tmp-dir && $(TAR) cf ../$(manpages).tar .
        gzip -n -9 -f $(manpages).tar
        $(RM) -r .doc-tmp-dir


Now to install it,
# sudo tar -xzvf git-manpages-1.9.0.tar.gz -C /usr/local/share/man

With above, I have installed teh manual pages in  /usr/local/share/man. git-manpages-1.9.0.tar.gz was the tarball in my case and may differ depending upon the version you compiled and want to install.

Verification :

Now, how do you verify if the latest manual pages are installed or not:
Hmm, Run this:
# git --version
Check the version that shows up

Now, open a manual page for any of the git commands. Example:
# git checkout --help
Scroll down this manual page and look on the bottom left . Git's version is displayed there.
It should match what you obtained earlier with "git --version".