Git Checkout And Merge
Git is an essential tool for version control, allowing you to track changes in your code and collaborate with others. Two important Git commands you’ll use frequently are git checkout and git merge.
Git checkout lets you switch between branches or restore files, while git merge combines changes from different branches into one. Together, they help manage code updates and streamline collaboration.
What is git checkout?
The git checkout command is used to switch between different branches or to restore files in your working directory. It’s like changing paths or opening different versions of your project.
Key Functions of git checkout
- Switching Branches: When you want to move between branches, you use git checkout. For example, if you have a feature branch and want to switch to it from the main branch, you would use this command.
git checkout feature-branch
- Creating a New Branch: If you want to start working on a new branch, you can create and switch to it in a single command using the -b option.
git checkout -b new-feature
This creates a new branch and automatically switches to it.
- Restoring Files: If you’ve made changes to a file and want to undo them, you can restore the file to its state in the last commit.
git checkout -- filename
Why to use git checkout
- Switch Between Branches: git checkout lets you move between different branches to work on various features or versions of your project.
- Restore Files or View Old Commits: It allows you to revert files to a previous state or check out an older commit to review past changes.
Commands of git checkout
1. git checkout
A head pointer is maintained to track the current branch on which the user is working upon , In this code the git checkout command is making the head pointer travel to the main branch

git checkout
2. git checkout -b feature-branch
In the below code the git checkout -b ‘feature-branch’ makes a new branch named feature-branch and then switches to it automatically

git checkout -b feature-branch
3. Check out a specific commit
As soon as the git commit command is written the code is added into the repository made by the user and the current state of the code is added the git checkout ‘hash-code of the branch’ detaches the head from that branch and now new branch can be pointed by the head

Check out a specific commit
4. Restore a specific file to the last committed version
As soon as the git commit command is used the code enters the repository and if the user want that the previous version of the code must enter the repository then git checkout –‘file-name’ is used understand it as a kid name A he was 10 years old back then and after the commit he became 15 years old if i want to restore his old age which was 10 years then this command is used

the command to restore the old version

the restored version of the file
5. Discard all changes in the working directory
In the current working directory there must be some files those are not committed yet or are in the staging area so to restore the old version of all these files and also to stop the process of committing for them the ‘git checkout . ‘ command is used

Discard all changes in the working directory
Git merge
The git merge command is used to combine changes from one branch into another. It’s like taking two paths in your project and merging them into a single path.
You can understand it as a person A who wants to become person B so what he does is he writes a command as git merge and then write person B so now A will inherit all the properties from person B and now A has become B as it has merged with B
Working process of git merge
- Merges changes from one branch into another.
- If both branches have new changes, Git creates a special commit to combine them.
- If no new commits exist on the current branch, Git simply moves the branch pointer forward.
- If the same file is changed in both branches, Git requires manual conflict resolution.
- Unlike rebase, merging keeps the commit history of both branches intact.
Why to use git merge
- To combine changes: Git merge helps bring changes from one branch into another.
- To keep history clear: It keeps all past commits, making it easy to track changes.
Commands of git merge
1. Basic Merge
At the first step git checkout command is used to travel to a branch and then git merge command with the name of the branch whose state has to be adopted by the checkout branch is written then after the current branch updates to the branch into which it is getting merged

Basic Merge
git log –oneline –graph –decorate –all(use this command to check if your branch has merged into the main branch)

merge graph
2. Fast-Forward Merge
If a commit A is made and then a file B is made that contains some content in it as commit A has no changes after the file B is made if we try to merge the A with B then only the head or the branch pointer will move to the file B and no new merge file would be constructed
git checkout A
git merge B

Fast-Forward Merge
type the command git reflog to check if the old merge was a fast forward merge as if it was a fast forward merge the branch pointer must have moved ahead without printing any merge message

Fast-Forward Merge
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

Merge with a commit message
4. Merge while resolving conflicts manually
In this code first i have moved to the main branch using the command git checkout main and then after i have used the command git merge pranjal to merge the pranjal branch into the main branch
git merge branchname
git status

Merge while resolving conflicts manually
5. Merge and squash commits into a single commit
Git squash is like putting your messy toys into one neat box instead of having them all scattered. It takes several little changes and squashes them into one big, clean change!
git merge --squash 'branchname'

Merge and squash commits into a single commit
Conclusion
Understanding git checkout and git merge is essential for efficient branch management in Git. git checkout allows you to switch between branches or restore specific files, while git merge integrates changes from one branch into another. Merging can be done using different strategies, such as fast-forward, recursive, or squash, depending on the workflow needs. By effectively using these commands, developers can manage feature development, resolve conflicts, and maintain a clean project history.