git tips

This blog post is personal reference to some git ressources I don't want to forget.

Using gitignore to ignore (but not delete) files

    #source http://stackoverflow.com/a/16442091
    #Instead of .gitignore, file track in origin repo
    #Modification in local repo will never be marked changed
    git update-index --assume-unchanged <file> 
    #revert this operation 
    git update-index --no-assume-unchanged <file> 
    git ls-files -v | grep '^h' #list such files     
    #delete cached file
    #vi .gitignore and add <file>
    git rm --cached <file>

Checkout remote git branch

    git fetch origin #fetch a branch
    git branch -v -a #show available remote branches
    #branch off remote branch(without switch)
    git branch remote_branch origin/remote_branch 
    #branch off remote(with switch)
    git checkout -b remote_branch origin/remote_branch 

    #from git:
    git checkout -b [branch] [remotename]/[branch]#remotename=origin 
    git checkout --track [remotename]/[branch] #shorthand to previous 
    git checkout [branch]#shorthand of previous shorthand
    git branch -vv #shows tracking branches

checkout and fetch came from StackOverflow answer.
More information came from Git Branching Remote Branches

Reverting a commit

How to revert a merge commit that's already pushed to remote branch?
Answers are from 1 and 2

  #-m 1 will get you the tree as it was in <commit-hash>
  #-m 2 will reinstate the tree as it was in <commit-hash-1>  
  git revert -m 1 <commit-hash> 
  git commit -m "Reverting commit that messed up."
  git push -u origin master

Reset or revert a specific file to a specific revision using Git?

    git checkout <commit-hash> file/to/restore

"Resolve Git merge conflicts in favor of their changes during a pull?". 3

#by default  -s recursive is applied
git pull -s recursive -X theirs <remoterepo or other repo>  
git pull -X theirs <remoterepo or other repo>

#or simply 
git pull -X theirs#in current repository  

Merging two branches from two separate repositories

#On Current Repository
git checkout <master|branch>  
git checkout -b <branch>

#Adding a second remote(<repository>.git)  
git remote add <remote-or-repository> ssh://git@git-server/<repository>.git  
git remote update

#Being on <branch>, merge from local repository, merge with new remote 
git merge <remote-or-repository>/<their-branch> #<~ git merge <repo-name>/<their-branch>  

Show Comments