Open CheatSheet Back To List Next Tutorial

GitHub Exercise (using Git commands)

Intro

  • This practice exercise repeats the actions from both the Beginning and Intermediate GitHub tutorials without the detailed explanations. 
  • It uses only the Git commands from the command line. The next exercise uses the VS Code Source Control GUI.
  • Go through the actions one or more times until you are comfortable with all the concepts and commands.  Use the CheatSheet as reference.
  • To reset the git-demo project to the beginning of the GitHub tutorial enter: 
    • git reset --hard v2-intermediate-git 



From Beginning GitHub tutorial

Add a Readme page to the git-demo project:

Add a Readme file
  • Add a Readme page:

README.md
The purpose of the project is to demonstrate Git and GitHub. It is meant to accompany a series of video tutorials and CheatSheets on those topics.

  • In the Terminal, add it to the staging area and commit it. Then view the commits again:
    • git add README.md
    • git commit -m 'Add readme' 
    • git glog

  • Make sure you are on the main branch, git status is clean, the commits are what you expect, and .gitignore has the files and directories you want to exclude:
    • git branch
    • git status
    • git glog


Create a remote repo
  • Go to your GitHub home page and click the Create new repository button or link.
  • Enter the project name as the GitHub repository name: git-demo
  • Add an optional description: Demo app for learning Git and GitHub
  • Select public or private.
  • Skip the next prompts about adding readme, gitignore and license files.
  • Click the Create Repository button.
  • Copy the lines under …or push an existing repository from the command line

Add the remote and initial push to the project

  • Create a remote called origin, push it, push your tags.
    • git remote add origin https://github.com/your-username/git-demo.git
    • git push -u origin main 
    • git push --tags 

  • Print your remotes: git remote --verbose | git remove -v



Create branch, commit, merge it, push to remote

  • Create and check out a new branch called feature4: 
    • git checkout -b feature4

  • Add your changes and save the files: 
feature4.txt
Some text.

app.txt
This is a demo app to learn Git.
Second line modified.
Third line modified in main branch and modified in feature3 branch.
Feature 1.
Feature 2.
Feature 3.
Feature 4.

  • Add it to the staging area, and commit it:
    • git add .
    • git commit -m 'Add feature 4'
  • Check out the main branch: git checkout main
  • Merge feature4 into main: git merge feature4
  • Then push the new commit to our default remote: git push
  • Delete the feature4 branch: git branch -d feature4

  • Go to Github and refresh the page to see that feature4 has been added.



Add a tag

  • View our commits so far: git glog
  • Add tag to the last commit:
    • git tag -am 'Beginning GitHub' v3-beginning-github 
  • Then push tag:
    • git push origin v3-beginning-github 



Clone a Remote Repository

  • From the GitHub repository click the "Code" button, then copy the HTTPS URL.
  • Git clone command: paste the URL as the first argument, optionally change the repo name in the second argument.
    • git clone https://github.com/username/repository-name.git [new-name] 
  • If the repo's git history is not needed, delete it: 
    • rm -rf .git



From Intermediate GitHub tutorial


Add a software license directly on GitHub using a license template

  • On the Github repository, click Add file button > select Create new file > file name: LICENSE or LICENSE.md in all caps > click Choose a license template > Select a license (e.g., MIT license) > click Review and submit button > click Commit changes button > change commit message: Add MIT license > select Commit directly to the main branch > click Commit changes.

Fetch or Pull from the remote repository

  • Let's get our local repo in sync with the remote, adding the License commit.
  • Do a fetch to check if there are additional commits. Check the status to see how many commits your are behind:
    • git fetch 
    • git status 
  • Optionally, view the change details, comparing the main branch with the remote's main branch:
    • git diff main origin/main
  • Merge the changes from the remote's main branch into our local main branch and log the recent commits:
    • git merge origin/main
    • git olog v3-beginning-github.. 

  • Undo the last commit so we can do it again with the pull command. Log the commits to confirm it is gone:
    • git reset --hard head^ 
    • git olog v3-beginning-github.. 

  • The git pull command will combine fetch and merge into one command. Log the commits to confirm the "Add MIT license" commit is back.
    • git pull
    • git olog v3-beginning-github.. 



Shared Repository Collaboration Model - Example 1

A) Alias remote URL as "origin", and set origin/main as remote-tracking branch. We already did this step.
  • git remote -v Shows the remote alias and the URL it represents.
  • git branch --remotes Shows the remote for our current branch, main. 

B) Create a topic branch, code it, then push it to the central repository:
  • Before working on a topic branch, make sure we are in sync with the remote: 
  • Confirm you are in sync with the remote, then checkout a topic branch called feature5
    • git pull 
    • git checkout -b feature5 

  • Code the topic branch and make commits: 
feature5.txt
Some text.
  • git add feature5.txt
  • git commit -m 'Add feature5.txt'

