Simply via:
$ git branch newbranch
To switch the the new branch, do not use the branch
command (according to the help file, the branch
command only lists, creates, or deletes branches), but the checkout command:
$ git checkout newbranch
A shortcut for both is:
git checkout -b newbranch
See here.
To add the new branch to the upstream repository, use
git push --set-upstream origin newbranch
git will also tell you about this command when just doing git push
.
If you have made changes in one branch but would like to work in the other branch, one option is stashing:
$ git stash
$ git checkout branch2
$ git stash pop
However, you can just create the new branch and commit the changes there.
See here
This source discusses that a commit in the old branch resetting it later is a safer option and (see below).
Add the local tag:
git tag name
Push all tags:
git push origin --tags
To set up an annotated tag for a new version, use:
git tag -a v0.1 -m "this is the new version of my software 0.1"
Alternative to pushing this specific tag:
git push origin v0.1
To merge two a branch into another branch, use:
git merge target_branch
In this case, the commits in branch target_branch
is merged into the currently checked out branch (common example: check out to master, and merge a development branch).
Merging means: collect the commits from one branch and put them into the branch that I am currently working on – not the other way around.
When working on a development branch that has to be merged into a master branch and – for some stupid reason – clean merging does not work, use git merge with the option ours
(when merging master into devel) or theirs
(when merging devel into master).
$ git merge branch -X theirs
See here
Show the untracked files that would be removed:
$ git clean -d -n
The -n
option shows the to-be-removed files.
Then, delete all untracked files:
$ git clean -d -f
The -f
option is usually needed to force the action.
This command undoes the most recent commit but keeps the changes (that are unstaged then):
$ git reset HEAD~
HEAD~ is the parent of head (i.e., our latest commit on the current branch)—the previous commit.
The following will undo the latest commit and remove all work done since the last commit (danger territory).
$ git reset --hard HEAD~
When performing a merge pull request on Github (and afterwards deleting the obsolete devel branch there), the local repository will still believe that the branch exists remotely. Use the following commands to first remove the branch locally and then the reference to the remote branch:
$ git branch -d devel
$ git fetch --prune
The latter command removes the remote reference from the local repository.
Using git diff myfile
does not work when the changes are already staged, but sometimes we still want to see the changes that have been done since the most recent commit. This works:
$ git diff HEAD myfile
HEAD is usually the most recent commit on the branch we are working on, the parent of our next commit (see here).
$ git log --pretty=oneline
When I create a git repository locally and not on Github first and then start pushing to a remote repository (such as Github), I have to configure git to know where to push. First, add the remote as described here:
$ git remote add origin https://github.com/user/repo.git
# Set a new remote
$ git remote -v
# Verify new remote
> origin https://github.com/user/repo.git (fetch)
> origin https://github.com/user/repo.git (push)
Then, make sure that pushing and pulling actually uses this remote by default, as described here:
git push -u origin my_branch
Stashing is something different than staging; stashing can be done to not commit dirty changes, for example if we wish to switch to a different branch before we have finished our work. Staging is the more common process done via git add
. Files can be stashed using git stash
and retrieved using git stash pop
.
This post has a nice example of what to do when some commits go wrong and should be reverted: git: fetch and merge, don’t pull
If you want to browse the code at a certain commit, do:
git checkout -b tmpbranch # use throw-away branch
git reset --hard tag # remove all changes since tag/commit
git clean -f -d # remove all untracked files / directories
After checking out the code, switch back from the branch and remove it.
git checkout master
git branch -d tmpbranch
Last updated: 2020-10-22