Git Tips Tricks for Developers
Install Bash git completion
brew install git bash-completion
Caching GitHub password in Git
git config --global credential.helper 'cache --timeout 72000'
- Remove cached password (to reenter new password when it’s changed)
git credential-osxkeychain erase
Temporarily ignoring files
git update-index --assume-unchanged file-path
git update-index --no-assume-unchanged file-path
Remove file from the repository but keep it locally
git rm --cached -r somedir/somefile
then add them to .gitignore
Reset current HEAD to the specified state
undo last commit
git reset --soft HEAD~1
- Undo commits permanently:
git reset --hard HEAD~3
- The last three commits (HEAD, HEAD^, and HEAD~2) were bad and you do not want to ever see them again.
- Undo a merge or pull inside a dirty working tree: git reset –merge ORIG_HEAD
- return to any previous revision
git reset --soft commit_hash
- Reset committed changes to local changes
git reset HEAD~
undo git pull
git reset --hard
Reset local branch to be same as remote branch
git fetch --all && git reset --hard upstream/master
- If need delete everything(just like re-clone): git clean -x -d -f
~ vs ^
- Both ~ and ^ on their own refer to the parent of the commit.
- ~2 means up two levels in the hierarchy, via the first parent if a commit has more than one parent
- ^2 means the second parent where a commit has more than one parent (i.e. because it’s a merge)
- HEAD~2^3 means HEAD’s grandparent commit’s third parent commit.
rebase
Edit your X most recent commits interactively (squash, fixup, reword, drop)
- git rebase -i HEAD~X
Modify a specific commit
git rebase --interactive 'bbc643cd^'
# change pick to edit, Make your changes and then commit them with the same message
git commit --all --amend --no-edit
git rebase --continue
Split a commit in two with Git
git rebase -i <oldsha1>
# mark the expected commit as `edit` (replace pick in front of the line), save and close
git reset HEAD^
git add ...
git commit -m "First part"
git add ...
git commit -m "Second part"
git rebase --continue
Delete a commit
- If it is the last commit this is very straight forward. git reset HEAD
git rebase --onto <branch name>~<first commit number to remove> <branch name>~<first commit to be kept> <branch name>
# e.g to remove commits 2 & 3
git rebase --onto repair~3 repair~1 repair
Branch
- Delete local branch:
git branch -d <local_branchname>
- Delete remote branch:
git push origin --delete <remote_branchname>
orgit push origin :<remote_branchname>
- Rename local branch name:
git branch -m <new-branch-name>
Checkout remote brach
- git branch shows local branch, sometimes some remote branches are not in local even after git fetch -all (when have multiple remotes), in this case:
- which git branches are tracking which remote / upstream branch?
- git branch -vv # doubly verbose!
- How to change the remote a branch is tracking?
- git branch branch_name –set-upstream-to your_new_remote/branch_name
git log
- git log –oneline [–pretty=oneline]
Show commit id only
- git log -1 –pretty=format:“%H”
View change in a specific commit
- git show
Undo anything
Wipes your unstaged changes
- git checkout
undo rm -rf * - git stash - if no local update
undo git commit –amend
- git reset –soft HEAD@{1}
Checkout remote file after merge conflict
- git checkout HEAD the_file
shallow clone
git clone --depth 1 https://path/to/repo/foo.git -b bar
- –no–single–branch instead -b bar, to download first commit form ALL branches.
- –no-checkout(-n): No checkout of HEAD is performed after the clone is complete.
ls-remote
git ls-remote --tags remote_git_url
Tags
- git tag
git clean
- -n: –dry-run
git clean -x -d -f
git commit
- Add change to your last commit, instead of making a new commit
git commit --amend
Commit messages
<type>(<scope>): <subject>
- type: feat(new feature), fix, docs, style(format code, no code change), refactor, test(add test cases), chore
git remote
- List all remotes:
git remote
- Add another remote:
git remote add upstream the_url
git remote show
git remote show upstream
git stash
- -u|–include-untracked
- List all stashes:
git stash list
git stash
- Also stack untracked files:
git stash -u
- Apply the stash:
git stash apply <stash@{n}>
- Apply last stash and remove the stash:
git stash pop
git stash clear
git checkout <stash@{n}> -- <filePath>
git diff
- Ignore the white space:
git diff -w
- Show local staged change(added but not committed):
git diff --cached
- Show committed but not pushed change:
git diff origin/master..master
emoji
Misc
- Show recent changes:
git whatchanged --since='1 weeks ago'
- git status - show tracked files only
git status -uno(--untracked-files=no)
- Delete all stashes:
git stash clear
- git add –interactiveSymbolic links in Git
- The following untracked working tree files would be overwritten by merge
- The problem is that you are not tracking the files locally but identical files are tracked remotely so in order to “pull” your system would be forced to overwrite the local files which are not version controlled.
How to Copy Text From Copy-Protected Web Pages
Awesome Tips about Chrome Series
The Problem
Sometimes we want to copy some text/code from a website, but find out that it disables copy text or even completely disables right click, right context menu. It’s annoying, but we can fix it easily.
Using Proxy Sites
Specific Chrome Extensions
We can use the following Specific Chrome extensions to make us able to copy text.
Reader View Chrome Extensions
If we don’t want to install the above specific Chrome extensions, then we can use the more general reader view chrome extension which can also make us able to copy text.
Read here for more must-have google chrome extensions
Reader View
- This opens the page in chrome-extension:// and we have to wait until (at least) the main content of the page is loaded.
- The greatest thing is it reduces the memory usage a LOT for pages that load a lot of flashes, ads.
- e.g. reduces this page from around 450+ mb to 87mb.
- This also works well together with
The Great Suspender
: the page in reader view will not be suspended as it runs in extension mode. - Assign shortcut
Ctrl+Shift+R
toToggle the Reader View
- Print and save the reader mode version, show or hide image.
- Able to change the speed of
text-to-speech
- The Cron is it doesn’t work with other extensions, e.g., Chrome Vimium
- as It opens the page in chrome-extension.
Tips
- Before
Ctrl+Shift+R
to enter reader view:- Copy the url
Ctrl+l then Ctrl+c
oryy
from Chrome Vimium Cmd+down
to scroll to the end of page first to make sure the page load completes.
- Copy the url
- It adds the page url to the first line from there we can copy the page url
Just Read
- Able to change theme(dark or light), width, font size and save the config.
- Assign
Ctrl+Shift+L
toOpen the Just Read format for the current page
- Assign
Ctrl+Shift+K
toEnable user text selection mode using Just Read
, then choose which part you want to select then enter (it will automatically try best to choose), it will show the section in read mode. - When it doesn’t work(e.g. in quip), use
View this selection in Just Read
. - Works with Chrome Vimium
Clearly
- Assign shortcut
Ctrl+Shift+C
toToggle Clearly
- Make
not easy to copy text
easier to copy - Support outline, different themes, fullscreen, text-to-voice
- Cron: Can’t change the width
- Distill Page from Chrome
- the result is not that good, and hard to run the command.
Mercury Reader
- can’t change width
Summary
- Use Reader View when the page(website) is slow: load lots of ads, flash, uses a lot of memory and you will take time(2+ mins) to read it.
- Use Just Read in other case.
- Use Clearly to check the outline.
Subscribe to:
Posts (Atom)
Labels
adsense
(5)
Algorithm
(69)
Algorithm Series
(35)
Android
(7)
ANT
(6)
bat
(8)
Big Data
(7)
Blogger
(14)
Bugs
(6)
Cache
(5)
Chrome
(19)
Code Example
(29)
Code Quality
(7)
Coding Skills
(5)
Database
(7)
Debug
(16)
Design
(5)
Dev Tips
(63)
Eclipse
(32)
Git
(5)
Google
(33)
Guava
(7)
How to
(9)
Http Client
(8)
IDE
(7)
Interview
(88)
J2EE
(13)
J2SE
(49)
Java
(186)
JavaScript
(27)
JSON
(7)
Learning code
(9)
Lesson Learned
(6)
Linux
(26)
Lucene-Solr
(112)
Mac
(10)
Maven
(8)
Network
(9)
Nutch2
(18)
Performance
(9)
PowerShell
(11)
Problem Solving
(11)
Programmer Skills
(6)
regex
(5)
Scala
(6)
Security
(9)
Soft Skills
(38)
Spring
(22)
System Design
(11)
Testing
(7)
Text Mining
(14)
Tips
(17)
Tools
(24)
Troubleshooting
(29)
UIMA
(9)
Web Development
(19)
Windows
(21)
xml
(5)