Pages

Friday, October 23, 2020

Fork-and-Branch Git Workflow(GitHub)

Some companies use GitHub and Fork-and-Branch Git Workflow.
Let's see how it works in detail.

Each developer will have 2 server-side Git repositories: 
  • private(origin) 
  • public(upstream)
The diagram below shows the high level
of the Fork-and-Branch Git Workflow:
























The “fork and branch” workflow:
  • Fork the central GitHub repository.
  • Clone the forked repository to your local system.
  • Add a Git remote (upstream) for the original central repository.
  • Create a feature branch in which to place your changes.
  • Make your changes to the new branch.
  • Commit the changes to the branch.
  • Push the branch to GitHub.
  • Open a pull request from the new branch to the original central repository.
  • Clean up after your pull request is merged.

Forking Company GitHub Repository

The first step is to fork the company's central GitHub repository.
Forking it is basically making a copy of the repository,
but with a link back to the original.
  • Login into GitHub with your account.
  • Chose the central GitHub repository, for example: "CompanyName/RepoName".
  • Click the Fork button on the upper right-hand side of the repository’s page.
  • You should have now "YourName/RepoName" server-side repository.

Clone the forked repository

Navigate to the forked repository and look on the right-hand side of the web page.
You should see a button labeled as "Code"










Copy the URL there, and then use it with git clone like this:
git clone git@github.com:YourNameHere/RepoName.git
Adding the remote (upstream)

Now the server-side forked repository acts as Git remote and named as origin.
To use the “fork and branch” workflow, you’ll need to add a Git remote(upstream)
pointing back to the original central repository.
cd "YourLocalFolderWhereYouClonedTheRepo"
git remote add upstream git@github.com:CompanyName/RepoName.git
Now to test if everything is configured correctly, run the following 
command and you should see the output as below
git remote -v





Creating a Branch

So far, you’ve forked a central repository, cloned it down to your local system,
and added a Git remote(upstream) to your clone that points to the original
central repository on GitHub.

Now to make changes we need to create a Branch from the local repository.
After adding commit(s) to the new created branch,
we will need to push the created branch to GitHub and create a Pull Request
to merge the changes to the central repository.

To create a new branch and check it out, use this command:

git checkout -b <new branch name>
If you are creating a new branch not right after creating a new fork,
you should sync the fork with upstream before creating a new branch from the fork.
git checkout master
git pull upstream master
git push origin master

Commit the changes to the local repo

Once you are done with the changes, use git add .
command to stage all the changes,
it tells Git that you want to include updates to file(s) in the next commit.
git add .
Execute git commit -m "Your Comments" to apply the changes to your local repository.
git commit -m "Your Comments"

Pushing changes to GitHub

Once you’ve committed the changes to your local repository. 
The next step is to push those changes back up to GitHub.

If you were working in a branch called 
TestBranch,
then pushing the changes you made in that branch back to GitHub would look like this:

git push origin TestBranch

Opening a Pull Request

After you push the new branch up to your GitHub server-side repository, 
you need to create a pull request.

This can be done on the GitHub website.

After pushing the branch, browse the GitHub website
and you will see an option to create a pull request.


Merge Pull Request

Once your Pull Request is approved, you need to merge it to the central repository (upstream).

Cleaning up After a Merged Pull Request

First, you should update your local clone by using

git pull upstream master.

git pull upstream master
This pulls the changes from the original central repository’s (indicated by upstream) master branch (indicated by master in that command) to your local cloned repository. Afterward, you can delete the feature branch (because the changes are already in the master branch):
git checkout master
git branch -d <branch name>
Then you can update the master branch in your forked repository:
git push origin master
And push the deletion of the feature branch to your GitHub repository
git push --delete origin <branch name>

Keeping Your Fork in Sync

The forked repository doesn’t automatically stay in sync with the original repository.
To keep your fork in sync with the original repository, use these commands:

git checkout master
git pull upstream master
git push origin master
This pulls the changes from the original repository 
(the one pointed to by the upstream Git remote) and
pushes them to your forked repository (the one pointed to by the origin remote).

1 comment:

  1. Great post.
    Also, I would like to read about pros and cons of Fork-and-Branch workflow

    ReplyDelete