10.1 Version Control with Git: Repository Operations, Branching, Merging & Conflict Resolution
Key Takeaways
Inspect active Git configuration and ignore rules in the context of the repository.
Use git diff for the working tree against the index and git diff --staged for the index against HEAD.
Choose a branch integration method by ancestry and policy. A squash merge prepares a single-parent commit; review a new merge or rebase result before publishing.
Fetch observes remote state; pull integrates it. Check the destination before publishing a branch or tag.
Distinguish path restoration, reference reset and a new revert commit. Review a stash before reapplying it.
Version Control with Git
Git commits record project snapshots and their parent relationships. A branch is a movable reference to a commit, and HEAD commonly identifies the current branch. With detached HEAD, it identifies a commit directly. A regular clone provides local history and permits many operations offline. A shallow clone limits history, and partial clones can fetch omitted objects when needed. A local clone still needs a backup plan for working changes and refs that exist only on one machine.
Git stores objects such as blobs, trees, commits and annotated tags using object IDs. Git repositories can use SHA-1 or, with appropriate tool support, SHA-256. Object IDs identify content. Use verified signatures and a trusted distribution path when you need to establish provenance.
Start a repository and inspect its settings
Use git init --initial-branch=main to initialize an empty lab directory. Use git clone URL lab-infra to create a local repository from an approved source in a new directory. A typical clone sets up origin and remote-tracking references according to its fetch configuration. Inspect the actual remotes and branches.
Use git config --local user.name "Lab Admin" and git config --local user.email lab@example.test to set a lab commit identity. Git can read configuration from system, global and repository scopes. A worktree scope can apply when configured, and command settings can override particular values. Includes and multi-valued keys can affect the result. Inspect the active values and origins with git config --list --show-origin --show-scope. Commit identity is separate from authentication for a remote and from commit signatures.
Patterns in .gitignore apply to untracked paths. For example, *.log can ignore generated logs, and /.env scopes the pattern to the top-level file in the directory containing the ignore rules. git check-ignore -v -- path identifies a matching rule for an ignored path. Adding a pattern does not change files already tracked in the index. In an authorized change, git rm --cached -- path stages removal while keeping the working file. Existing commits still contain earlier copies. Follow incident procedure if sensitive data reached history.
Compare the working tree, index and commit
| Location | Role | Inspect it |
|---|---|---|
| Working tree | Files currently on disk | git status --short and git diff |
| Index | Proposed contents of an ordinary next commit | git diff --staged |
| Local commit history | Committed project snapshots and metadata | git log --oneline --graph --decorate --all and git show COMMIT |
| Remote repository | Another repository with its own refs and objects | git remote -v shows configured URLs; remote-tracking refs summarize observations from fetches or accepted updates. |
A file can have staged and unstaged edits at once. For example, change app.conf from port=80 to port=8080, then stage it with git add -- app.conf. Edit the file on disk again to port=8443. git diff --staged -- app.conf compares HEAD with the index and shows 80 to 8080. git diff -- app.conf compares the index with the working file and shows 8080 to 8443. git diff HEAD -- app.conf compares the working file against HEAD. git commit -m "Set application port" records the staged snapshot. The later edit remains in the working tree and can be reviewed separately. An ordinary commit normally records the entire index, including other staged paths.
Use git add -- PATH for an intentional scope and examine the staged diff before committing. git add -A without a pathspec stages additions, updates and removals across the worktree, subject to ignore rules for untracked paths. git commit -a stages modifications and removals of tracked files and excludes new untracked files. git log -n 5 reads recent history. git show can inspect an object by name, such as displaying an ordinary commit’s metadata and patch.
Branches and integration
git branch lists local branches and git branch lab-audit creates a branch from the current commit. git checkout lab-audit or git switch lab-audit changes to an existing branch, subject to conflict and local-change checks. git checkout -b lab-audit creates and checks out a new branch. Use explicit path separators to distinguish a path restore from branch checkout.
Merge into the currently checked-out destination. If its tip is an ancestor of the source tip, a default merge can fast-forward by moving the reference. git merge --ff-only feature-audit rejects a non-fast-forward integration. With divergent histories, an ordinary successful merge integrates changes and records a merge commit. --no-ff requests a merge commit when a fast-forward would otherwise happen. git merge --squash feature-audit prepares the combined result for a separate commit, with no merge parent recorded in that commit.
An interactive rebase can reword or combine commits on an unpublished branch. A simple git rebase main replays selected commits onto the new base, commonly producing new IDs. Options affect commit selection and preservation of merges. Coordinate any intentional rewrite of shared history, and prefer history-preserving integration on protected shared branches.
When an integration stops on a conflict, inspect git status and the actual operation. Text conflicts can show marker lines such as <<<<<<<, ======= and >>>>>>>. Other conflicts can lack text markers. Reconcile the intended content and stage it with git add -- PATH. Use the applicable continuation, such as git merge --continue or git rebase --continue, after resolving the affected paths. To cancel, use the operation’s abort command. Capture existing edits before beginning an integration; merge abort may be unable to reconstruct pre-existing uncommitted changes.
Exchange changes and label releases
git fetch origin downloads selected objects and updates references according to the fetch refspec. In a typical clone, it updates references such as origin/main without integrating them into the checked-out branch. Then use git log HEAD..origin/main or git diff HEAD origin/main to inspect what changed. git pull fetches and then integrates into the current branch. The strategy depends on options and configuration. git pull --ff-only origin main requires a fast-forward when a merge is needed, and git pull --rebase origin main selects rebase.
git push -u origin lab-audit requests an update to the matching remote branch and sets tracking information for a branch accepted or already up to date. Server policy and permission apply. Inspect the remote and branch before publishing. Defaults for future pull and push depend on configuration.
git tag lab-v1 makes a lightweight tag at the current commit. git tag -a lab-v1 -m "Lab release 1" makes an annotated tag with its own object and message; choose one form for the same tag name. Listing with git tag --list and inspecting with git show lab-v1 work locally. A tag can be published explicitly with git push origin refs/tags/lab-v1 when allowed. Published tags should remain stable under the project policy.
Restore, reset, revert and stash
| Goal | Command and effect |
|---|---|
| Discard an unstaged edit to a tracked file | git restore -- app.conf replaces the working copy from the index. Inspect the diff first. |
| Unstage without changing working content | git restore --staged -- app.conf restores the index copy from HEAD by default. |
| Move a local unpublished branch while retaining files | git reset --soft COMMIT updates the current reference or detached HEAD and leaves the index and working tree alone. --mixed also writes the target to the index. |
| Make tracked files match a target commit | git reset --hard COMMIT resets the reference, index and tracked working state. It can lose local changes and remove untracked paths that obstruct writing tracked paths. |
| Undo a committed change on a shared branch | git revert COMMIT attempts to reverse the selected change in the current history and records a new commit on success under its usual mode. |
| Shelve local edits | git stash push -m "lab work" records tracked staged and unstaged changes. Add -u to include untracked files. Review the actual saved entry with git stash list. |
For a faulty ordinary commit already shared, review the selected commit and later changes before reverting. A later edit of the same content can conflict with the reversal. Resolve it and continue the revert, or use git revert --abort. The selected original commit remains in history. Reverting a merge has a separate mainline-parent decision. Review the resulting diff and test before publishing.
git stash apply 'stash@{0}' applies the changes and keeps the entry. git stash pop usually drops the entry after a successful application, and keeps it if conflicts occur. --index attempts to restore the staged state as well. Preview the stash and preserve it until the recovered changes are verified.
- Git clone, configuration, ignore rules, and diff
- Git branch, checkout, merge, and rebase
- Git fetch, pull, push, and tag
- Git restore, reset, revert, and stash
A Git repository already has a commit. You edited and staged app.conf, then edited its working copy again. Which command shows the index-to-HEAD changes for app.conf and excludes the unstaged edit?
git diff -- app.conf
git diff --staged -- app.conf
git diff HEAD -- app.conf
git show HEAD
An ordinary, non-merge commit introduced a faulty configuration on a shared branch. The working tree is clean and policy requires the published history to be preserved. Which operation attempts to record an undo as a new commit?
git reset --hard HEAD~1
git restore --source=HEAD~1 -- app.conf
git branch -D main
git revert COMMIT
A Git merge is paused with an unmerged auth.conf. You resolved the file on disk and want to stage only this resolution before continuing the merge. Which command does that?
git add -- auth.conf
git merge --continue
git diff --staged -- auth.conf
git checkout --ours -- auth.conf
Sections you finish are checked off in the contents.