Git & branches - bradsorour/notes GitHub Wiki
In your github fork, you need to keep your master branch clean (i.e. without any changes). Each time that you want to commit a bug or a feature, you need to create a branch for it, which will be a copy of your master branch.
$ git clone [email protected]:hmcts/ia-ccd-definitions.git
Pull the changes from upstream. Your master needs to be up to date.
$ git pull
Create the branch on your local machine and switch in this branch:
$ git checkout -b [name_of_new_branch]
Push the branch on github:
$ git push origin [name_of_new_branch]
or
$ git push --set-upstream origin [name_of_new_branch]
When you want to commit something in your branch, be sure to be in your branch. Add -u parameter to set upstream.
You can see all branches created by using:
$ git branch -a
$ git checkout -b test <name of remote>/test
E.g.,
$ git checkout -b test origin/test
E.g.,
$ git checkout -b feature/autofill origin/feature/autofill
Create a new repository in GitHub by clicking Repositories -> New and copy the repository URL. Then run the following commands:
$ git init
$ git add .
$ git commit -m "initial commit"
$ git remote add origin <name-of-copied-github-repository-url>
$ git push origin master
If you get an error because origin is not available, you may have followed the incorrect order of Git configuration. You may have already added a 'git origin' to your .git configuration. You can change the remote origin in your Git configuration with the following line:
$ git remote set-url origin <name-of-copied-github-repository-url>
$ git checkout [branch_name]
Which will show :
* approval_messages
master
master_clean
$ git remote add [name_of_your_remote] [name_of_your_new_branch]
Push changes from your commit into your branch
$ git push origin [name_of_your_new_remote]
$ git checkout master
$ git pull origin master
$ git merge brad-branch
$ git push origin master
Update your branch when the original branch from official repository has been updated:
$ git fetch [name_of_your_remote]
Then you need to apply to merge changes, if your branch is derivated from develop you need to do:
$ git merge [name_of_your_remote]/develop
Submodules are Git repositories nested inside a parent Git repository at a specific path in the parent repository’s working directory. Submodules allow you to keep projects in separate repositories but still be able to reference them as folders in the working directory of other repositories.
To pull the changes in a submodule, you will run the following command:
$ git submodule update --remote --merge
First, update your local master branch.
$ git checkout master
Fetch the remote, bringing the branches and their commits from the remote repository. You can use the -p, --prune option to delete any remote-tracking references that no longer exist in the remote. Commits to master will be stored in a local branch, remotes/origin/master
$ git fetch -p origin
Merge the changes from origin/master into your local master branch. This brings your master branch in sync with the remote repository, without losing your local changes. If your local branch didn't have any unique commits, Git will instead perform a "fast-forward".
$ git merge origin/master
Checkout the branch you want to merge into.
$ git checkout [feature-branch]
Merge your (now updated) master branch into your feature branch to update it with the latest changes from your team.
$ git merge master
This only updates your local feature branch. To update it on GitHub, push your changes.
$ git push origin [feature-branch]
On your local filesystem:
$ git branch -d [name_of_your_new_branch]
To force the deletion of local branch on your filesystem:
$ git branch -D [name_of_your_new_branch]
Delete the branch on github:
$ git push origin :[name_of_your_new_branch]
To delete a local repository, simply delete the folder on the filesystem.
The only difference is the : to say delete, you can do it too by using github interface to remove branch : https://help.github.com/articles/deleting-unused-branches.
If you want to change default branch, it's so easy with github, in your fork go into Admin and in the drop-down list default branch choose what you want.
If you want create a new branch:
$ git branch [name_of_your_new_branch]
Type Command (⌘)-T in IDE to get any branch changes. Apply their changes.
Fetch from the origin: $ git fetch --all
Reset the HEAD: $ git reset --hard origin/[name_of_branch]
Check the log: $ git log
Re-build the project: $ ./gradlew clean test
Rebasing takes the commits of a branch and appends them to the commits of a different branch. So git’s rebase command reapplies your changes onto another branch. As opposed to merging, which pulls the differences from the other branch into yours, rebasing switches your branch’s base to the other branch’s position and walks through your commits one by one to apply them again.
Git’s rebase command temporarily rewinds the commits on your current branch, pulls in the commits from the other branch and reapplies the rewinded commits back on top. By switching the current, this bases the current branch onto the other branch.
$ git rebase master [other_branch]
or
$ git rebase master
Both above commands lead to the same result. The latter example assumes you are in the other_branch already. After rebasing you are automatically in other_branch branch.
The technical syntax for rebase is $ git rebase [<upstream>] [<branch>]
In case of conflict, git rebase will stop at the first problematic commit and leave conflict markers in the tree. You can use git diff to locate the markers (<<<<<<) and make edits to resolve the conflict. For each file you edit, you need to tell Git that the conflict has been resolved with the following commands:
$ git add <file-name>
$ git rebase --continue
When you’re working on a feature branch and you need changes from the main master branch, it's best to use rebase. Merge can be used when you want to merge a feature branch back into your master branch.
This is performed to affect additional changes to/after a pull request (i.e. after recommendations from other devs)
$ git rebase -i origin/[name_of_branch]~2 [name_of_branch]
- The
-iabove brings up interactive mode. Replacepickwithsin the editor to denotesquash. - The
~2above denotes using the 2nd commit.
To force the push, use the + with the following command
$ git push origin +[name_of_branch]
In this example, there was a conflict with the application.yaml file, which needed to be resolved. Multiple commits on this branch were squashed during the process.
Inside the RIA-1568 branch we performed the following steps:
$ git fetch$ git status-
$ git logThis listed all the commits for this branch (6 in total) $ git rebase -i HEAD~6-
$ git log
-
- copy all the hashes
-
$ git fetch --all
-
-
$ git reset --hard origin/masterSelect Merge (check no commit and squash commits)
-
-
$ git cherry-pick <hash of squashed commit>
$ git status$ git cherry-pick --continue$ git log-
$ git push origin RIA-1568 --force
In this example, there were 22 commits in my PR. We reduced all to a single commit by the following:
-
$ git fetch origin- to get the updated changes from master -
$ git log- so show all the changes - Select and copy the most recent commit hash (6bbb5...) from the git log
-
$ git reset --hard origin/master- throw away all my staged and unstaged changes, forget everything on my current local branch and make it exactly the same as origin/master $ git cherry-pick <most recent commit hash 6bbb5...>$ git push origin <branch name> --force- Run the e2e tests
In this example, there were 29 commits in my PR. We reduced all to a single commit by the following:
-
$ git fetch origin- to get the updated changes from master -
$ git log- so show all the changes - Select and copy the most recent commit hash (6bbb5...) from the git log
-
$ git reset --hard origin/master- throw away all my staged and unstaged changes, forget everything on my current local branch and make it exactly the same as origin/master $ git cherry-pick <most recent commit hash 6bbb5...>$ git push origin <branch name> --force- Run the e2e tests
In this example, we reduce my commits on the same branch (squashing into a single commit):
-
$ git logThis listed all the commits for this branch (6 in total) $ git rebase -i HEAD~6- In interactive mode (FROM THE BOTTOM UP) replace
pickwithf $ git push origin +RIA-437-notify-legal-representative
Inside the RIA-438-notify-home-office branch we performed the following:
$ git fetch --all$ git log- Copy all the hashes
$ git reset --hard origin/master- In IntelliJ, select
VCS -> Git -> Merge Changes(IMPORTANT, be sure to check:No commitandSquash commit~ this denotes rebasing). This will merge 'rebase' the previous branch's changes. $ git commit -m "RIA-437 merge"-
$ git cherry-pick <hash of squashed commit>- this will import your recent changes - Resolve conflicts, in IntellliJ:
VCS -> Git -> Resolve Conflicts... $ git status$ git commit -m "RIA-438 notify home office"$ git log$ git push origin +RIA-438-notify-home-office
Working together on the RIA-436-2087-submit-hearing-requirements branch, we performed the following steps after either of us having made any changes:
Stash your changes:
$ git stash save "my changes"
Fetch the latest updates from the remote repository:
$ git fetch --all
Reset the head to the remote branch:
$ git reset --hard origin/RIA-436-2087-submit-hearing-requirements
Apply the stashed changes:
$ git stash apply or git stash apply stash@{0}
Add the stashed changes:
$ git add .
Commit the changes:
$ git commit -m "my changes"
Squash the latest commit (for cleanliness) in interactive mode:
$ git rebase -i HEAD~3
Force push to the remote repository:
$ git push origin +RIA-436-2087-submit-hearing-requirements
Use these commands to set the (fetch) and (push) urls:
Check the current settings with the following command:
$ git remote -v
Set using the following commands:
$ git remote set-url origin [email protected]:hmcts/rpx-xui-webapp.git
and
$ git remote set-url --push [email protected]:bradsorour/rpx-xui-webapp.git
Create a new repository in GitHub by clicking Repositories -> New and copy the repository URL. Then run the following commands:
$ git init
$ git add .
$ git commit -m "initial commit"
$ git remote add origin <name-of-copied-github-repository-url>
$ git push origin main
If you get an error because origin is not available, you may have followed the incorrect order of Git configuration. You may have already added a 'git origin' to your .git configuration. You can change the remote origin in your Git configuration with the following line:
$ git remote set-url origin <name-of-copied-github-repository-url>
If you want to set main branch (instead of master)
$ git checkout -b main
$ git push origin +main
$ git branch -D master
$ git push origin :master
$ git merge --no-commit --squash origin/static-page
$ git commit -m "Merge static-page"