Dec 14th, 2015
To resync your master with all the changes.
$ git fetch upstream $ git checkout master $ git merge upstream/master $ git push origin master
If there is no upstream
$ git remote -v > origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch) > origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)
$ git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git
$ git remote -v > origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch) > origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (push) > upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (fetch) > upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (push)
Now your master has the latest code. Create a new branch called
ISSUE1 now and you can fix the bug and submit a PR from that branch.
To list branches
$ git branch -a
Create a new branch named
$ git branch ISSUE1
Switch over to the branch
ISSUE1 when you want to add new commits to it.
$ git checkout ISSUE1
Once you are on the
ISSUE1 branch, you can start adding commits to it.
$ git add . $ git commit -m "commit for ISSUE1"
To merge commits into the master branch, let’s now switch over to the master branch.
$ git checkout master
$ git merge ISSUE1
ISSUE1 has been successfully merged with
master, we can delete it.
$ git branch -d <branchname>
You can also use personal access tokens to authenticate against Git over HTTP. They are the only accepted password when you have Two-Factor Authentication (2FA) enabled.
First, you need to create a personal access token (PAT). This is described here:
$ git clone https://github.com/user-or-organisation/myrepo.git Username: <my-username> Password: <my-personal-access-token>
$ git push https://github.com/user-or-organisation/myrepo.git Username: <my-username> Password: <my-personal-access-token>
git checkout 6e559cb95
If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example:
git checkout -b <new-branch-name> git checkout -b newbranch 6e559cb95
To get back to master commit
git checkout master
git tag new old git tag -d old git push origin :refs/tags/old git push --tags
git push origin :refs/tags/tagname git tag --delete tagname
git branch branchname <sha1-of-commit>
git push origin BRANCH_NAME
git clone -b <branch> <remote_repo>
git branch -m old_branch new_branch # Rename the branch locally git branch -m new_branch # Rename the currently checked out branch git push origin new_branch # Then go to GitHub UI settings/options, if the old branch was 'master', change 'Default Branch' to another branch that is not 'master'. Then git push origin :old_branch # Delete the old branch
Rename your local branch
#If you are on the branch you want to rename git branch -m new-name #If you are on a different branch git branch -m old-name new-name
Delete the old-name remote branch and push the new-name local branch
git push origin :old-name new-name
Reset the upstream branch for the new-name local branch
#Switch to the branch and then git push origin -u new-name
// delete branch locally git branch -d localBranchName // delete branch remotely git push origin --delete remoteBranchName
git checkout -b newBranch git status
# Revert changes to modified files. git reset --hard # Remove all untracked files and directories. (`-f` is `force`, `-d` is `remove directories`) git clean -fd
Rebase your local changes on the newest codebase.
git pull --rebase
Firstly, find out the comit that you want to revert back to.
For example, commit 7f6d03 was before the 2 wrongful commits. Force push that commit as the new master:
git push origin +7f6d03:master
The + is interpreted as forced push. You can also use git reset to undo things. Then force push.
git reset 7f6d03 --hard git push origin -f
Setting your branch to exactly match the remote branch can be done in two steps:
git fetch origin git reset --hard origin/master
If you want to save your current branch’s state before doing this (just in case), you can do:
git commit -a -m "Saving my work, just in case" git branch my-saved-work
git remote -v
git log --pretty=format:'%h' -n 1
To create shortcut for long commands. Below defines
loagd instead of
log --oneline --all --graph --decorate. Config file location can be
git config --global alias.loagd "log --oneline --all --graph --decorate"
To list the content of git config
git config --global --list
To find the place of a command
git config --show-origin alias.loagd
To see the content of a command
git config --get alias.loagd
Explicit Merge: Creates a new merge commit. (This is what you will get if you used
Fast Forward Merge: Forward rapidly, without creating a new commit:
Rebase: Establish a new base level:
Squash on Merge: Crush or squeeze (something) with force so that it becomes flat:
Listing the N=10 largest files
git ls-tree -r -t -l --full-name HEAD | sort -n -k 4 | tail -n 10