Dec 14th, 2015
git commit --signoff --message 'This is my commit message'
git commit -s -m "This is my commit message"
To resync your main
branch with all the changes.
$ git fetch upstream
$ git checkout main
$ git merge upstream/main
$ git push origin main
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 main
branch 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 ISSUE1
$ 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 main
branch, let’s now switch over to the main
branch.
$ git checkout main
$ git merge ISSUE1
Now that ISSUE1
has been successfully merged with main
, 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 main
branch commit
git checkout main
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
Remove the submodule entry from .git/config
git submodule deinit -f path/to/submodule
Remove the submodule directory from the superproject’s .git/modules
directory
rm -rf .git/modules/path/to/submodule
Remove the entry in .gitmodules and remove the submodule directory located at path/to/submodule
git rm -f path/to/submodule
git branch
git branch branchname <sha1-of-commit>
git push origin BRANCH_NAME
git clone -b <branch> <remote_repo>
git clone --recurse-submodules -j8 <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
// or shorter
git push origin :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.
git log
For example, commit 7f6d03 was before the 2 wrongful commits. Force push that commit as the new main:
git push origin +7f6d03:main
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 HEAD -f
Setting your branch to exactly match the remote branch can be done in two steps:
git fetch origin
git reset --hard origin/main
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 --global
, --system
, --local
, --file
, --blob
.
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
References
Explicit Merge: Creates a new merge commit. (This is what you will get if you used --no-ff
.)
Workflow
git checkout -b development
(do your work on "development" branch)
git status
git commit -am "updates in development branch"
git checkout main
git merge --no-ff development
git branch -d development
git push origin main
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