Pushing Local Commits To Github
Pushing Local Commits To Github
Introduction
For this project, you'll need to fork an existing repository, fix a bug in a script, push your
commit to GitHub, and create a pull request with your commit.
What you'll do
Open Github. If you don't already have a Github account, create one by entering a
username, email, and password. If you already have a Github account proceed to the
next step.
Log in to your account from the Github login page.
A fork is a copy of a repository. Forking a repository allows you to freely experiment with
changes without affecting the original project.
Forking a repository is a simple two-step process. We've created a repository for you to
practice with!
Output:
cd ~/it-cert-automation-practice
First, verify that you have already setup a remote for the upstream repository, and an
origin.Type the following command and press Enter. You'll see the current configured
remote repository for your fork.
git remote -v
Output:
In terms of source control, you're "downstream" when you copy (clone, checkout, etc) from a
repository. Information is flowed "downstream" to you.
When you make changes, you usually want to send them back "upstream" so they make it
into that repository so that everyone pulling from the same source is working with all the
same changes. This is mostly a social issue of how everyone can coordinate their work rather
than a technical requirement of source control. You want to get your changes into the main
project so you're not tracking divergent lines of development.
Setting the upstream for a fork you have created using the following command:
To verify the new upstream repository you've specified for your fork, type git remote -v
again. You should see the URL for your fork as origin, and the URL for the original
repository as upstream.
git remote -v
Output:
Configure Git
Git uses a username to associate commits with an identity. It does this by using the git config
command. Set the Git username with the following command:
Replace Name with your name. Any future commits you push to GitHub from the command
line will now be represented by this name. You can even use git config to change the name
associated with your Git commits. This will only affect future commits and won't change the
name used for past commits.
Let's set your email address to associate them with your Git commits.
Replace [email protected] with your email-id. Any future commits you now push to
GitHub will be associated with this email address. You can also use git config to change the
user email associated with your Git commits.
Branches allow you to add new features or test out ideas without putting your main project at
risk. In order to add new changes into the repo directory
it-cert-automation-practice/Course3/Lab4/, create a new branch named improve-
username-behavior in your forked repository using the following command:
git branch improve-username-behavior
cd ~/it-cert-automation-practice/Course3/Lab4
ls
Output:
cat validations.py
Output:
This script should validate usernames if they start with an letter only.
Here, you can check the validate_user function's behavior by calling the function. To edit
the validations.py Python script, open it in a nano editor using the following command:
nano validations.py
Now, add the following lines of code at the end of the script:
print(validate_user("blue.kale", 3)) # True print(validate_user(".blue.kale", 3)) # Currently
True, should be False print(validate_user("red_quinoa", 4)) # True
print(validate_user("_red_quinoa", 4)) # Currently True, should be False
Once you've finished writing this script, save the file by pressing Ctrl-o, the Enter key, and
Ctrl-x.
python3 validations.py
Output:
Here, as we see the output, it function returns true even if the username doesnot start with an
letter. Here we need to change the check of the first character as only letters are allowed in the
first character of the username.
Continue by opening validations.py in the nano editor using the following command:
nano validations.py
There are lots of ways to fix the code; ultimately, you'll want to add additional conditional
checks to validate the first character doesn't start with either of the forbidden characters. You
can choose whichever way you'd like to implement this.
Once you've finished writing this script, save the file by pressing Ctrl-o, the Enter key, and
Ctrl-x.
python3 validations.py
Output:
The git status command shows the different states of the files in your working directory and
staging area, like files that are modified but unstaged and files that are staged but not yet
committed.
You can now see that the validations.py has been modified.
Now, let's add the file to the staging area using the following command:
Use the git add command to add content from the working directory into the staging area for
the next commit.
git status
Output:
Let's now commit the changes. A git commit is like saving your work.
git commit
This now opens up an editor that asks you to type a commit message. Every commit has an
associated commit message, which is a log message from the user describing the changes.
Enter a commit message of your choice and append a line: "Closes: #1" at the beginning to
indicate that you're closing the issue. Adding this keyword has an additional effect when
using Github to manage your repos, which will automatically close the issue for you (for more
information, please see the documentation here).
Closes: #1 Updated validations.py python script. Fixed the behavior of validate_user function
in validations.py.
Once you've entered the commit message, save it by pressing Ctrl-o and the Enter key. To
exit, click Ctrl-x.
Output:
Push changes
You forked a repository and made changes to the fork. Now you can ask that the upstream
repository accept your changes by creating a pull request. Now, let's push the changes.
Output:
Then, from GitHub, create a pull request from your forked repository [git-username]/it-
cert-automation-practice that includes a description of your change. Your branch
improve-username-behavior is now able to merge into the master branch. It should look
like the image below:
After initializing a pull request, you'll see a review page that shows a high-level overview of
the changes between your branch (the compare branch) and the repository's base branch. You
can add a summary of the proposed changes, review the changes made by commits, add
labels, milestones, and assignees, and @mention individual contributors or teams.
Once you've created a pull request, you can push commits from your topic branch to add them
to your existing pull request. These commits will appear in chronological order within your
pull request and the changes will be visible in the "Files changed" tab.
Other contributors can review your proposed changes, add review comments, contribute to the
pull request discussion, and even add commits to the pull request.
You can see information about the branch's current deployment status and past deployment
activity on the "Conversation" tab.
Note: PR won't be merged on the master branch so that other users can also make a similar
change to fix the issue.
Congratulations!
In this lab, you successfully forked a repository, committed changes to your own fork, and
created a pull request to the upstream. Well done!