Git is a widely used version control system for software development which emphasises speed, data integrity and support for distributed, non-linear workflows -- source.
This is based on a number of sources but especially the excellent http://git-scm.com/book written by Scott Chacon and Ben Straub and published by Apress. Anything in angled brackets needs to be replaced with something suitable. A "#" indicates the start of a comment - so ignore the rest of the line.
E.g. $ git help <command> might mean $git help config
$ git config --global user.name "Stephen Moon"
$ git config --global user.email [email protected]
$ git config --global core.editor "'<path to your editor>'"
$ git config --list
$ git init # inside the directory you want to begin using source control with
$ git clone <url to repository> # e.g. https://github.com/s-moon/CheatSheets.git
$ git clone <url to repository> <your choice of directory name> # e.g. https://github.com/s-moon/CheatSheets.git CheatSheetsGalore
# long form
$ git status
# short form of the same (see: http://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository#Short-Status)
$ git status --short
$ cat .gitignore
*.[oa] # ignore everything ending in o or a e.g. file.oa
*~ # ignore all files which end in the tilde character
# see (http://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository#Ignoring-Files)
$ git config --global init.defaultBranch master
$ git help <command>
$ git add <file path>
$ git add .
$ git add -p
$ git commit -am 'Message'
$ git diff
$ git checkout --- <file you want to lose the edit for>
$ git commit # commit message will be required
$ git commit -m "This is my commit message" # commit message supplied
$ git commit -a # stage all modified (but not new) files and commit them in one.
$ git diff --staged
$ git remote add <name of remote> <url of remote>
$ git push origin
$ git push origin master # use this if it complains about branches/which branch
$ git remote -v
$ git pull upstream master
$ git push origin master
NB - this assumes you have origin pointing to your forked repository and that upstream points to the source repository.
$ git fetch --all
$ git reset --hard origin/master
See: stackoverflow
$ git rm PROJECTS.md # removes from the file system AND from staging
$ git mv file_from file_to
$ git log
$ git log -p -2
$ git log --pretty=oneline # one line version or,
$ git log --format=oneline
- this will allow you to re-do your commit message
$ git commit --amend
$ git add <file-that-I-forgot>
- a second file is added and included in the original commit
$ git commit --amend
$ git reset HEAD <file to undo>
$ git commit --amend --author="Author Name <[email protected]>"
$ git commit --amend
$ git checkout -- <file to undo> # this will revert a file back to before you modified it
- remove all changes
$ git co --
$ git checkout .
$ git checkout -b <branch-name>
$ git branch -d <branch-name>
$ git branch -D <branch-name>
$ git push <remote_name> --delete <branch_name>
$ git co -
- repeat to switch back to previous branch
or
$ git checkout <branch-name>
Let's say thay you have a branch called master (you will) and another which is named . already contains committed changes that you want to merge back into master.
$ git checkout master
$ git merge <branch-name>
After creating a new branch, foo
, you can push it to a remote branch origin
with
the following command ONCE AND FOR THE FIRST TIME:
$ git push -u <remote> <new-branch>
Where is usually master
and <new-branch>
is your new branch name.
$ git submodule add <git-reference> # e.g. https://github.com/zendframework/zf1.git
$ git submodule update --init --recursive
If on Windows, do this in a Git Bash window.
$ git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
$ git fetch --all
$ git pull --all
Steps largely taken from: https://stackoverflow.com/a/63727509
$ git branch branch-name-you-wished-you-had-used
$ git reset abc5b0de1 --hard <replace abc5b0de1 with the commit log id you want to go back to>
$ git push origin master <if master was the branch that commit related to>
$ git checkout branch-name-you-wished-you-had-used
$ git push origin branch-name-you-wished-you-had-used
Written by Stephen Moon ([email protected])