Git Usage

A random collection of git commands. See GitTutorial for a start with the basics.


Git tips
Git reference Git book

Using the stash

git stash save "work in progress"

Do other work.

git stash apply

Continue where you left off.

Apply pull request as patch

daijo@dhmbp:facebook-ios-sdk [master]$ curl | git am
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                             Dload  Upload   Total   Spent    Left  Speed
100  1039  100  1039    0     0    496      0  0:00:02  0:00:02 --:--:--   760
Applying: Fixed feed dialog disappearing immediately after returning from FB app SSO
daijo@dhmbp:facebook-ios-sdk [master]$ git push origin master

Add git submodule

cd project
git submodule add git:// vendor\plugins\rspec

Then on older git versions:

git submodule init
git submodule update


Remove git submodule

Delete the relevant line from the .gitmodules file.
Delete the relevant section from .git/config.
git rm --cached path_to_submodule (no trailing slash).
Commit and delete the now untracked submodule files.

From Stackoverflow.

Git Email etc.

$ git config --global "Firstname Lastname" Sets the name of the user for all git instances on the system
$ git config --global ""

Clone a repo

git clone

Create branch

git branch [branch_name]

Switch branch

git checkout [branch_name]

Merge branch

git checkout master
git merge [branch_name]

Delete branch

With check if changes are merged to otehr branch first:

git branch -d [branch_name]

Without check, force delete:

git branch -D [branch_name]

Delete from remote:

git push origin :[branch_name]

Track branch in remote

$ git fetch origin
$ git checkout --track origin/develop
Branch develop set up to track remote branch develop from origin.
Switched to a new branch 'develop'

Add upstream and fetch from upstream

cd Spoon-Knife
$ git remote add upstream git://
$ git fetch upstream

Fetch and merge

$ git fetch upstream Fetches any new changes from the original repo
$ git merge upstream/master

Pull from repo

git pull /Users/daijo/Dropbox/Patchwork/Projects/webpage.git/ master

Discard your changes

git checkout master -f
git clean -f

Revert commit

git reset HEAD^ --hard

If already pushed

git push origin -f

Example .gitignore


Add and commit changed file

git add [file]  
git commit

Change your mind after add

git rm -r --cached .

Can also be used after adding files to .gitignore that has already been added and commited. When adding files again the new .gitignore will exclude what you want.

Push to repo

git push /Users/daijo/Dropbox/Patchwork/Projects/webpage.git/

alt. with remote

git push

Put on Github

Create a repo on github (say it's called created_repo)

cd existing_repo
git remote add origin
git push origin master

Lightweight Tag

git tag tag_name

Viewing available tags is done with -l:

git tag -l

push tags with:

git push --tags

Search for pattern

git grep -p [pattern]

[ IndexPage | Edit on Github | Powered by PotBliki | © 2010-2014 Daniel Hjort - CC BY-SA 3.0 | ]

blog comments powered by Disqus