Sunday, April 19, 2015

Useful Git Commands

git diff and git diff --staged(--cached)

Git diff will shows the diff of the unstaged files while with --staged flag it will shows the diff of which changes are there to be committed.

 git commit -a -m 'added new benchmarks'

Add all untrack files and commit(Staging will not happen during this commit)

 git rm --cached FILENAME

This command will remove the given file from the repository but will keep the file in local repository

 git mv file_from file_to

Renaming a file.

 git log -p -2

Difference introduce by last two commits

git log --stat

Overall changes happen in each commit.

git log --pretty=oneline

Show git log with each commit in single line without deeper details.

git log --pretty=format:"%h - %an, %ar : %s"
Format log as follow

ca82a6d - Scott Chacon, 6 years ago : changed the version number

 git log --pretty=format:"%h %s" --graph

Gives graphs like view for log

 git log -Sfunction_name

Git search for the commits which introduce changes in given string

 git log --pretty="%h - %s" --author=gitster --since="2008-10-01" \

Search for commits which made by author gister since the given date.

 git commit --amend

Sometime we forget to add files and commit. At that case we can add those files and using this command it will append those changes to the previous commit.

 git reset HEAD CONTRIBUTING.md

Undo changes in staging area

 git fetch [remote-name]

Update the remote branches with local. After that you can rebase or checkout them.

 git remote show origin

More detailed output of origin witth tracked branches and deleted ones.

 git remote rename pb paul

Rename remote server name

git remote rm paul

Remove git remote

 git tag

List available tags

 git tag -a v1.4 -m 'my version 1.4'

Create a annotated tag

 git show v1.4

Git show tag info

 git tag -a v1.2 9fceb02

Tag specific commit.

 git push origin v1.5 or  git push origin --tags

Push single tag or all tags

 git log --oneline --decorate

Detailed commit log view with which commit that each branch currently in.

 git branch -v

Branches with commits

 git branch --merged ( --no-merged )

Branches that are merged, branches without * is good to delete.

Compare which comments are on a branch that doesn't contain in other branch

git add -i

Very useful command to interactive staging and untaging and etc. Staging part of lines also possible

git stash

If you don't wanna commit changes and need to switch branches

git stash list

List number of stashes and descriptions

git stash apply | git stash apply stash@{2}

Re apply the stashed changes , but this will not staged newly created files. You need to execute it with git stash apply --index

git stash drop 

Drop the last stash