Home Web Front-end JS Tutorial Working with Merge in Git

Working with Merge in Git

Sep 27, 2024 pm 12:25 PM

Working with Merge in Git

In this week blog, I want to share my thought and experience after finishing my lab that is about working with git merge.

Git Merge Strategies

After completing a recent lab focused on working with Git, I gained a deeper understanding of the two primary merge strategies Git uses: Fast-forward and 3-way recursive (recursive-ort) merges.

  • Fast-forward merge: This occurs when the main branch has no new commits since the feature branch was created. In this scenario, Git simply moves the main branch pointer forward to the latest commit from the feature branch. This type of merge does not create a separate merge commit, making it straightforward and linear.

  • 3-way recursive merge: This approach is utilized when both the main branch and the feature branch have diverging commits. Git computes a common ancestor and attempts to merge the changes from both branches. Conflicts may arise if changes have been made to the same lines or files in both branches, requiring manual resolution. Initially, I was under the impression that conflicts would always occur when modifying the same file across different branches. However, conflicts only happen when the exact same line of code is changed in both branches.

Lab Implementation: Feature Branch Merges

For this lab, I worked on adding two features to my repository, VShell, which involved creating separate branches for each feature. These features were designed to improve the tool's functionality by supporting multiple input files/folders and streaming output.

Feature 1: Support Multiple Files and Folders - issue-15:

The first feature involved enabling the tool to process multiple files and folder paths simultaneously. Previously, the tool only handled individual file inputs, but with this enhancement, users can now pass multiple files or directories as arguments. All files within the directories are processed.

To implement this, I extended the existing logic to recursively iterate through folder contents, converting file paths to absolute paths, and storing all relevant files in an array. The relevant snippet:

files.forEach((file) => {
    // convert a file path to an absolute path
const filePath = path.resolve(file);
...
const directoryFiles = fs
          .readdirSync(filePath)
          .map((f) => path.join(filePath, f));
allFiles = allFiles.concat(directoryFiles);
...
const results = allFiles.map((file) => {
    process.stderr.write(`Debug: Processing file: ${file}. \n`);
    return fs.readFileSync(file, "utf-8");
});
return results.join("\n");
}
Copy after login

This code ensures that both individual files and all files within directories are processed accordingly.

Feature 2: Streaming Responses to Stdout - issue-16:

The second feature added streaming support to the tool, enabling real-time output of responses to stdout using the -s/--stream flag. This is a significant improvement over the previous implementation, where responses were only written to an output file or displayed in full once processing was complete.

To achieve this, I introduced asynchronous iteration using the for await...of loop to handle data chunks as they are streamed. Additionally, I tracked token usage in real-time, as token information is only available in the final response chunk. Here is the core logic:

if (options.stream) {
// Handle streaming response
const { response, tokenInfo } = await readStream(chatCompletion);
return { response, tokenInfo };
}
Copy after login
async function readStream(stream) {
  let response = "";
  let tokenInfo;
  for await (const chunk of stream) {
    const content = chunk.choices[0]?.delta?.content;
    if (content) {
      process.stdout.write(content);
      response += content;
    }
    // The last chunk will contain the usage information
    if (chunk?.x_groq?.usage) {
      // Retrieve Token Usage from Response
      const usage = chunk?.x_groq?.usage;
      const promptToken = usage?.prompt_tokens || 0;
      const completionToken = usage?.completion_tokens || 0;
      const totalToken = usage?.total_tokens || 0;
      tokenInfo = { promptToken, completionToken, totalToken };
    }
  }
  return { response, tokenInfo };
}
Copy after login

Notes:

The real-time streaming approach required adjustments in token tracking, as opposed to the simpler method used for non-streamed responses, where usage data can be accessed directly.

For streaming, the token usage object can only be accessed after processing the final chunk in the loop.

// The last chunk will contain the usage information
if (chunk?.x_groq?.usage) {
  // Retrieve Token Usage from Response
  const usage = chunk?.x_groq?.usage;
  ...
}
Copy after login

By default, when using the -s/--stream flag without specifying an output file via -o/--output, the response will be streamed and displayed in the console in real time. However, if the user wants to export the response to a file, they can specify the output file using the -o/--output flag.

Merge Process and Git Merge Strategy

After completing both features, I initiated the merge process, starting by merging Feature 1 into the main branch, followed by Feature 2. Since the features were developed in separate files, no conflicts occurred during the merge process. However, Git used the ORT merge strategy (Ostensibly Recursive's Twin), which is the default starting from Git 2.34. The ORT strategy is a rewrite of the classic recursive merge strategy, offering better performance and accuracy in handling complex merge scenarios.

My final merge commit hash was: 286e23c

The above is the detailed content of Working with Merge in Git. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The Evolution of JavaScript: Current Trends and Future Prospects The Evolution of JavaScript: Current Trends and Future Prospects Apr 10, 2025 am 09:33 AM

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

See all articles