app.txt
...
Feature 5.
  • git commit -am 'Add feature 5 to app.txt

  • Check out main and confirm it is still in sync with the remote:
    • git checkout main
    • git pull 

  • Checkout feature5 again, log the commits, then push the commits to the remote.
    • git checkout feature5
    • git olog -2
    • git push origin feature5 

C) In the remote repo, create a pull request from the topic branch:
  • Go to the Github repository > click Compare & Pull request button > modify commit message: Add feature 5 > give more info in the comment > click Create pull request button.

D & E) Reviewer(s) review the pull request. Submitter makes any requested changes, the pushes revised commits. In our case we are not requiring a review.

F) Merge the pull request:
  • Click Merge pull request button > select Create a merge commit > Optionally modify the commit message > click Confirm merge
  • Click the Delete Branch button.
  • View the new commits: Go back to the repo main page > click commits link. 

G) Sync your local repo with the remote:
  • In the local repo checkout the main branch and do a git pull, then log the commits.
    • git checkout main
    • git pull 
    • git glog
  • Delete the feature5 branch:
    • git branch -d feature5



Shared Repository Model - Example 2:

A) Our remote is set up with alias origin.
B) In the local repo create a topic branch, code it, push it to the central repository:

  • Make sure you are still in sync with the remote: git pull
  • We are in sync, so create and checkout a new topic branch: git checkout -b feature6
  • Add your code and make commits:
feature6.txt
Some text.
There is a problem on this line.
 
  • git add .
  • git commit -m 'Add feature6.txt'

app.txt
...
Feature 6.

  • git commit -am 'Add feature 6 to app.txt' 

  • GitHub: Simulate a change to the central repository by another collaborator:
    • Go to the GitHub repo > Click Add File button > select Create New File.
    • Name the file about.txt > Content: About this app 
    • Click  Commit Changes button > Change commit message to: Add about page 
    • Select "Commit directly to the main branch" option > Click Commit Changes button.

  • Go back to your local repo. Checkout the main branch to pull any changes since starting our branch:
  • git checkout main
  • git pull 
  • git log -1 will log the new commit.
 
  • Checkout feature6 branch, merge in the main branch.
    • git checkout feature6 
    • git merge main  > Close the MERGE_MSG file to use the default commit message. 
    • git olog -4 Log the last 4 commits.
  • Push the branch to the remote: 
    • git push origin feature6 

C) In remote repo, create a pull request from the topic branch:
  • GitHub: There is a notification of the feature6 branch. Click Compare & Pull request button.
  • Modify pull request name Add feature 6 > add description as a comment.
  • Click Create a pull request button.

D) Reviewer(s) review the pull request: Approve, make comments, or request changes:
  • Simulate a reviewer: On the GitHub repo's navigation bar click the Pull Requests tab > click Add feature 6
  • Click Files changed tab.
  • Review the changes. 
  • Click Review changes button > Enter comment: There is a problem in feature6.txt
  • Select Comment as the type of review > click Submit review button.

E) If the reviewer requests changes, make changes to the local topic branch, then push the commit:
  • After reviewing the reviewer comment, fix the problem on feature6.txt, commit it and push the update:
feature6.txt
Some text.
This problem was corrected.

  • Stage and commit it:
    • git commit -am 'Correct problem in feature6.txt'
  • And push the branch again:
    • git push origin feature6

F) Review the updated Pull request, then Merge it:
  • GitHub: Simulate the reviewer again. Click Pull Requests tab > click the Add Feature 6 pull request.
  • The Files changed tab will show the changes in the topic branch.
  • If everything looks good, an actual reviewer would click Review changes and approve it. 

  • Change Merge pull request button to the squash and merge option then click and confirm it. This will combine the branch commits into one commit.
  • Click the Delete branch button.

G) When the pull request gets merged into the remote main branch, sync the local repo:
  • Checkout the main branch and run git pull to update the local repo. Log the commits.
    • git checkout main
    • git pull 
    • git glog v3-beginning-github.. 
  • Now you can delete the topic branch:
    • git branch -d feature6



Fork and Pull Model

Step A1 (Remote fork) Fork the repo:
  • In your GitHub git-demo repo, click the fork button to create a forked repo.
  • Name the forked repo git-demo-fork, then click the Create fork button.

Step A2 (Local fork): Copy the forked repo URL then clone it on your computer:
  • Click the Code button > The copy the repo's URL. It will be something like:
    • https://github.com/your-username/git-demo-fork.git

  • Open your Terminal application and enter the git clone command using your own username:
    • git clone https://github.com/your-username/git-demo-fork.git 
    • Then open the project in your text editor.

