Skip to content

Instantly share code, notes, and snippets.

@eonil
Last active August 29, 2015 14:06
Show Gist options
  • Save eonil/eee843700cb271029734 to your computer and use it in GitHub Desktop.
Save eonil/eee843700cb271029734 to your computer and use it in GitHub Desktop.
Git How-To

Git How-To

Hoon H.

Push to a Remote

https://help.github.com/articles/pushing-to-a-remote

git push  <REMOTENAME> <BRANCHNAME>
git push  <REMOTENAME> <LOCALBRANCHNAME>:<REMOTEBRANCHNAME> 

Cancel All Uncommitted Changes

git reset           # This unstage all uncommited changes, but they still remain in working tree.
git checkout .      # This reloads all files from latest commit into working tree.

Reset to Remote Version

http://stackoverflow.com/questions/1628088/how-to-reset-my-local-repository-to-be-just-like-the-remote-repository-head

git fetch origin
git reset --hard origin/master

Bringing All Submodules At Once

git pull --recurse-submodules
git submodule update --recursive

This fetches submodules of currently linked revision, and does NOT UPDATE REVISION linkage to a newer version. This is a feature for the situation that you git clone a new repository because it doesn't bring the linked submodules automatically. (except you set some explicit parameters...)

If you want to update the submodule linkage, you need to git pull on each submodule directory and commit on master repository.

Reference: http://stackoverflow.com/questions/1030169/easy-way-pull-latest-of-all-submodules#answer-8084186

Adding Submodules

This is THE ONLY WORKING way. Do not try clone command.

  1. Move to desired directory.

  2. git submodule add <url>

  3. Do this for all submodules you want.

    If the added submodule has another submodules, it won't be downloaded automatically. Then, run this to make it downloaded.

  4. git submodule update --init --recursive

  5. Commit at this point.

     git add . -A
     git commit -a
    

Source: http://stackoverflow.com/questions/1535524/git-submodule-inside-of-a-submodule-nested-submodules

Updating a Submodule

Move to the root directory of the submodule. And,

git pull

You're done.

Updating All Submodules at Once

git submodule foreach git pull
git submodule foreach git pull origin master # If the submodule does not have a default upstream...

This effectlvely updates submodule linkages to newer versions. Anyway, this does not commit, so you should add/commit from the master repository to persist the revision link update. Otherwise changes will remain only in working tree, and the working tree will remain as dirty.

Reference: http://stackoverflow.com/questions/1030169/easy-way-pull-latest-of-all-submodules#answer-1032653

Removing Submodules

  1. Move to a container directory of a submodule that you want to delete.

  2. git submodule deinit <path-to-submodule-without-trailing-slash>

  3. git rm -r <path-to-submodule-without-trailing-slash>

    Second command will also erase files in working tree.

  4. Move to repository root.

  5. rm -r .git/modules/<path-to-submodule-from-repo-root-without-trailing-slash>

    Then now you can commit changes.

  6. git add . -A; git commit -a

If Something goes wrong on submodules...

You need to erase them all and re-establish them all again. http://stackoverflow.com/questions/19508849/how-to-fix-broken-submodule-config-in-git

Merging Two Repositories

This entry shows the best way.

# in proj2:
git remote add proj1 path/to/proj1
git fetch proj1
git merge proj1/master # or whichever branch you want to merge

Note that;

  1. You have to add a remote name. It doesn't work direct remote path.
  2. You must fetch. Repository will be corrupted without fetching.
  3. Repositories are merged at root level. Move mergee files into desired subdirectory before merging.

This is far better solution than git subtree merge. Works very well.

Splitting Repository

Just follow this procedure. Very precise, correct and simple.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment