DEV Community

Cover image for Open Source: Git remotes and merges
MizuhoOkimoto
MizuhoOkimoto

Posted on

Open Source: Git remotes and merges

Hi there!πŸ™‹
Last week I practiced creating issues and pulling requests (my last week's blog). This week I learned more about git and practiced with my partners for the 4th assignment. I added new features to my partner's Static Site Generator(SSG), and I did a Pull Request(PR). Also, my another partner updated my SSG, so I reviewed the changes and merged them.

I divided those contents into Part 1 and Part 2. Part 1 was the procedure for me to post an issue, PR and ask for a merge. Part 2 summarized the procedure after I received the PR.

What's the new features?

I updated my partner's SSG to support config with Config File.
For example, the SSG generates HTML files from text files with $node textToHTML --input <filename/foldername> --stylesheet <stylesheet url or path>. This time, if users enter ssg --c ./ssg-config.json on the command line, the JSON formatted configuration file instead of having to pass them all as command line arguments.

Example: ssg-config.json file

{ "input": "./site",
  "output": "./build",
  "stylesheet": 
  "https://cdn.jsdelivr.net/npm/water.css@2/out/water.css",
  "lang": "fr" }
Enter fullscreen mode Exit fullscreen mode

Inside the site folder, you can store your text files, and they will be generated to HTML file and stored in the build folder.

βœ…Part1: Creating an Issue and a Pull Request

I did a fork on my partner Andre's repo, cloned it to my local file, and updated it based on the requirements like last week. However, this time was a little bit different. I needed to make a Draft Pull Request so I could update the comment when it's ready for review.
image
I implemented the new functionality by adding the following code.

//add a new yarg option
option('c', {
    alias: 'config',
    demandOption: false,
    describe: 'Accept a file path to a JSON config file.',
  }
Enter fullscreen mode Exit fullscreen mode
//add config
if(argv.c){
  const configJson = fs.readFileSync(path.normalize(argv.c));
  const con = JSON.parse(configJson);
  argv.input = con.input;
  argv.stylesheet = con.stylesheet;
  argv.lang = con.lang;
  argv.output = con.output || "./dist";
}else{
  console.log("Error: Could not read config.json file");
  process.exitCode = -1;
} 
Enter fullscreen mode Exit fullscreen mode

After I committed, pushed, and asked for merge, my partner requested me to modify his README based on the features. I modified it and pushed it again.

βœ…Part2: Reviewing and Testing via Remotes

My other partner, Gustavo, added features to my SSG, so I reviewed and merged. I got the name of his forked repo and the name of the branch he was working on.

1. Added a remote to my local repository(repo):
$git remote add Gu https://github.com/GMOTGIT/pajama-ssg.git
I named my local repo "Gu", and the URL is my SSG repo that he forked.

2. Fetched his work into my local repo:
$git fetch Gu
This will download all the commits and branches in the remote repo to my local repo, but not merge anything. Everything he has in his remote repo is now copied to my git repo.

3. Created a "tracking branch" in my local repo:
$git checkout -b issue-19-check Gu/issue-19($git checkout -b <branch-name> <name-of-student>/<branch-name>)
So, I was able to track his work on the branch and switched to a new branch 'issue-19-check' that I created.

Switched to a new branch 'issue-19-check'
D       dist/The Red Headed League.html
D       doc/test.md
M       test.txt
Branch 'issue-19-check' set up to track remote branch 'issue-19' from 'Gu'.
Enter fullscreen mode Exit fullscreen mode

4. Tested new features!
My SSG successfully worked with and without new features.

5. Checked if he worked during my testing:
$git pull Gu issue-19

From https://github.com/GMOTGIT/pajama-ssg
 * branch            issue-19   -> FETCH_HEAD
Already up to date.
Enter fullscreen mode Exit fullscreen mode

6. Final check with $git log if I'm on the HEAD:

PS C:\Users\Mizuho\Desktop\OSD600\pajama-ssg> git log
commit 04a71d577754ed4996ed5cd52182e6b3da6f4434 (HEAD, origin/main, origin/HEAD, Gu/issue-19, main, issue-19-check)
Author: Gustavo Tavares <email>
Date:   Tue Oct 5 22:42:22 2021 -0300
    Updating the README.md
commit 7fc385385c9d6fc9fa7f3991c6bf57c85e0979de
Author: Gustavo Tavares <email>
Date:   Tue Oct 5 21:08:21 2021 -0300
    Adding Config File Feature
Enter fullscreen mode Exit fullscreen mode

7. Ready to merge!
$git checkout main
$git merge Gu/issue-19
$git push origin main

Merging and pushing to the main branch automatically closed the pull request and issue from Gustavo on GitHub!

Problems

Through this lab, I got problems:

  1. In Part1, I was updating the project without creating a new branch, so I had to $git restore ., and create a branch and then paste my updated code to it.

  2. I accidentally created a repo so I couldn't create a tracking branch as the same name. I needed to check what I created with git remote command.

  3. In the Par2 step 3, I created a tracking branch, but I got an error because I didn't commit my previous change.

error: Your local changes to the following files would be overwritten by checkout:
        README.md
Please commit your changes or stash them before you switch branches.
Aborting
Enter fullscreen mode Exit fullscreen mode

Therefore, I had to check the details and commit it first.

PS C:\Users\Mizuho\Desktop\OSD600\pajama-ssg> git status
On branch issue-19-review
Changes not staged for commit
        modified:   README.md
        deleted:    doc/test.md
        modified:   test.txt
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        dist/test.html
        test.md
Enter fullscreen mode Exit fullscreen mode

Conclusion

My partners also used JavaScript for their SSG, which I thought would make it easier to understand, but it took some extra time to understand their code. Luckily, we communicated well so the implementation itself wasn't too hard.
Before I worked on this assignment, I was confused when to use branches and remotes, but I recognize the differences now. I'm getting comfortable using Git, but I forget some things easily, so this blog is a good place to come back to!

(Photo by Shahid Tanweer from Pexels)

Top comments (0)