https://git-scm.com/

Show origin

1
$ git remote show origin
Reference:

Change origin

1
2
$ git remote rm origin
$ git remote add origin https://github.com/jslim89/dotfiles

another way is

1
$ git remote set-url origin git@github.com:jslim89/dotfiles.git
Reference:

Merge with other’s github repo

Setup

$ git remote add upstream https://github.com/otheruser/repo.git

Sync

$ git fetch upstream

Merge

$ git checkout master
$ git merge upstream/master

Reference:

Add / Update / Delete submodule

1
2
3
4
5
6
$ git submodule add git@github.com:user/repo.git /path/to/repo # adding a submodule

$ git submodule init
$ git submodule update # update submodule

$ git rm --cached /path/to/repo # remove submodule
Reference:

Keep the source from branch and content to master (Octopress)

I’ve tried octopress on github. I realize that it only shows the static html files in my [GitHub repo] (https://github.com/jslim89/jslim89.github.com). A question come out from my mind, that is
What if I lost my source files (i.e. Rakefile, _config.yml, etc)?
A solution that I found is

1
2
3
4
5
6
7
8
9
$ git clone https://github.com/jslim89/jslim89.github.com.git
$ cd jslim89.github.com # You should see those static HTML files
$ git checkout source # It keeps all source in `source` branch, now no more HTML files
$ mkdir _deploy
$ cd _deploy
$ git init
$ git remote add origin https://github.com/jslim89/jslim89.github.com.git
$ git pull origin master # check out the master (Static HTML files)
$ cd ..

Actually during the octopress setup, they push the source to another branch rather than master.

Reference:

un-add a file before commit

1
$ git reset
Reference:

un-delete before commit

1
$ git checkout <filename>
Reference:

Accidentally pushing sensitive date to octopress blog on github repo

1
2
$ git filter-branch --index-filter 'git rm -r --cached --ignore-unmatch filepath/filename' --prune-empty --tag-name-filter cat -- --all
$ git push origin master --force
Reference:

Un-modify a file before commit

1
$ git checkout -- filename
Reference:

Problem with no HEAD branch

1
2
3
4
5
6
$ git remote show origin
Identity added: /Users/jslim/.ssh/id_rsa (/Users/jslim/.ssh/id_rsa)
* remote origin
Fetch URL: git@yourdomain.com:/yourporject.git
Push URL: git@yourdomain.com:/yourporject.git
HEAD branch: (unknown)

When I type the same command to my learning journey, it has HEAD branch

1
2
3
4
5
6
7
8
9
10
11
$ git remote show origin
* remote origin
Fetch URL: git@github.com:jslim89/js-learning-journey.git
Push URL: git@github.com:jslim89/js-learning-journey.git
HEAD branch: master
Remote branch:
master tracked
Local branch configured for 'git pull':
master merges with remote master
Local ref configured for 'git push':
master pushes to master (up to date)

When first I see this, I was thinking about how to add master to the HEAD. In fact, it is not necessary

1
2
3
4
5
6
7
8
$ git push origin master
Counting objects: 18, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (16/16), done.
Writing objects: 100% (18/18), 13.30 KiB, done.
Total 18 (delta 1), reused 0 (delta 0)
To git@yourdomain.com:/yourporject.git
* [new branch] master -> master

Show origin again

1
2
3
4
5
6
7
8
9
$ git remote show origin
* remote origin
Fetch URL: git@yourdomain.com:/yourporject.git
Push URL: git@yourdomain.com:/yourporject.git
HEAD branch: master
Remote branch:
master tracked
Local ref configured for 'git push':
master pushes to master (up to date)

Checkout specific revision

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Just to play safe, clone to a new folder
$ git clone git@yourdomain.com:/yourporject.git
$ git checkout <SHA1>
Checking out files: 100% (6466/6466), done.
Note: checking out '49869aec20e12345c40cbabcde0f5e8e959d5aa'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

git checkout -b new_branch_name

HEAD is now at 49869ae... Your commit message here
Reference:

Change last commit message

1
$ git commit --amend -m "New commit message"
Reference:

Show commited files on a specific revision

1
2
3
$ git diff-tree --no-commit-id --name-only -r 16fafae4328371850e21ad34e6f4b083bc9e566c
controller/Index.php
model/UserModel.php
Reference:

Update all EXCEPT for a file

Commit all files except /path/to/file.txt

1
2
3
$ git update-index --assume-unchanged /path/to/file.txt
$ git commit -a -m "Commit msg"
$ git update-index --no-assume-unchanged /path/to/file.txt
Reference:

Change submodule origin

  1. Edit .gitmodule with the new URL
  2. Run git submodule sync (This will update the new URL to the submodule)
Reference:

git tag

1
2
3
4
5
6
$ git commit -am "completed version 1.0"
# tag current commit on local
$ git tag v1.0
$ git push origin master
# push the tag to remote
$ git push --tags # or git push origin v1.0
Reference:

Clone a specific branch from remote branch

1
2
3
$ git clone git://example.com/myproject
$ cd myproject
$ git checkout -b wanted_branch origin/wanted_branch
Reference:

un-commit last commit

1
$ git reset --hard HEAD^
Reference:

Rename branch

E.g. rename dev branch to master

1
2
3
4
5
6
# switch to `dev` branch
$ git checkout dev
# delete `master` branch
$ git branch -D master
# rename current branch to `master`
$ git branch -m master
Reference:

Show updated file name between 2 commits

1
2
$ git diff --name-only d92bd53 ffc95b0
$ git diff --name-only HEAD~10 HEAD~5
Reference:

Choose specific key for specific host

Create/Edit ~/.ssh/config, add

1
2
3
4
Host bitbucket.org
HostName bitbucket.org
IdentityFile ~/.ssh/my_new_rsa
IdentitiesOnly yes
Reference:

Merge certain files from other branch

1
2
3
4
5
6
$ git checkout source_branch path/to/file1.php path/to/deep/path/file2.php
$ git status -s
A path/to/file1.php
A path/to/deep/path/file2.php

$ git commit -m "merge from 'source_branch'"
Reference: