Friday, October 16, 2015

Git commands


Basics:
========
To add files to be committed      
  git add 
To commit a change in the local tree. To be done after the git add.
  git commit -m "comment about the changes"
To push the changes or checkin the changes to main branch    
  git push
To discard all changes done in a branch locally
  git stash save --keep-index
  git stash drop
    
To create a new branch     
  git checkout -b myBranch
 
To create a patch from the local branch
 Before this check out a new branch, do all changes in the branch, verify it. And then commit those changes. Do not push the changes to master.
 git format-patch master --stdout > myPatch.patch

git reset  updates the index for that path so that it matches HEAD (the current commit). 
It doesn't touch the working tree

git rm --cached -r  to remove a directory which was added.
 
GT Diff:
==========
git diff HEAD@{1}
The @{1} means "the previous position of the ref I've specified", so that
evaluates to what you had checked out previously - just before the pull.

git diff HEAD@{1} filename
This is a general thing - if you want to know about the state of a file
in a given commit, you specify the commit and the file, not an ID/hash
specific to the file.

git diff --cached --name-only   
It lists the filenames in the staging area that will be committed to git. Can be used after all the files to be committed are added thru 'git add'  to verify only intended files are being commited

git difftool --cached
the diff of files that are about to be committed against the head will be showed in vimdiff one by one.

git difftool  
the diff of files in the two commits will be shown in vimdiff one by one.

git log --pretty=oneline
compact display of git log

how about 
  • git diff --name-only for changes relative to index
  • git diff --name-only --staged for ... well staged chages :)
  • git diff --name-only HEAD got both
To get the modified files alone:
git status --porcelain|awk '{if($1=="M") {print "basename " $2}}'|sh

To copy the modified files alone to a remote server, as below:
git status --porcelain|awk '{if($1=="M") {print "scp " $2 " account_name@server_ip:~/my_codebase/$(dirname " $2 ")/;"} }'|sh


No comments: