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 origin/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 https://repo.link, then “origin” represents https://repo.link.
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 https://github.com/exampleuser/public-repo.git cd public-repo.git git push --mirror https://github.com/yourname/private-repo.git
To download the updates from the public repo:
cd private-repo git remote add public https://github.com/exampleuser/public-repo.git 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 https://github.com/dreamibor/mlpractical.git (fetch) origin https://github.com/dreamibor/mlpractical.git (push) public https://github.com/CSTR-Edinburgh/mlpractical.git (fetch) public https://github.com/CSTR-Edinburgh/mlpractical.git (push)
The result also explains what does the keyword origin represent.
9. Git Submodule
Add a new remote repository:
git submodule add https://github.com/xxx.git
git submodule update --remote --recursive
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).
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>