![]() |
VOOZH | about |
Git checkout and merge are commands used to switch branches and combine code changes in a repository. They play a key role in managing version control and integrating different development work.
Git checkout is used to switch between branches, create new branches, or restore previous versions of files. This command is essential for moving between different feature developments or viewing past states of your project.
Here are the key functions of git checkout:
Some common commands of git checkout are discussed below:
1. git checkout
A HEAD pointer tracks the current branch or commit. The git checkout command moves the HEAD pointer to the specified branch (e.g., main).
2. git checkout -b feature-branch
The git checkout -b feature-branch command creates a new branch named feature-branch and automatically switches to it.
3. Check out a specific commit
git checkout <commit-hash> switches to a specific commit and puts the repository in a detached HEAD state. In this state, new commits are not linked to any branch unless a new branch is created.
4. Restore a specific file to the last committed version
The git checkout --<file-name> command restores a file to its last committed state, discarding any uncommitted changes.
5. Discard all changes in the working directory
The git checkout . command restores all tracked files in the working directory to their last committed state, discarding uncommitted changes.
Git merge is used to integrate changes from one branch into another. This command combines updates, so work done by different contributors or developed in parallel branches converges cleanly.
Some common commands of git merge are discussed below:
1. Basic Merge
In a basic merge, you first switch to the target branch using git checkout, then run git merge <branch-name> to combine changes from the specified branch into the current branch.
git log --oneline --graph --decorate --all(use this command to check if your branch has merged into the main branch)
2. Fast-Forward Merge
In a fast-forward merge, if the current branch has no new commits, Git simply moves the branch pointer forward to the latest commit of the target branch without creating a new merge commit.
git checkout A
git merge B
Use git reflog to view recent HEAD movements. In a fast-forward merge, the branch pointer moves forward without creating a merge commit, so no merge commit entry appears in the history.
3. Merge with a commit message
Merges with a custom commit message and then use git show HEAD to verify the latest commit details.
git merge -m 'message'
git show HEAD
4. Merge while resolving conflicts manually
First, switch to the target branch using git checkout <branch-name>, then run git merge <other-branch> to merge changes from the specified branch into the current branch, resolving any conflicts manually if they occur.
git merge branchname
git status
5. Merge and squash commits into a single commit
Squash merge combines multiple commits into a single commit, creating a cleaner and more concise commit history.
git merge --squash 'branchname'