Git Tricks
Mort Yao1 Working with branches
1.1 Clone a single branch from a remote repository
$ git clone -b develop --single-branch [email protected]:soimort/example.git
Or: (if there’s already an existing remote, just checkout
the branch)
$ git checkout -b develop origin/develop
1.2 Force update of a repository brutally (and throw away any uncommitted local changes / commits)
This operation does not require configuration of git user name / email. (Thus could be useful on an app server.)
$ git fetch
$ git reset --hard origin/master
1.4 Delete a remote branch
$ git push origin :branch-to-be-deleted
1.5 Clean obsolete references
Show all stale branches:
$ git branch -r
Update local references to the remote repository: (and delete obsolete references that are removed in the remote repository)
$ git fetch --prune
2 Working with submodules
2.1 Update all submodules
$ git submodule update --recursive --remote
3 Staging
3.1 Revert (uncommitted change of) a single file
$ git checkout file
To avoid confusion with a branch name, use:
$ git checkout -- file
3.2 Unstage any staged changes for a file
$ git reset file
3.3 Stage the removal of a file (but leave it untracked in the working tree)
$ git rm --cached file
4 GitHub tricks
4.1 Get a Pull Request (and create a feature branch for the PR)
$ git fetch origin pull/123/head
$ git checkout -b feature-branch FETCH_HEAD
Or:
$ git fetch origin pull/123/head:feature-branch
$ git checkout feature-branch
4.2 Push an empty commit to trigger a GitHub Pages rebuild
$ git commit -m 'rebuild pages' --allow-empty
4.3 Download a specific folder in a GitHub repo
$ svn export https://github.com/user/repo.git/trunk/path/to/something
5 Miscellaneous
5.1 Compare two arbitrary files
$ git diff --no-index file_a file_b
5.2 Create the archive of a tag
$ git archive --format=tar.gz --prefix=foobar-1.2.3/ v1.2.3 > v1.2.3.tar.gz
5.3 Remove untracked files and directories from the working tree
$ git clean -fd
To remove ignored files as well:
$ git clean -fdx