Working with Upstream Remotes & Branches ๐Ÿด ๐ŸŒด ๐Ÿด - AideenM12/retro-gaming-hackathon GitHub Wiki

โ“ โ” Why this approach..........

Well, personally I think this is a process that can really benefit your knowledge of how Git/Github works in a team environment. You have a primary repository, and you have been given a feature to develop. You don't want to mutate the original repository until you are absolutely certain it won't cause โ˜ข๏ธ nuclear damage!๐Ÿ˜† Developing on a fork, and then subsequently a branch gives you as the developer layers of security....and then with a Pull Request that gets reviewed by your peers before merging into the main repository, you have multiple layers of security. :lock:

๐Ÿง  ๐Ÿค” So, how do we do this? ๐Ÿค” ๐Ÿง  Follow the steps below ๐Ÿ‘‡ :

  • First, you'll need to FORK the main repository onto your own GitHub account.
  • Once you have the repo forked, you can either clone your new repo locally, or work on it directly in Gitpod. (If you do not know how to do this, don't stress ๐Ÿ˜„ post in the channel and we can go through this! ๐Ÿ’ฌ )

Add the upstream remote: (these are terminal commands :wink: :technologist: )

  • git remote add upstream https://github.com/original-owner/repo-name.git
  • Check to ensure the remote added correctly by running git remote -v

And your output should look like this:

$ git remote -v
> origin    https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch)
> origin    https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)
> upstream  https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (fetch)
> upstream  https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (push)

We do this part so that we can have a relationship between your fork, and the main repository so we can do Pull Requests (<---- more on this later).

Work on a BRANCH :deciduous_tree: :

  • Make sure to checkout into an appropriate branch. Never develop onto the master / main branch. This piece here not only give you vital exposure to more GIT :octocat: , but also gives an extra layer of security when working in a team.

You might notice above we have mentioned " master / main ", this is because Github recently changed their naming convention for the primary branch and depending on how you define it, it could be called either ๐Ÿ˜ƒ.

  • git branch -a (view existing branches)
  • git checkout -b new_branch_name (create new branch and move to it) <--- this will create the branch and automatically move you into the new branch!
  • git checkout branch_name (go to an existing branch) <--- do this step if you already have the branch made, notice how the only difference here is the omission of the -b flag ๐Ÿ˜ƒ.
  • While on your branch of choice, DEVELOP! Create the feature, code & experiment! ๐Ÿ‘จโ€๐Ÿ’ป ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿงช ๐Ÿงช
  • Commit and add your changes to the branch in your fork.
  • When ready, go to https://github.com/original-user/repo-name and click on the "Pull Requests" Tab to open a PR, and then you will have an option to select your fork and branch! ๐Ÿ˜„ (there is a little more to this......but I'll get to this in the next post ๐Ÿ˜‰ )

Side note: ๐Ÿ““ If you want to go from one branch to another, you should never directly bounce ๐Ÿ€ from one branch to another. Couple of reasons for this, but firstly, before even considering it, commit your changes to the current branch before moving elsewhere, or you risk losing those changes! Instead, you should get into the habit of traversing back to the main / master branch first, and then going to the branch you want. So in relative order:

  • git add . <--- add all modified files to the staging area
  • git commit -m "meaningful message here" <--- commit them!
  • At this stage, you can push them to the branch using git push or you can navigate to the main/master branch using git checkout main <-- change "main" to "master" to suit your forked repository setup.
  • Then git checkout branch-name to traverse from the main/master branch to the new branch!

Simple right!? ๐Ÿ˜… Don't worry, after a few times doing this, it becomes second nature.


:octocat: - ๐ŸŒด - ๐Ÿด