Git
- A great book (Chinese) - 為你自己學 Git
Checkout
Checkout some files
This can be useful when you want to test the two version of a file in two branches.
$ git checkout <branch> [--] <pathspec>
# --
# Do not interpret any more arguments as options.
#
# <pathspec>...
# Limits the paths affected by the operation.
#
# Examples:
$ git checkout mybranch -- somefile.cpp
$ git checkout mybranch -- somefile.cpp secondfile.cpp
$ git checkout mybranch -- path/to/be/checkout
Cherry-pick
For example, if you are writing homework, you may keep the original homework template in a branch template
and cherry-pick
those newly added homework templates each week into your main
branch.
$ git checkout main
$ git cherry-pick template
Branch
Create
$ git checkout -b hotfix
Switched to a new branch "hotfix"
Merge
Assume that you are in hotfix
branch and want to merge it back into master
branch:
$ git checkout master
$ git merge hotfix
Remote
Check
$ git remote -v
origin https://github.com/myuser/test.git (fetch)
origin https://github.com/myuser/test.git (push)
Add
$ git remote add origin git@github.com:myuser/test.git
$ git remote add whaterevername git@github.com:myuser/test.git
Push
First time, we need setup upstream by -u
. For example,
$ git push -u origin master
master
branch to origin/master
. After that you only need to do
$ git push
$ git push origin <LocalBranch>[:RemoteBranch]
# examples:
$ git push origin master
$ git push origin master:myremotemain
Multiple Remote
$ git remote add second git@github.com:myuser/test.git
$ git remote -v
origin https://github.com/myuser/test.git (fetch)
origin https://github.com/myuser/test.git (push)
second https://github.com/seconduser/test.git (fetch)
second https://github.com/seconduser/test.git (push)
$ git fetch second
$ git merge second/master
$ git push origin master
Tag
List
$ git tag
v1.0
v2.0
v2.1
$ git tag show v1.0
...
Create
Annotated tags are meant for release while lightweight tags are meant for private or temporary object labels.
-
Lightweight Tags
# current commit $ git tag v1.0 # past commit (51d54ff) $ git tag v1.0 51d54ff
-
Annotated Tags
# current commit $ git tag v1.0 -a -m "Version 1.0" # past commit (51d54ff) $ git tag v1.0 51d54ff -a -m "Version 1.0"
Delete
$ git tag -d v1.0
Push
- Ref: https://stackoverflow.com/questions/5195859/how-do-you-push-a-tag-to-a-remote-repository-using-git
- Push a single tag
$ git push <remote> <tagname> # examples: $ git push origin v1.0
- Push all tags (NOT recommended)
$ git push --tags
Last update:
September 19, 2022