Managing Code Changes and Merging Branches with Git
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:
git diff --name-only latest upstream/master
git diff main:_includes/archive-single.html upstream/master:_includes/archive-single.html
git pull upstream master
git merge --no-ff --no-commit upstream/master --allow-unrelated-histories
git reset HEAD assets/images
git checkout -- assets/images
git commit -m "Merge upstream master with project"
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:
- Check for modified files:
git diff --name-only latest upstream/master
- Compare a specific file:
git diff main:_includes/...
- Pull the latest updates:
git pull upstream master
- Merge the changes:
git merge --no-ff --no-commit upstream/master
- Review and commit:
git commit -m "Merge upstream master with project"
- 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