Nov 23, 2012

Git HowTos

Apply diff
git diff > some.diff
git apply some.diff
Retaine all the untracked junk in your working directory
git stash
# Code is now as it was at last checkin.
git checkout fe9c4
# Look around here.
git checkout master # Or whatever commit you had started with
# Code is now as it was at last checkin, so replay stashed diffs with:
git stash pop
Take the working directory back to the state it was in when you last checked out
git reset --hard
Create branch
git branch new_leaf # Create a new branch...
git checkout new_leaf # then check out the branch you just created.
#or execute both steps at once with the equivalent:
git checkout -b new_leaf
git push -u origin new_leaf
Create tag:
git tag v1.2 # Create a new tag... 
git push origin --tags
Merge
  • Run git merge other_branch.
  • In all likelihood, get told that there are conflicts you have to resolve.
  • Check the list of unmerged files using git status.
  • Pick a file to manually check on. Open it in a text editor and find the merge-me marks if it is a content conflict. If it’s a filename or file position conflict, move the file into place.
  • Run git add your_now_fixed_file.
  • Repeat steps 3−5 until all unmerged files are checked in.
  • Run git commit to finalize the merge.
Resolve conflict
git mergetool
Replay on the testing branch all of the changes made on the main branch since the common ancestor
git branch testing # get on the testing branch
git rebase abcd123
# or equivalently: git rebase main
How to retrieve the hash for the current commit in Git?
git rev-parse HEAD
# 86816eb7515578112ee9460da8a4040200f478d7
# or short SHA-1 version:
git rev-parse --short HEAD
# 86816eb
Reset vs Revert
Forget all local changes but you cant push to remote branch
git reset --hard HEAD 
# or 
git reset COMMIT_NO
git revert COMMIT_NO
# or 
git revert -m 2 COMMIT_NO # for merge

git push
How to count total lines changed by a specific author in a Git repository?
git log --author="_Your_Name_Here_" --pretty=tformat: --numstat \
| gawk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s removed lines: %s total lines: %s\n", add, subs, loc }' -
Git – how to determine the committers or authors
git shortlog -sn # or if you want to include the email
git shortlog -sne

No comments: