Andrew Pennebaker
https://github.com/mcandre/cheatsheets/blob/master/git.md
Git is a decentralized version control system.
$ apt-get install git
$ brew install git
C:\> chocolatey install git
$HOME/.gitconfig
$HOME/.gitconfig
can be edited manually, and/or specific fields can be set with git config
commands:
$ git config --global user.name "<Your Name>"
$ git config --global user.email <your@email.com>
$ git config --global core.editor emacs
Git encourages authentication with SSH keys, skipping the need for password authentication. Consult your GitHub or GitLab documentation for more information on setting up SSH keys.
$ git init
$ git add <files or folders>
Git does not version control folders, only files. To effectively force Git to add a directory to version control, we can add a dummy file, .gitkeep
to the desired folder.
$ mkdir empty/
$ touch empty/.gitkeep
$ git add empty/
$ git commit
$ git rm [-r] [-f] <files or folders>
$ git add <files or folders>
$ git commit
git-commit takes an optional -m <message>
to enter the message directly into the shell.
git-commit can load the text editor of your choice for writing commit messages, by customizing .gitconfig
.
Git can commit specific line changes, temporarily ignoring other line changes.
$ git add -p
This launches an interactive patch session in which a commit is constructed based on user selections of yes (y
), no (n
), or split (s
) for each change to consider.
Then commit the partial changes:
$ git commit
$ git log
$ git diff <commit a> <commit b>
For more ways to view Git history, see Pro Git Chapter 2:
http://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History
$ git push
Or
$ git push origin master
$ git pull
$ vi .gitignore
$ git add .gitignore
$ git commit
Example: https://github.com/mcandre/dotfiles/blob/master/.gitignore
project (master)$ git checkout -b myfeature
project (myfeature)$
You can display the current Git branch in your shell prompt by following the Git Prompt setup instructions:
https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh
Tab completion for branches and other git commands can be added to your shell by following the Git Completion instructions:
https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash
project (master)$ git merge myfeature
project (master)$ git commit
$ git tag -a v1.0 -m 'bump to v1.0'
$ git tag -l
v1.0
$ git push --tags
$ git pull --tags
Git purists will complain that you can't technically checkout a tag, but we can effectively do this by checking out a new branch based on a tag.
project (master)$ git checkout -b v1.0 v1.0
project (v1.0)$
Here, we supplied the tag name as the second argument, and the same tag name as the first argument, to git-checkout.
Any scripts named .git/hooks/post-merge
, .git/hooks/post-checkout
, etc., will be run as a side effect of the associated Git process.
Example:
https://github.com/mcandre/dotfiles/blob/master/hooks/post-merge-checkout
Git offers a spare queue for temporarily storing code changes, useful when editing Git history.
$ git stash
$ git status
nothing to commit, working directory clean
$ git stash apply
Sometimes you want to revisit an earlier commit:
$ git checkout <commit>
Sometimes you need to point HEAD to a commit:
$ git reset <commit ID> [--hard]
Reset to an earlier commit and continue working.
$ git reset --hard <an earlier commit>
$ git push origin HEAD --force
If you are quick enough, this will alter Git history on the remote before anyone has pulled the commit you deleted. This could save you if you accidentally pushed sensitive information in a commit.
But in many cases, this will result in a very awkward state, almost corrupting your fellow Git users' Git histories.
$ git branch -d <branch name>
$ git push origin --delete <branch name>
$ git tag -d <tag>
$ git push --delete origin <tag>
Generally, the safer option is to revert the commit, a sort of Git Undo operation:
$ git revert <commit-to-remove> [<another-commit-to-remove> ...]
$ git push
$ git checkout <destination-branch>
project (destination branch)$ git cherry-pick <desired-commit>
$ git submodule init
$ git submodule add <git URL>
$ git submodule foreach git pull
$ git add <path to submodule>
$ git commit
$ git push
$ cd project/
project$ find . -type d -name .svn -exec rm -rf {} \;
project$ git init && git add . && git commit && git remote add origin <...> && git push
This may require upgrading to latest Git (e.g., brew install git
).
$ cd svnproject/
svnproject$ svn log --stop-on-copy .
<revision start point>
$ mkdir gitproject/
gitproject$ git init
gitproject$ git svn clone -r <revision start point>:HEAD <svn URL> .
Create a remote on GitHub/GitLab/BitBucket/Google Code, etc. Then:
gitproject$ git remote add origin <...>
gitproject$ git push
View current branch information:
$ git branch -a
Add an svn remote to .git/config
:
[svn-remote "svn-<branch name>"]
url = <branch URL>
fetch = :refs/remotes/<branch name>
Update git refs:
$ git svn fetch --fetch-all
Checkout the remote svn branch as a local git branch:
$ git checkout -b <branch name> remotes/<branch name>
Push new git branch to git remote:
$ git push -u origin <branch name>
$ git diff --no-prefix [<start commit>[..<end commit>]] > feature-2.git.patch
$ git apply feature-2.git.patch
Or, with the classic patch tool:
$ patch -p0 -i feature-2.git.patch
- GitHub https://github.com/
- BitBucket https://bitbucket.org/
- GitLab https://about.gitlab.com/
- Google Code https://code.google.com/
- Assembla https://www.assembla.com/home
- Beanstalk http://beanstalkapp.com/
- Gitorious https://gitorious.org/
- repo.or.cz http://repo.or.cz/
- SourceForge http://sourceforge.net/
Git can integrate with non-Git repositories:
- git- svn https://www.kernel.org/pub/software/scm/git/docs/git-svn.html
- hg-git http://hg-git.github.io/
- darcs-to-git https://github.com/purcell/darcs-to-git