Step A3 (Local fork): Alias original remote URL as "upstream":
  • When you clone the repo, git automatically adds origin as a remote pointing to your forked repo. 
  • Add a second remote called upstream that points to the original source repository:
    • git remote add upstream https://github.com/orig-username/git-demo.git 
  • git remote -v shows your remotes. 
    • Push proposed changes to your fork repo, remote name origin.
    • Pull updates from the source repo, remote name upstream.

Step B (Local fork): Create a local topic branch, code it, and push it to forked repo:
  • Create and check out a new topic branch called feature7. 
    • git checkout -b feature7

  • Add feature7.txt file (with a bug in it), stage and commit it.
feature7.txt
Some text.
This is a bug.

  • git add feature7.txt
  • git commit -m 'Add feature7.txt'

  • Modify the app file, stage and commit it.
app.txt
...
Feature 7.

  • git commit -am 'Add feature 7 to app.txt' 

  • git olog -2 Log the two commits

  • Check out main and do a pull on the upstream repo to make sure it is still in sync.
    • git checkout main
    • git pull upstream main 

  • It is, so switch back to the feature7, and push it to the forked repo:
    • git checkout feature7
    • git push origin feature7 [click Publish Branch: Origin]

Step C (Remote fork): In the remote forked repo, create a pull request from the topic branch:
  • The GitHub git-demo-forked repo has a feature7 notification. Click Compare & pull request button.
  • This creates a Pull Request on the original repo. Modify the Pull request title: Add feature 7 
    • Add a comment with info on the proposed changes: Adds functionality x, y, z.
  • Click Create pull request button.

Step D (Remote source): Source repo reviewer reviews the pull request.
Step E (Local fork): Make changes if the reviewer requests them. Assume no change requests are needed.

Step F (Remote source): If approved, the source repo administrator can Merge or Reject the Pull Request.
  • Assume you are the source repo administrator. Go to the Pull Request page.
    • Change the merge button option to Rebase and merge then click it. 
    • That adds the commits from the topic branch to the main branch, but does not a separate merge commit. 
  • Click the Confirm rebase and merge button
  • Click the Delete branch button.

Step G (Local fork): If reviewer merges your PR in original repo, sync local and remote fork repos:
  • Go back to your local forked repo and checkout the main branch:
    • git checkout main
  • Then do a git pull from the original repo upstream from the main branch:
    • git pull upstream main 
  • But your remote fork repo is still out of sync, so do a push to the "origin" remote:
    • git push origin main 
  • If you are done with the feature7 branch you can delete it:
    • git branch -d feature7



Issues

  • Open an issue for a bug in feature7: Click the Issues tab >  New issue button.
  • Add a title: Feature 7 contains a bug
  • In the message field, provide details:
Put more details here. 
Mention @someUsername and they will be notified.

Tasklist
- [ ] Task1
- [ ] Task2
  • Click Submit new issue button. Note the issue number.

  • Assignees ⚙ setting: Assign yourself to the issue
  • Labels ⚙ setting.  Select the bug label.

  • Add a comment:
The problem seems to be coming from feature7.txt on line 2:
`This is a bug.`
  • Click Comment button.

Fix the issue:
Step A) is already done. We have a fork and cloned it to our computer.
Step B) In your local repo create a topic branch, code it, push the topic branch:

  • In Local git-demo-fork repo main branch, confirm you are still in sync with the remote:
    • git pull upstream main

  • Create and checkout a branch to fix the issue: 
    • git checkout -b patch-feature7

  • Then fix the issue in your code. Change line 2 and save the file.
feature7.txt
Some text.
This bug has been patched.

  • Stage and commit the change with an extended message: Change #4 to your issue number:
git commit -am 'Fix bug in feature7
 
fixes #4 issue' 

  • Switch to the main branch, and make sure it is still in sync.
    • git checkout main
    • git pull upstream main

  • It is still in sync so go back to the topic branch.
    • git checkout patch-feature7

  • Then push the branch to the forked repo.
    • git push origin patch-feature7
    • Or in VS Code, you can go to the Source Control sidebar and click the Publish branch button and select origin as the remote.

Step C) In the remote forked repo, create a pull request from the topic branch:
  • In the git-demo-fork repo next to the push notification, click Compare & pull request button.
  • This takes you to the source git-demo repo. Click Create pull request button.

Steps D & E) Skip the review and review followup steps.

Step F) Merge the pull request:
  • Select the type of merge you want to do: Change the merge button type to Confirm squash and merge and click it.
  • Click the Delete branch button.

Step G) Sync the fork repos:
  • In the local git-demo-fork repo, switch to the main branch:
    • git checkout main 
  • Pull from the upstream remote to get your local fork repo back in sync with the source repo:
    • git pull upstream main 
  • But your remote git-demo-fork repo is still out of sync, so do a push to the "origin" remote:
    • git push origin main 
  • If you don't need the patch-feature7 branch anymore, you can delete it:
    • git branch -d patch-feature7

Open CheatSheet Back To List Next Tutorial