CLEAN GIT: Pushing Local Changes to Remote Repository Using Git
Tags: tutorial, programming, git, webdev
Published: 2023-09-08 19:02:09

In this Standard Operating Procedure (SOP), we will walk through the process of applying CLEAN GIT principles to efficiently push our local code changes to a remote repository. This SOP assumes a Git flow with branches such as `staging`, `local/feature`, `UAT`, and `main`. It also covers scenarios where code changes may require adjustments during the review process.

## Git Flow Overview

Our Git workflow involves the following branches:


![Git Flow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ezot5lxctq2wtjf27yjx.png)

1. `local/feature`: This branch is dedicated to feature development.
2. `main`: The main branch serves as the central location for all integrated features.
3. `staging`: QA tests features from the `main` branch.
4. `UAT`: This branch is available for client testing.
5. `PROD`: The production-ready branch.

It's important to note that while the `main` branch may contain untested features, the `staging` branch is reserved for QA-tested features.

## Pushing Local Changes to Remote Repository

Let's delve into the steps of applying CLEAN GIT principles when pushing local code changes to the remote repository.

### Step 1: Create a New Branch

Start by creating a new branch for your feature. In this example, we'll name it `feature/TICKET-1234`.

```bash
git checkout main
git pull
git checkout -b feature/TICKET-1234
```

### Step 2: Develop the Feature

Develop your feature by adding or modifying files. In this instance, we create a new file called `feature.txt` and add content to it.

```bash
echo "This is a feature" > feature.txt
```

### Step 3: Commit the Changes

Commit your changes to the local repository.

```bash
git add feature.txt
git commit
```

This will open your default text editor. You can use `vim` or another text editor to compose your commit message. For example:

```bash
[TICKET-1234]: Add feature.txt

Previously, we did not have a feature.txt file in the repository.

In this commit, we add the feature.txt file to the repository.

Thus, we can now use the feature.txt file.
```

### Step 4: Fetch and Rebase the Latest Changes

Retrieve the latest changes from the remote repository and rebase your changes onto them.

```bash
git fetch --all --tags
git rebase -i --autosquash origin/main
```

This will open your default text editor. For example:

```bash
pick 1a2b3c4 [TICKET-1234]: Add feature.txt
```

### Step 5: Push the Changes to the Remote Repository

Push your changes to the remote repository.

```bash
git push -u origin feature/TICKET-1234
```

### Step 6: Create a Pull Request

Now, create a pull request to merge your changes into the `main` branch.

## Handling Reviewer-Requested Changes

### Major Changes with the Same Technical Decision

If the reviewer requests significant changes but the technical approach remains the same, you can amend your commit after making the necessary adjustments.

```bash
git add feature.txt
git commit --amend
```

Update the commit message to reflect the changes:

```bash
[TICKET-1234]: Add feature.txt

Previously, we did not have a feature.txt file in the repository.

In this commit, we add the feature.txt file to the repository.

Thus, we can now use the feature.txt file.
```

### Major Changes with a Different Technical Decision

In case the reviewer requests substantial changes that involve a different technical approach, create a new commit to incorporate the new decision.

```bash
git add feature.txt
git commit
```

### Minor Changes

For minor adjustments requested by the reviewer, you can amend the existing commit without changing the commit message:

```bash
git add feature.txt
git commit --amend --no-edit
```

## Conclusion

In this SOP, we've outlined the CLEAN GIT principles for pushing local code changes to a remote repository. We've also covered how to handle reviewer-requested changes efficiently. In our next SOP, we'll explore how to apply CLEAN GIT principles to address hotfix requests in a production environment.