Skip to content

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
That sets the default upstream of local master branch to origin/master. After that you only need to do
$ git push
Otherwise, we need to execute
$ git push origin <LocalBranch>[:RemoteBranch]
# examples:
$ git push origin master
$ git push origin master:myremotemain
every time.

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


Last update: September 19, 2022