Zodiac Signs and Money Mindset · CodeAmber

Mastering Version Control: Git Workflow and Troubleshooting Guide

Mastering Version Control: Git Workflow and Troubleshooting Guide

A technical reference for developers to streamline their version control process and resolve common Git conflicts and state errors.

What is the difference between git fetch and git pull?

Git fetch downloads the latest changes from a remote repository to your local copy but does not merge them into your current working branch. Git pull performs a fetch and immediately attempts to merge those changes into your active branch, updating your local code.

How do I fix a detached HEAD state in Git?

A detached HEAD occurs when you check out a specific commit rather than a branch. To resolve this, switch back to a named branch using 'git checkout [branch-name]' or create a new branch from your current position using 'git checkout -b [new-branch-name]' to preserve any changes.

What is the best way to undo the most recent commit that has not been pushed?

Use 'git reset --soft HEAD~1' to undo the commit while keeping your changes staged in the index. If you want to completely discard the changes and return to the previous commit state, use 'git reset --hard HEAD~1'.

How do I resolve a merge conflict in Git?

Open the conflicted files and locate the markers indicating the start and end of the conflict. Manually edit the code to choose the desired version or combine both changes, remove the markers, and then run 'git add' and 'git commit' to finalize the merge.

What is the purpose of git stash and when should I use it?

Git stash temporarily shelves uncommitted changes, allowing you to switch branches without committing incomplete work. Use 'git stash' to save your current state and 'git stash pop' to reapply those changes once you return to the original branch.

How can I remove a file from the Git index without deleting it from the local file system?

Use the command 'git rm --cached [filename]'. This tells Git to stop tracking the file and removes it from the repository's version control, while the physical file remains on your hard drive.

What is the difference between git merge and git rebase?

Git merge combines two branches by creating a new merge commit, preserving the complete history of both branches. Git rebase moves the entire feature branch to begin on the tip of the main branch, creating a linear project history by rewriting commits.

How do I recover a deleted branch that was not merged?

Use 'git reflog' to find the SHA-1 hash of the last commit on the deleted branch. Once identified, you can recreate the branch at that specific commit using 'git checkout -b [branch-name] [commit-hash]'.

What is a .gitignore file and why is it necessary?

A .gitignore file is a text file that tells Git which files or directories to ignore. It is essential for preventing sensitive data, such as API keys, or bulky system files, like node_modules and build artifacts, from being tracked in the repository.

How do I change the commit message of the most recent commit?

Run 'git commit --amend' to open your default text editor and modify the last commit message. This is only recommended for commits that have not yet been pushed to a shared remote repository.

See also

Original resource: Visit the source site