Wednesday, August 26, 2015

git shell commands

You can install GitHub/Git Shell from http://desktop.github.com
This DOES NOT require admin privilege (will get a small install stub of 116kb
which will again download around 112 Mb for windows client

Use only PowerShell to get files affected count is command prompt. If Git Shell opens 
some other shell like windows cmd or ming, open GitGUI and change shell type in settings.


http://www-cs-students.stanford.edu/~blynn/gitmagic/ch02.html

-----------------------------------------------------------------------------
know quickly the current working [branch] 
how many new files added(+),  # files modified(~), # files deleted(-)
-----------------------------------------------------------------------------
C:\folder [master +0 ~0 -1]> 
               |  |  |  |
               |  |  |  |__ # of files deleted
               |  |  | 
               |  |  |__ # of files modified
               |  |
               |  |___ # of new files added
               |
               |___ branch
 

-----------------------------------------------------------------------------
Create new git repo and clone
-----------------------------------------------------------------------------
touch README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin http://github.com/nerunja/myproject.git
git push -u origin master
git clone -b master http://github.com/nerunja/myproject.git

-----------------------------------------------------------------------------
Ignore a file accidentally deleted in workspace from commiting
-----------------------------------------------------------------------------
Ignore a file deleted which is pending commit/push

C:\folder [master +0 ~0 -1]> git status
On branch master
Your branch is up-to-date with 'origin/master'.

Changes not staged for commit:
  (use "git add/rm ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

        deleted:    file1.txt

no changes added to commit (use "git add" and/or "git commit -a")
C:\folder [master +0 ~0 -1]> git checkout -- file1.txt
C:\folder [master +0 ~0 -1]> git status
On branch master
Your branch is up-to-date with 'origin/master'.

nothing to commit, working directory clean
C:\folder [master]>
-----------------------------------------------------------------------------
Remove/Rename a file from repo
-----------------------------------------------------------------------------
$git rm
$git mv
-----------------------------------------------------------------------------
checkout any older revision by looking at commit comments
-----------------------------------------------------------------------------
Start with:
$ git log
which shows you a list of recent commits with comments, and their SHA1 hashes.
Next, type:
$ git checkout  file
-----------------------------------------------------------------------------
Remove untracked files 
-----------------------------------------------------------------------------
$git clean --force

Remove ONLY ignored files (files specified in .gitignore)
$git clean --force -X

Remove both ignored files and untracked files
$git clean --force -x (note small x)

Remove untracked files INCLUDING the directory containing and also the gitignored files
$git clean -xdf

-----------------------------------------------------------------------------
$ git diff filename
-----------------------------------------------------------------------------
Amend message of last commit that is pending push

git commit --amend -m "New commit message"
-----------------------------------------------------------------------------
View latest n commit messages (say 5)
$ git log --oneline -n 5
-----------------------------------------------------------------------------
The ultimate log output
git log --oneline --graph --decorate [filename]

* 129cce6 (origin/master, origin/HEAD, master) Merge branch 'testing'
|\
| * a86067a (origin/testing, testing) testing branch commit
* | 1a36bd6 master branch commit
-----------------------------------------------------------------------------
show the commit, person, and date on which each line of file
git blame --date=short <filename>
-----------------------------------------------------------------------------
Review the changes made to a file
git diff <commit hash> <filename>

to see diff with previous commit use HEAD~1 instead giving the actual commit hash
$ git diff HEAD~1 <filename>

Then to revert a specific file to that commit use the reset command:
$ git reset <commit hash> <filename>

You may need to use the --hard option if you have local modifications.

-----------------------------------------------------------------------------
See all the file changes across between current and previous commit
- Just don't mention the filename in git diff 
$ git diff HEAD~1

-----------------------------------------------------------------------------
Unable to pull (after committing local changes) from repo due to local modifications in a file. 
   How to discard local changes on specific files 
  (Note: Files with modifications that need to be retained SHOULD be committed first before pull command)
$ git checkout -- <filename that has been modified in local / working directory >
$ git pull 
$ git push   (now it will successfully push committed changes)
-----------------------------------------------------------------------------
Reset git HEAD in current/any branch by moving & placing HEAD over any other commit
Resetting is a way to move the tip of a branch to a different commit. 
This can be used to remove commits from the current branch. 

$git reset --hard HEAD
$git config --global push.default current
$git pull

The following command moves the hotfix branch backwards by two commits. 

$git checkout hotfix
$git reset HEAD~2
                            hotfix/HEAD
                            |
                            |
                        |---A---B--C
                        |
------------a---b---c---d----e
                             |
                             |
                             master


The parameters passed to git reset and git checkout determine their scope. 
When you don’t include a file path as a parameter, they operate on whole commits. 
Note that git revert has no file-level counterpart.
git revert is ALWAYS at commit level.
-----------------------------------------------------------------------------
git revert
Reverting undoes a commit by creating a new commit. 
This is a safe way to undo changes, as it has no chance of re-writing the commit history. 
For example, the following command will figure out the changes 
contained in the 2nd to last commit, create a new commit undoing those changes

git revert HEAD~2

Command Scope Common use cases
git reset Commit-level Discard commits in a private branch or throw away uncommited changes
git reset File-level Unstage a file

git checkout Commit-level Switch between branches or inspect old snapshots
git checkout File-level Discard changes in the working directory

git revert Commit-level Undo commits in a public branch
git revert File-level (N/A)

-----------------------------------------------------------------------------
git log --after="2016-1-1" --before="2016-1-10" --author="Username "
-----------------------------------------------------------------------------
Git Reset to a previous commit:

Steps:
1. Clone the respective branch into a new directory
2. Git reset to previous commit by below command
   git reset --hard <CommitId to which we need to point back> [ex:ab870c42194351535d0d35d1f7200b78cf3f41ea]
3. git push -f [force push to the specified commit].
-----------------------------------------------------------------------------
Save/Stash changes in working directory temporarily and get the committed version before this local changes
NOTE: every git stash pushes the previous stashed ref to next number and makes the current stash available at ref stash@{0}. It works like a STACK. LIFO (last-in-first-out)

$git stash [save] (save param is optional)
$git stash list
$git diff stash@{0}
$git stash pop (applied changes saved in stash stack on working directory)
$git clear clear (remove everything from stash)

-----------------------------------------------------------------------------

Typical usage of STASH ( temporarily move the local changes to avoid conflicts)

$git stash
$git reset --hard
$git pull origin dev
$git stash apply

-----------------------------------------------------------------------------



No comments:

Post a Comment