Hands-on commands
Checkout remote branch:
git checkout -b <name> origin/<name>
Checkout directory to given changeset:
git checkout 3257289075289037592abcde253 -- path/to/the/folder/
Multiple working trees sharing one repository:
git worktree add ../some/path/ branch
Checkouts branch
under ../some/path/
Compare file with branch:
git diff mybranch otherbranch -- somefile
Transfer single commit between branches:
git checkout desired_branch
git cherry-pick commit_to_be_transferred_here_by_hash
git checkout orig_branch
#rollback commit
git reset --hard HEAD^
Branches
Checkout
Remote to local:
git checkout -b <local_branch_name> <remote_name>/<remote_branch_name>
Delete
Remote:
git push <remote_name> --delete <branch_name>
Locally:
git branch -d <local_branch_name>
Rename
First rename locally, then delete old branch on remote, finally push "new"
git branch -m old_branch new_branch
git push origin :old_branch
git push --set-upstream origin new_branch
Submodules
Project can depend on other projects. Root project is called superproject.
Submodule is always a commit in other repostiory. In other words: submodule points to particular commit.
Adding
git submodule add https://repo.url... [path]
add submodule to project at path.
If no path is given then repo name is used.
Deleting
git submodule deinit <submodule>
git rm <submodule>
rm -rf .git/modules/asubmodule
Cloning
By default submodules are not cloned. In order to clone repository that contains submodules:
git clone <repo url>
git submodule init
git submodule update
Upgrading
In order to "upgrade" the submodule (actually submodule's commit):
cd submodule_dir
git checkout desired_branch
git pull
cd ..
git add submodule_dir && git commit -m "..."
Other
In order to change submodule's URL, edit .gitmodules files and run git submodule sync
Useful infographic
References
- http://www.saintsjd.com/2011/01/what-is-a-bare-git-repository/
- http://gitolite.com/gcs.html#%281%29