3 minute read

When working on a project with multiple contributors, keeping your code up to date and merging changes from different branches is essential. Git provides useful commands to handle code changes, review differences, merge branches, and manage conflicts. In this post, I’ll walk you through some common Git commands, including merging with a custom commit message and pushing changes to a remote repository.

Key Git Commands Covered:

  1. git diff --name-only latest upstream/master
  2. git diff main:_includes/archive-single.html upstream/master:_includes/archive-single.html
  3. git pull upstream master
  4. git merge --no-ff --no-commit upstream/master --allow-unrelated-histories
  5. git reset HEAD assets/images
  6. git checkout -- assets/images
  7. git commit -m "Merge upstream master with project"
  8. git push origin main

Let’s break down each command and its purpose.

1. View Changed Files:

This command lists the files that have changed between your latest commit and the upstream/master branch.

git diff --name-only latest upstream/master

When to Use:

When you want a quick view of which files have been modified, without seeing the specific changes in each file.


2. Compare Specific Files:

This command compares a specific file (in this case, archive-single.html) between two branches: main and upstream/master.

git diff main:_includes/archive-single.html upstream/master:_includes/archive-single.html

When to Use:

When you only want to see differences in a particular file between two branches, instead of checking all files.


3. Pull Latest Changes:

This command pulls (fetches and integrates) the latest changes from the master branch of the upstream repository into your current branch.

git pull upstream master

When to Use:

Use this to sync your local branch with the most recent updates from the original repository (upstream).


4. Merge Without Fast-Forward:

This merges the upstream/master branch into your current branch. The --no-ff flag ensures that a merge commit is created, and --no-commit lets you review the changes before committing. The --allow-unrelated-histories option is useful if the branches have no shared history (for example, if they were created in different repositories).

git merge --no-ff --no-commit upstream/master --allow-unrelated-histories

When to Use:

Use this when you want to merge changes between branches that don’t have a shared history or when you want to manually commit after reviewing the merge.


5. Unstage Files:

This command unstages the specified files (assets/images), removing them from the staging area but keeping the changes in your working directory.

git reset HEAD assets/images

When to Use:

If you added files to the staging area by mistake and want to remove them from being included in the next commit.


6. Discard Changes in a File:

This command discards any changes made to the assets/images folder, restoring the files to the state they were in during the last commit.

git checkout -- assets/images

When to Use:

Use this when you want to discard local changes to a file and restore it to its last committed state.


7. Commit the Merge with a Message:

Once you’ve reviewed the merge and you’re satisfied with the changes, you can commit the merge with a custom message.

git commit -m "Merge upstream master with project"

When to Use:

Always add a clear commit message, especially for merge commits, so it’s easier to understand what the commit does.


8. Push Changes to the Remote Repository:

Once you’ve merged and committed your changes, push them to the remote repository (origin) to share your updates with others.

git push origin main

When to Use:

Push changes to the remote repository so that everyone on your team has access to the latest code.


Workflow Recap

Here’s a quick overview of how to use these commands in your daily workflow:

  1. Check for modified files: git diff --name-only latest upstream/master
  2. Compare a specific file: git diff main:_includes/...
  3. Pull the latest updates: git pull upstream master
  4. Merge the changes: git merge --no-ff --no-commit upstream/master
  5. Review and commit: git commit -m "Merge upstream master with project"
  6. Push the changes: git push origin main

These Git commands make it easier to manage code changes, merge branches, and keep your repository up to date. By following this process, you can avoid common Git conflicts and keep your workflow smooth.


Conclusion

Git is a powerful tool for managing code changes, especially when working in teams. The commands discussed in this post help you track changes, compare files, merge updates, and push code efficiently. By using Git commands effectively, you can streamline your code management and avoid common issues during merges.

Leave a comment