- Git Recipes and Commands
- Commands
- Recipes
- Creating a new branch from command line
- Downloading a newly created branch of your fork
- Syncing a fork: Merging commits from main repo master into your fork's master
- Rebasing a branch of your fork to the your fork's master
- Pulling all changes
- Merging a PR to a branch locally to test out the changes
- Updating an Exisiting Branch on Your Fork with Latest Code From Master on Main Repo
- Updating a New Branch on Your Fork with Latest Code From Master on Main Repo
- Targeting a PR to a branch other than Master
- Undoing changes made to fork's master by mistake
- Reverting your last change
- Using the same Pravega Version as the Hadoop Connector
- Disabling push to upstream
- Cherry-Picking a PR for merge from master to a branch
- Resolving Merge Conflicts on a Branch of Your Fork
- Stashing and Unstashing
Table of contents generated with markdown-toc
Assumptions:
- Remote
upstream
is pravega/pravega.git - Remote
origin
is youracount/pravega.git
-
Display commit logs
$ git log $ git log --oneline # See more options at https://git-scm.com/docs/git-log
-
Checkout
# git checkout <commit-id> # Revert changes since last commit $ git checkout . # Revert changes since last commit on a file $ git checkout <file-name> # Checkout an existing branch $ git checkout <branch-name> # Create a new branch and and check it out $ git checkout -b <branch-name> # See more options here: https://guide.freecodecamp.org/git/git-checkout/
Further Reading:
$ git clone https://github.com/ravisharda/pravega.git
$ git checkout -b issue-3227-auth-logic
(verify using 'git branch')
$ git push origin issue-3227-auth-logic
Pravega:
$ git clone https://github.com/ravisharda/pravega.git
$ cd pravega
$ git branch
$ git checkout -b <branch-name>
$ git branch
Pravega Samples: Note: In Samples, PRs are against develop branch, not against master (please, check https://github.com/pravega/pravega-samples/wiki/How-to-release#about-this-repository).
$ git clone https://github.com/ravisharda/pravega-samples.git
$ cd pravega-samples
$ git branch
$ git checkout -b issue-181-security-samples
$ git branch
(Alternatively, you can sync a fork's branch from the main repo's branch: just replace master with the branch name in the commands below, and in step 1, do a git clone -b <branch-name> ...
S.No. | Command | Description | Verification (if any) |
---|---|---|---|
1 | git clone https://github.com/ravisharda/pravega.git |
Download the forked copy from Github | - |
2 | cd pravega |
- | - |
3 | git remote add upstream https://github.com/pravega/pravega.git |
Add an upstream remote branch, which will facilitate pulling down new commits from the main repo | git remote -v or git remote show upstream |
5 | git remote set-url --push upstream no-pushing |
Prevent pushes to upstream | - |
6 | git fetch upstream |
Fetch the branches and their respective commits from the upstream repository. Commits to master will be stored in a local branch, upstream/master. | - |
7 | git checkout master |
Check out your fork's local master branch | git branch |
8 | git merge upstream/master |
Merge the changes from upstream/master into your local master branch. This brings your fork's master branch into sync with the upstream repository, without losing your local changes. | - |
9 | git push origin master |
Syncing your fork only updates your local copy of the repository. To update your fork on GitHub, you must push your changes. | - |
10 | Go to https://github.com/ravisharda/pravega/ and inspect the message |
Verify that your fork's master is even with upstream. |
Source: GitHub Help - Syncing a Fork
$ git clone https://github.com/<youraccount>/pravega.git
$ cd pravega
$ git checkout <branch-name>
$ git rebase master
$ git log
$ git push origin <branch-name>
- Pulling all changes...
git pull
- ... in the remote repo to master:
git pull origin master
- ... in the remote repo to a branch:
git pull origin <branch-name>
(Will fail if there are merge conflicts)
# git fetch origin pull/ID/head:BRANCHNAME
# Assumptions:
# PR no. = 3356, branch that you want to test out is: r0.4
#
$ git clone https://github.com/pravega/pravega.git
$ git fetch origin pull/3356/head:r0.4
$ git checkout r0.4
See more at https://help.github.com/en/articles/checking-out-pull-requests-locally
Similar experience, when trying out a colleague's PR (PR 217 vijikarthi:issue-pravega-security
):
Forked the repo
$ git clone https://github.com/ravisharda/flink-connectors.git
$ git remote add upstream https://github.com/pravega/flink-connectors.git
$ git fetch upstream pull/217/head:issue-pravega-security
Yet another way - actually a much simpler one:
# Recursive is necessary for flink-connections project
$ git clone --recursive -b issue-pravega-security https://github.com/vijikarthi/flink-connectors
- Download the branch from your fork:
git clone -b <branch_name> https://github.com/ravisharda/pravega.git
- Add an upstream remote branch, which will facilitate pulling down new commits from the main repo:
$ git remote add upstream https://github.com/pravega/pravega.git $ git remote -v $ git remote set-url --push upstream no-pushing $ git remote -v
- Perform the rebase:
- First make sure you are currently in the feature branch:
git branch
- Fetch all the new changes from the main repo/upstream:
git pull --rebase upstream master
- Fix any mrge conflicts that might arise.
- Continue rebasing:
git rebase --continue
- Verify the code.
- Now, push all the changes you have made to the branch to the master of your forked repo:
$ git push --force origin <pull-request-branch-name> # In some cases you might have to force push. $ git push --force origin <pull-request-branch-name>
- First make sure you are currently in the feature branch:
- Download the forked copy from GitHub:
git clone https://github.com/ravisharda/pravega.git
- Add an upstream remote branch, which will facilitate pulling down new commits from the main repo:
git remote add upstream https://github.com/pravega/pravega.git
. (Verify usinggit remote -v
) - Make a new branch:
git checkout -b <branch_name>
- Make changes to the code as needed.
- Commit the changes you have made.
- Push up to your GitHub fork:
- First make sure you are currently in the feature branch:
git branch
- Fetch all the new changes from the main repo/upstream:
git pull --rebase upstream master
- Fix any mrge conflicts that might arise.
- Continue rebasing:
git rebase --continue
- Verify the code.
- Now, push all the changes you have made to the branch to the master of your forked repo:
git push --force origin <pull-request-branch-name>
- First make sure you are currently in the feature branch:
In this case your forked repo/branch will be ahead of forked repo/master. Not something you often want to do. It might be useful when you want to forked branch to track a branch on the main repo.
I used these steps for one of the PRs in pravega-samples repo:
git clone https://github.com/ravisharda/pravega.git
- Creates issue-181-security-samples-dev branch off develop:
git checkout -b issue-181-security-samples-dev develop
- Checkout the new branch:
git checkout issue-181-security-samples
- Made changes.
- Committed and pushed as usual.
Use git reset approach described here.
- Identify the commit you want to revert to:
git log --oneline
git reset --hard d9a15c2b6
git push --force
git reset HEAD~1 // this undo the last commit //git add -u (optionally, add some changes) git commit -s git push --force
cd $hadoop-connector/pravega
git log
to get the commit point.- Checkout Pravega to that commit point.
git clone https://github.com/pravega/pravega.git git checkout <COMMIT>
This'd be useful when you have commit access to main and want to prevent any inadvertant push to main's master:
$ git remote -v
origin https://github.com/ravisharda/pravega.git (fetch)
origin https://github.com/ravisharda/pravega.git (push)
upstream https://github.com/pravega/pravega.git (fetch)
upstream https://github.com/pravega/pravega.git (push)
# Update/override the push URL to be something other than the pull URL
$ git remote set-url --push upstream no-pushing
$ git remote -v
origin https://github.com/ravisharda/pravega.git (fetch)
origin https://github.com/ravisharda/pravega.git (push)
upstream https://github.com/pravega/pravega.git (fetch)
upstream no-pushing (push)
Say, source = master, target = r0.6
First, make sure the target branch in your form is up-to-date. Follow the steps mentioned under "Syncing a fork..", replacing the master branch with the target branch.
Now, perform the cherry pick.
Create a branch in your form, say `issue-3696-cherrypick-pr-4420-to-r0.6` from the target branch (`r0.6` in this case).
# git clone https://github.com/ravisharda/pravega.git
$ git checkout issue-3696-cherrypick-pr-4420-to-r0.6
# Verify you are on the right branch.
$ git branch
# Identify the commit id of the PR that you are trying to cherry pick. Use that one below.
$ git cherry-pick b8911f8f4c0b452d18cb34e3d6ce23a13d6eccee
$ git status --oneline
$ git push origin
# Now, navigate to Github repo online and create a PR keeping source as `ravisharda/issue-3696-cherrypick-pr-4420-to-r0.6` and target as say, `pravega/r0.6`.
Say, source: ravisharda/src-branch (created out of master), target = ravisharda/target-branch (created based on r0.6)
git clone ...
Create a branch "target-branch" based out of r0.6 branch
git checkout master
git fetch
git checkout target-branch
git status
Note the commits (from... to)
git cherry-pick d32d46ac4b71d581ea58f53c3a1a36b05e5f619c..86b239c71b4ccfdf7ce527d4239bcae54a07fa7f
If the previous step doesn't work
git cherry-pick -abort
git cherry-pick commit-id-4
git cherry-pick commit-id-3
git cherry-pick commit-id-2
git cherry-pick commit-id-1
git commit
Push the commits
When working on a recent PR, I encountered an issue where my branch had merge conflicts. Here are the steps I followed to resolve them:
- Ensure remote upstream for fetch operation is set. Check it is by running:
git remote -v
. Expect to see an output like the following:origin https://github.com/ravisharda/pravega.git (fetch) origin https://github.com/ravisharda/pravega.git (push) upstream https://github.com/pravega/pravega.git (fetch) upstream no-pushing (push)
git fetch upstream
- Perform the merge.
$ git branch * issue-3728-batch-client-auth master $ git stash $ git checkout master Switched to branch 'master' Your branch is up to date with 'origin/master'. $ git status on branch master Your branch is up to date with 'origin/master'. nothing to commit, working tree clean # Merge upstream's master to local master. We are already on local master. $ git merge upstream/master $ git status n branch master Your branch is ahead of 'origin/master' by 19 commits. (use "git push" to publish your local commits) nothing to commit, working tree clean $ git checkout issue-3728-batch-client-auth Switched to branch 'issue-3728-batch-client-auth' $ git merge master Auto-merging controller/src/main/java/io/pravega/controller/server/rpc/grpc/v1/ControllerServiceImpl.java CONFLICT (content): Merge conflict in controller/src/main/java/io/pravega/controller/server/rpc/grpc/v1/ControllerServiceImpl.java Automatic merge failed; fix conflicts and then commit the result. # Now, used IntelliJ resolve conflicts feature to perform a merge. $ git status git status On branch issue-3728-batch-client-auth All conflicts fixed but you are still merging. (use "git commit" to conclude merge) Changes to be committed: modified: build.gradle modified: client/src/main/java/io/praveg.. ... $ git commit -s [issue-3728-batch-client-auth b10a1635f] Merge branch 'master' into issue-3728-batch-client-auth $ git status On branch issue-3728-batch-client-auth nothing to commit, working tree clean
- Push the changes. I did it via IntelliJ.
Say you were in a branch and had some deltas. Now you want to work in another branch.
> 3669-tls-material/pravega$ git branch -v
* issue-3669-improve-default-tls-material fac451837 Use the new PKI/TLS material.
master cb253c8c7 Issue #3624: Fix backward compatibility issue with WireCommands. (#3640)
> 3669-tls-material/pravega$ git remote -v
origin https://github.com/ravisharda/pravega.git (fetch)
origin https://github.com/ravisharda/pravega.git (push)
> 3669-tls-material/pravega$ git stash
Saved working directory and index state WIP on issue-3669-improve-default-tls-material: fac451837 Use the new PKI/TLS material.
> 3669-tls-material/pravega$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
> 3669-tls-material/pravega$ git remote -v
origin https://github.com/ravisharda/pravega.git (fetch)
origin https://github.com/ravisharda/pravega.git (push)
> 3669-tls-material/pravega$ git checkout issue-3669-improve-default-tls-material
Switched to branch 'issue-3669-improve-default-tls-material'
> 3669-tls-material/pravega$ git stash pop
On branch issue-3669-improve-default-tls-material
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: config/logback.xml
modified: standalone/src/main/java/io/pravega/local/LocalPravegaEmulator.java
Untracked files:
(use "git add <file>..." to include in what will be committed)
config/.cleanup_pki.sh.swp
config/bak/
config/ca-cert.srl
config/cleanup_pki.sh
config/custom-csr.conf
config/junk/
config/server.csr
config/server.keystore.p12
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (86b7ac7e7689c7d258e899e066a18307d79eb83e)
> 3669-tls-material/pravega$