Git

A distributed version control system. (Probably the most widely used?)
web | project | wiki | glossary

See also (clients) | :rabbitvcs :tortoisegit :git:windows
See also (topics) | :version-control

Notes

Simple tasks (TODO)

Copy remote repository

If you are copying an existing remote repository for the first time into an empty folder:

git clone <URL>
# clone remote repo into new folder of the same name
# switches to branch "master" by default

git clone <URL> .
# clone remote repo into current (empty) folder
# switches to branch "master" by default

If you are copying an existing remote repository into a non-empty folder:

git init
# create local repository in current directory (may be non-empty)
git remote add <SHORTNAME> <URL>
# add reference to remote repo
git fetch
# update copy of remote repo
git checkout -t <SHORTNAME>/<BRANCH>
# switch to branch on remote repo

Copy upstream repository

If you are copying a known "upstream" repository:

git clone --origin upstream <URL>
# names the remote "upstream"

You can later add your own remote named "origin" as follows:

git remote add origin <URL>
# add a new remote named "origin"
git fetch --all
# fetch branches from all remotes (including the new one)

Managing local branches

git branch -m <OLD_NAME> <NEW_NAME>
# rename local branch

git branch -m <NEW_NAME>
# rename current local branch

Managing remote branches (including deletion)

git push <REMOTE> <LOCAL_NAME>:<REMOTE_NAME>
# updates/creates branch named <REMOTE_NAME> at <REMOTE> repo
# that branch will be a copy of the local branch named <LOCAL_NAME>
# push a specific local branch to a specific remote branch
# only required is remote and local names are different (see below)

git push
# equivalent to git push origin <LOCAL_NAME>:<LOCAL_NAME>
# origin is "your" remote repo
# push your current branch to your "origin" repo

git push <REMOTE>
# equivalent to git push <REMOTE> <LOCAL_NAME>:<LOCAL_NAME>
# push your current branch to an alternate remote repo

git push <REMOTE> <LOCAL_NAME>
# equivalent to git push <REMOTE> <LOCAL_NAME>:<LOCAL_NAME>
# push a specific branch to an alternate remote repo

git push <REMOTE> :<REMOTE_NAME>
# delete remote branch called <REMOTE_NAME>

TODO: presumably git pull works in a similar way
TODO: worth adding remote rename from http://stackoverflow.com/questions/1526794 ?

Undo/redo a commit

git commit --message="Initial commit message"
git reset HEAD~
# perform fixes
git add ... # add files
git commit -c ORIG_HEAD
# -c opens the editor to modify the commit message

Reference: https://stackoverflow.com/questions/927358

Perform an interactive rebase

An interactive rebase allows you to edit the log history, typically by "squashing" multiple commits into one:

git rebase -i HEAD~?
# ? = number of commits to combine
# or just git rebase -i

# follow on screen instructions to pick commits
# typically: reorder as required, keep, edit/reword, fixup/squash
# edit and save modifications

git rebase --continue
git log # confirm changes have been made

See also: Squash option https://github.com/jnothman/git-squash

Visualise branch history

git log --all --decorate --oneline --graph

See also: https://stackoverflow.com/questions/12324050

Cherry pick

git cherry-pick <COMMIT_ID>

Reference: http://nathanhoad.net/how-to-cherry-pick-changes-with-git

Submodules

https://git-scm.com/book/en/v2/Git-Tools-Submodules (note modern approach)
https://git-scm.com/docs/git-submodule

Extract subfolder as new repo

https://stackoverflow.com/questions/25274614
subdirectory-filter in https://git-scm.com/docs/git-filter-branch

See also | :git:bfg

References



Backlinks: version-control tortoisegit github rabbitvcs microsoft:windows about:script git:dulwich

CC0 / Public domain dedication To the extent possible under law, d3vid seaward has waived all copyright and related or neighboring rights to "Git in Grasmere notebook, including code snippets" (why? how?)