Wednesday, August 22, 2012

Tips on GIT


GIT

USEFUL GIT COMMANDS
-------------------------------

Clone a repo:: git clone repository_url

To display whitespace errors # git diff --check
To revert a modified file to original state(unmodified) # git checkout     --    file_name  
To see list of modified and untracked files # git status
To stage a file/to mark a file to be committed in the next checkin command # git add file_name
To display diff of all modified files # git diff
To display last n commits # git log –n
To list only the direct commits to a branch, switch to that branch and do # git rev-list origin..HEAD --not $(git merge-base --all origin HEAD) --first-parent --pretty --no-merges

BRANCH
------
To see the branches you have # git branch
TO list all branches in the origin server #git branch --remotes
To switch to another branch # git checkout
To force branch switching even when it refuses #git checkout branch_name --force
To track a remote branch: #git checkout --track origin/branch_name
To delete a local branch #git branch -d branch_name
To force delete a local branch #git branch -D branch_name
To delete a remote branch from the remote server #git push origin :the_remote_branch

To list all files committed in the last commit # git show --pretty="format:" --name-only
To list all files committed in a particular commit object # git show --pretty="format:" --name-only
TO rename a branch #git branch (-m | -M) old_branch new_branch (old_branch optional)
To send a new local branch to remote server # git push -u origin new_branch_name [this creates a branch in the server with the same name]

RESET A BRANCH TO UPSTREAM STATE # git reset --hard origin/master (resets the master to upstream state; run this command repeatedly if it is not working once)

HISTORY/REVISION
-----------------
To list changes for a file #git log -- file_name
To list changes for a file graphically # gitk file_name

HOW TO RESOLVE CONFLICTS / MERGING
---------------------------------
After seeing a conflict, you can do two things:

Decide not to merge. The only clean-ups you need are to reset the index file to the HEAD commit to reverse 2. and to clean up working tree changes made by 2. and 3.; git merge --abort can be used for this.

Resolve the conflicts. Git will mark the conflicts in the working tree. Edit the files into shape and git add them to the index. Use git commit to seal the deal.

You can work through the conflict with a number of tools:

Use a mergetool. git mergetool to launch a graphical mergetool which will work you through the merge.

Look at the diffs. git diff will show a three-way diff, highlighting changes from both the HEAD and MERGE_HEAD versions.

Look at the diffs from each branch. git log --merge -p will show diffs first for the HEAD version and then the MERGE_HEAD version.

Look at the originals. git show :1:filename shows the common ancestor, git show :2:filename shows the HEAD version, and git show :3:filename shows the MERGE_HEAD version.

During merge conflict do the following:

#git mergetool --tool=tortoisemerge {make sure that you have tortoise git installed to use this merge tool}
#git commit -m "Manual merging master into this branch"
#git push {send to remote server}

STASH
-----
To stash the current working directory #git stash
To list stashes available #git stash [show|list]
To discard a stash #git stash drop

#no complex server setup is required to use GIT
$git init myproject
Creates directory called myproject

$git add . (notices files and readies them for recording)

$git commit -m "" (commits the final version)

#the final push downloads the central copy and merges with the local commits and offers help on any conflict during auto merge. This process happens to everybody who works parallelly. So, conflict resolution is localized.

Git Config
----------

$ git config --list
core.symlinks=false
core.autocrlf=true
color.diff=auto
color.status=auto
color.branch=auto
color.interactive=true
pack.packsizelimit=2g
help.format=html
http.sslcainfo=/bin/curl-ca-bundle.crt
sendemail.smtpserver=/bin/msmtp.exe
diff.astextplain.textconv=astextplain
rebase.autosquash=true
user.name=yourname
user.email=youremail
push.default=upstream

Git Line Endings
----------------
To list a property #git config --global proptery_name

core.eol = native – The default.
core.autocrlf = true [back and forth conversion of line endings in commit and checkouts]
core.safecrlf = true [backout of autoconversion if unsafe file is detected]

The following are added in .gitattributes files:::-

*.txt crlf in .gitattributes file enables auto crlf conversions for text files
*.txt -crlf in .gitattributes file disables auto crlf conversions for text files
*.txt crlf=input in .gitattributes file enables auto conversion only in commits but not in checkout on working directory
*.txt text [marks all .txt files as text files]
*.txt -text [.txt files will not go through auto crlf conversion]
*.txt text=auto [git auto detects .txt files as text files or not for crlf conversion; relies on git's internal binary detection heuristics]
*.png binary [marks .png files as binary files]
* text=auto [tell git to detect all text files and automatically normalize them (convert CRLF to LF)]

EGit(For Eclipse)
------------------
#Share the project to git first (right click, open Team menu and do Share)
#Try to pull the project and if you get proxy error, do the following:-
 1.Set HOME env variable pointing to your user home(C:\Users\user on Win7)
 2.Set GIT_SSH=C:\Program Files (x86)\Git\bin\ssh.exe as env variable
 3.Create .bash_rc file in your user home with 'GIT_SSH="C:\Program Files (x86)\Git\bin\ssh.exe"' line
 4.Relogin to your machine

 [THIS DIDN'T WORK. OF COURSE PROXY ERROR WENT AWAY, BUT IT WOULDN'T PULL AND WAITS FOREVER]

 This is due to signed certificate used for ssh in git. If you can reconfig the certificate without password, it would work.

No comments:

Post a Comment