1. Create code review in Gerrit and push to master branch:

git push origin HEAD:refs/for/master

2. Push the code to Gerrit sandbox without code review:

git push origin HEAD:refs/heads/sandbox/xxx

3. Delete the last pushed commit in the remote branch:

git push origin +HEAD^:master # HEAD can be replaced by commit number

4. Apply patches to git repository:

patch -p1 < xxx.patch 
# OR 
git apply xxx.patch

5. Merge branch committed before the current master is merged:

git fetch origin 
git checkout -b your_branch 
git fetch origin 
git checkout origin/master 
git merge --no-ff your_branch 
git push origin HEAD:your_branch

6. Delete local or remote branch:

# Delete remote branch: 
git push origin --delete <your_branch> 
# Delete local branch (1): 
git branch -D <branch_name> 
# Delete local branch (2): 
git branch --delete --force <branch_name> 
# same as -D 
# Delete local branch (3): 
git branch --delete <branch_name> # error on unmerge

7. What does “origin” means in Git?

In Git, “origin” is a shorthand name for the remote repository that a project was originally cloned from. For example, if you clone the repo using: git clone, then “origin” represents

8. Fork a public repository to private

(Option 1) Create a new GitHub repository by importing from the public repository using the GitHub GUI.

(Option 2) Create a blank private repo and push the public repo as a mirror:

git clone --bare
cd public-repo.git
git push --mirror

To download the updates from the public repo:

cd private-repo
git remote add public
git pull public master # Creates a merge commit
git push origin master

We can check the remote sources by:

git remote -v 

The result looks like:

origin (fetch)
origin (push)
public (fetch)
public (push)

The result also explains what does the keyword origin represent.


9. Git Submodule

Add a new remote repository:

git submodule add

Update submodules:

git submodule update --remote --recursive

Remove submodules:

git submodule rm xxxx

10. Git – set-upstream

I always use the following command line to push my branch to the repo:

git push -u origin your_branch

However, I am not sure what does -u mean exactly, even after I checked the poor document for Git.

It turns out that -u is a shorthand flag for –up-stream or –up-stream-to, and it’s doing:

It sets the default remote branch for the current local branch (sets the branch you are pushing to as the remote tracking branch of the branch you are pushing).

Any future git pull command (with the current local branch checked-out), will attempt to bring in commits from the <remote-branch> into the current local branch.

Reference: StackOverflow – What does ‘–set-upstream’ do?

11. Git – Rebase your branch when master changed

git checkout master
git pull
git checkout <your_branch>
git merge --strategy-option theirs master
git push -u origin <your_branch>