git config --global user.email "[email protected]"
git config --global user.name "John Doe"
Generate an SSH key; you may already have one in ~/.ssh/id_rsa (private) and ~/.ssh/id_rsa.pub
- Go to your Account Settings
- Click "SSH Keys" in the left sidebar
- Click "Add SSH key"
- Paste your PUBLIC key (*.pub) into the "Key" field
git clone [email protected]:github_username/name_of_the_repository
Make sure git status
shows all local changes are committed (this is to prevent conflicts).
git remote -v
git remote add upstream [email protected]:github_username/name_of_the_repository
git fetch upstream
git merge upstream/master
You can do this at any point in time, also after having made local changes.
Make sure you are in the right branch to begin with! You might want to first go back to master and create the new branch from there.
git checkout master
git checkout -b new_branch_name
Branches that git knows about:
git branch -a
All remote branches:
git ls-remote origin refs/heads/*
git add name/of/the/file.txt
git add -A # use to add all files from .
git add -u # use to tell git to track deleted files
Undo an add:
git reset HEAD name/of/the/file.txt
Remove file from repository and local filesystem:
git rm name/of/the/file.txt
Remove from repository but keep the local copy:
git rm --cached name/of/the/file.txt
If the file is not yet staged:
git diff name/of/the/file.txt
If the file is already staged:
git diff --staged name/of/the/file.txt
Reviewing what has changed betweeen branches:
git diff --stat master..branch_name
for a summary, or
git diff master..branch_name
for a full diff. To limit this to a single file, use:
git diff master..branch_name -- name/of/the/file.txt
git commit -m "Lots of cool new features"
N.B. this is before a push. Reset the working directory to the state before the git commit
.
git reset --soft HEAD~1
git commit -m 'initial commit'
git add forgotten_file
git commit --amend
This also works for changing commit messages:
git commit --amend -m "New commit message"
If you want to reset changes made to your working copy, do this (cannot be undone):
git checkout -- .
or reset a single local file (cannot be undone):
git checkout -- file_to_reset
You can also reset to a particular hash (cannot be undone):
git checkout 53f32ff -- file_to_reset
To go back to the state just before the given commit, use the history shortcut notation (53f32ff~1
).
Start again from scratch: let's say you're on the master branch, and the remote repository is called "upstream". First fetch upstream, and then:
git reset --hard upstream/master
Before doing this, consider the less radical rebasing (git rebase upstream/master
), and/or stash your local changes or place them in their own feature branch before resetting master.
"git-revert" creates a new commit (or commits) that precisely undoes the actions of a previous commit (or commits). Say that 14aa4d9
is the "bad" commit:
git revert 14aa4d9
Use --no-commit
no avoid creating a single undo commit for each commit to be undone.
Push the state of a local branch (e.g. "master") to a remote repository (e.g. "origin").
git push -u origin master
Let's say you want to merge the branch myfeature
into master
, and then remove the branch myfeature
:
git checkout master
git merge myfeature
git branch -d myfeature # delete the now redundant branch (master points at the same location)
git push origin :myfeature # delete the now redundant branch on the remote
git checkout 7529083f7f88787affc6e58bffbad305ba4860e3
You are now in "detached head mode". Detached head means you are no longer on a branch, you have checked out a single commit in the history (in this case the commit previous to HEAD, i.e. HEAD^).
Restore head via:
git checkout master
git checkout --ours /path/to/file
to keep the remote file, and:
git checkout --theirs /path/to/file
to keep the local file.
Then git add.
E.g. check out file1 and file2 from the master branch:
git checkout master -- file1 file2
Use the reflog.
git reflog
Find the entry just before the start of the rebase. Then reset to that.
git reset --hard "HEAD@{2}"
git diff COMMIT~ COMMIT
where COMMIT is the hash, will show you the difference between that COMMIT's ancestor and the COMMIT.
git config --global ssh.postBuffer 524288000
git config --global http.postBuffer 524288000
GIT_PAGER='less -eiFRSX' git diff ...
Stash files:
git stash
Stash all files, including untracked files, using:
git stash --all
Later on, pop from the stash:
# (do stuff, e.g. a git pull)
git stash apply # prefer "apply" to "pop"!
There might be merge conflicts.
In case the apply or pop fails (This will discard any unsaved local changes so be careful):
# backup your entire directory, just in case
git clean -xfd
git stash apply
Or, alternatively:
# backup your entire directory, just in case
git checkout stash -- .