Repository for learning git and GitHub collaboratively.
There are many ways to contribute to a project and two of those are higlighted below. We encourage participants to practice either one during an event and many open source projects have similar guides to contribute.
The two flows are:
- Contributing directly with a branch
- Contributing through a fork
The way you can contribute and choose one of the above flows depends on your access privileges to a repository. You can use both flows, if you are part of the organization that owns the repository. This is the case when you participate in the hackweek and we added you to the GitHub organizaion. Once you are not part of the organization or if you want to contribute to a repository that is on someone else's personal account, then you need to use a fork.
Before we dive into the workflows, here some essential terms and links to definitions:
An in-depth description for the forking workflow can be found here
Here are the steps:
-
Fork the repository. This can be done using the top right button on github.com on
https://github.com/uwhackweek/learning-git
Choose your user account in the dialog and it will create a fork under your GitHub user account.
-
Go to your user page on github.com, get the URL, and clone the repository locally.
For this, you can use a button on the GitHub user interface to copy the URL.
The below command has
<your-user-name>
as a sample placeholder. This should be replaced by your actual GitHub username.git clone https://github.com/<your-user-name>/learning-git.git
-
Once inside the newly cloned repository, add
UWhackweek
as the original source for your forked repositoryThis is step is considered adding a remote to a repository.
# Change to the directory cd learning-git # Add a new remote with the name 'UWhackweek' as the original fork source git remote add UWhackweek https://github.com/uwhackweek/learning-git.git
-
Verify that the remote was added successfully
List all remotes
git remote -v
The remote with the name
origin
is the default chosen name by git when you cloned the repository. This will always point to the URL of the one used in theclone
command.In our case it is:
UWhackweek https://github.com/uwhackweek/learning-git.git (fetch) UWhackweek https://github.com/uwhackweek/learning-git.git (push) origin https://github.com/<your-user-name>/learning-git.git (fetch) origin https://github.com/<your-user-name>/learning-git.git (push)
-
Make a new branch
# The '-b' is indicating a new branch with the name `new_branch` # It will also change to that. git checkout -b new_branch
Output:
Switched to a new branch 'new_branch'
-
Verify you are on the new branch
# Show all branches of the repository, the current active has the `*` git branch
Output:
main * new_branch
-
Make changes within the new branch
With this practice repository, you have to create a folder within
contributions
folder with your github username as the folder name. Use a new markdwon file (.md
) or text file (.txt
) file to practice git with. No other file types or folders are allowed outside your designated folder with this practice run. -
Commit the changes
# Add the new files git add . # Commit the changes git commit -m "My new files"
-
Push the changes to your forked repository
git push origin new_branch
-
Create a pull request from your new branch
Go to your user page on github.com. There, you will see a banner to create the pull request.
-
Go to the pull request tab on the UW Hackweek website and celebrate!
https://github.com/uwhackweek/learning-git/pulls
By forking the repository, you created a snapshot at that point in time of
the learning-git
repository. To get any changes from the UW Hackweek repository
copied into your version, you need to issue a pull command.
Here the steps:
-
Inside your forked repository
The first command queries for all changes to the
main
branch on UW Hackweek. You added theUWhackweek
remote in step number 3 above.git fetch UWhackweek
Sample output:
remote: Enumerating objects: 1, done. remote: Counting objects: 100% (1/1), done. remote: Total 1 (delta 0), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (1/1), 623 bytes | 623.00 KiB/s, done. From github.com:uwhackweek/learning-git 03a9288..64e856e main -> UWhackweek/main
-
Go back to the
main
branch in your forkgit checkout main
-
Transfer all the changes
This will transfer all the changes since your fork command from the UW Hackweek main branch to your version of the main branch, making it up to date.
git pull UWhackweek main
Sample output:
From github.com:uwhackweek/learning-git * branch main -> FETCH_HEAD Successfully mreged and updated refs/heads/main.
The following explains the steps to contribute directly to a repository using a branch.
In a nutshell, here the flow:
-
Clone the repository on your local computer
‼️ NOTE This is different from the above as you are cloning the original repository and not your fork.Command:
git clone https://github.com/uwhackweek/learning-git
-
Create a branch
Choose a unique name that is not already taken in the repository and helps other collaborators to identify your work. In this case we are adding a branch with name 'feature_xyz'
# The '-b' is indicating a new branch with the name `feature_xyz` # It will also change to that. git co -b feature_xyz
Sample output:
Switched to a new branch 'feature_xyz'
-
Verify you are on the new branch
# Show all branches of the repository, the current active has the `*` git branch
Output:
main * feature_xyz
-
Make changes within the new branch
With this practice repository, you have to create a folder within
contributions
folder with your GitHub username as the folder name. Use a new markdwon file (.md
) or text file (.txt
) file to practice git with. No other file types or folders are allowed outside your designated folder with this practice run. -
Commit the changes
# Add the new files git add . # Commit the changes git commit -m "My new files"
-
Push the changes to your forked repository
git push origin feature_xyz
-
Create a pull request from your new branch
Go to the repository page on github.com. There, you will see a banner to create the pull request.
-
Go to the pull request tab and celebrate!
https://github.com/uwhackweek/learning-git/pulls
-
Clean up and delete the branch locally and on the website.
Once your pull request is merged to the
main
branch, it is a good practice to delete the created branch. This has to be done in two places, once on the pull request page on github.com and the other on your local repository.For github.com, see the official documentation on how to delete a branch after it has been merged. In this practice repository, we have setup a rule that will take care of this step for you.
Locally, however, you must still do this yourself and here the steps you need to execute within your local repository:
# Go back to the `main` branch git checkout main # Get the latest changes that include your pull request git pull # Delete the branch. Here, our practice run branch `feature_xyz` # The -d flag is a safety option that only allows fully merged branches # to be deleted. git branch -d feature_xyz