git branch – Creating and Listing Branches
The git branch command is your main tool for managing branches in your repository. It allows you to create new branches, list existing branches, rename them and delete them. It is essential for organizing your work, experimenting with new features, fixing bugs and collaborating with other developers.
What does git branch do?
- Without arguments: Lists all existing branches in your local repository, indicating which is the current branch with an asterisk (*).
- With an argument (branch name): Creates a new branch with the specified name from the current commit.
- With option
-dor--delete: Deletes the specified branch (only if it has already been merged). - With option
-mo--move: Renames the specified branch.
When to use git branch?
- Create a new branch: When you want to start working on a new feature, bug fix or experiment without affecting the main branch.
- List branches: To see all existing branches in your repository and know which one you are currently working on.
- Delete branches: To clean up your repository and remove branches that are no longer needed.
- Rename branches: To rename a branch if necessary.
How to use git branch:
- List branches:
git branch
- Create a new branch:
git branch <branch-name>
- Delete a branch:
git branch -d <branch-name>
- Rename a branch:
git branch -m <old-branch-name> <new-branch-name>
Examples:
Considerations:
- Current branch: The branch you are currently working on is indicated with an asterisk (*) when listing branches.
- Unsaved changes: Make sure you have committed all changes to your current branch before creating or switching to another branch, to avoid losing work.
- Remote branches: The
git branchcommand only shows local branches. To see remote branches, usegit branch -rorgit branch -a(to see both local and remote branches).
git checkout – Switch branches
The git checkout command is your ticket to travel between the different branches in your repository. It allows you to move from one branch to another, updating the contents of your Working Tree (working directory) to reflect files and changes specific to the selected branch. It’s like changing scenery in a play, where each branch represents a different scene in your project.
What does git checkout do?
- Changes the HEAD pointer to the specified branch.
- Updates the Working Tree to match the contents of the selected branch.
- Prepares the Staging Area for the next changes to be made in the new branch.
When to use git checkout?
- Context switching: When you need to work on a different branch, either to develop a new feature, fix a bug or review another developer’s work.
- Create and switch to a new branch: You can use
git checkout -b <branch-name>to create a new branch and switch to it automatically in one step. - Review previous versions: You can use
git checkout <identifier-commit>to “time travel” and see what your project looked like at a specific commit, although you will not be able to make changes to that state unless you create a new branch from it.
How to use git checkout:
- Change to an existing branch:
git checkout <branch-name>. - Create and switch to a new branch:
git checkout -b <branch-name>Change to a new branch:git checkout -b <branch-name> - Checkout a specific commit (read-only mode):
git checkout <id-identifier-commit>
Examples:
Considerations:
- Unsaved changes: Before changing branches, make sure you have committed or saved your changes to the current branch to avoid losing work. **GIT will warn you if you try to switch branches with unsaved changes.
- Conflicts: If you have unsaved changes in your Working Tree that conflict with files in the branch you want to switch to, GIT will prevent you from switching branches until you resolve the conflicts or discard your changes.
- HEAD detached: When you checkout a specific commit, you enter a state called “HEAD detached”, where you are not on any branch. Any changes you make in this state will not be saved unless you create a new branch from that commit.
git merge – Merge branches
The git merge command is the tool that allows you to merge changes from one branch into another, combining their commit histories. It is like merging two paths that had been separated, creating a new path that incorporates the progress of both.
What does git merge do?
- It merges the changes from the specified branch (the branch you want to merge) into the current branch (the branch you are working on).
- Creates a new merge commit that merges the histories of both branches.
- It automatically resolves conflicts if possible, or prompts you to resolve them manually if necessary.
When to use git merge?
- Feature integration: When you have finished developing a new feature in a separate branch and want to incorporate it into the main branch.
- Bug fixes: When you have fixed a bug in a separate branch and want to apply that bug fix to the main branch.
- Collaboration: When several developers have been working on different branches and want to merge their changes into a single branch.
How to use git merge:
- Switch to the target branch: Use
git checkout <branch-name-target-branch>to move to the branch where you want to integrate the changes. - Run the merge: Type
git merge <branch-name-source-branch>and press Enter. Replace<source-branch-name>with the name of the branch containing the changes you want to integrate.
Example:
Types of Merges:
- Fast-forward merge: Occurs when the branch you want to merge is directly ahead of the current branch. GIT simply moves the current branch pointer forward to include the new commits. This is the simplest type of merge and does not create a new merge commit.
- 3-way merge: Occurs when the two branches you want to merge have diverged, i.e. they have different commits. GIT creates a new merge commit that combines the changes from both branches. This type of merge can generate conflicts if both branches have modified the same lines of code.
Merge conflicts:
- Arise when GIT cannot automatically merge the changes from both branches because they have modified the same lines of code differently.
- GIT will show you the conflicting files and ask you to resolve them manually.
- To resolve a conflict, you must edit the conflicting file, choose which changes to keep, and remove the conflict flags that GIT has added.
- Once the conflicts are resolved, add the files to the Staging Area with git add and commit to complete the merge.
Merge Conflicts Resolution
Merge conflicts are situations where GIT cannot automatically merge changes from two branches because they have modified the same lines of code differently. This can occur when two developers work on the same piece of code at the same time or when one branch has been significantly modified after another branch branched from it.
How to identify a merge conflict?
- When you try to do a
git merge, GIT will show you an error message indicating that there are conflicts. - The
git statuscommand will also show you which files have conflicts. - The conflicting files will have special markings inside them, such as:
<<<<<<< HEAD: Indicates the start of changes to your current branch.=======: Separates the changes in your current branch from the changes in the other branch.>>>>>>> <branch-name>: Indicates the end of the other branch’s changes.
How to resolve a merge conflict?
- Open the conflicting file in a text editor.
- Identify the conflicting sections: Look for the
<<<<<<<,=======and>>>>>>>flags. - Decide which changes to keep: Review the changes in both branches and decide which version of the code you want to keep.
- Edit the file: Remove the conflict flags and modify the code to make it the way you want it.
- Save the file: Save the changes to the file.
- Add the file to the Staging Area: Use
git add <file>to mark the conflict as resolved. - Make a commit: Use
git committo create a new commit that includes the conflict resolution.
Example of a merge conflict:
Possible solutions:
- Keep changes to your current branch:
- Keep changes from the other branch:
- Combine changes:
Tools for resolving conflicts:
- Text editors: Most modern text editors have functions to highlight and navigate between conflicting sections, making them easier to resolve.
- Visual merge tools: There are graphical tools that allow you to visualize the changes in both branches and select which changes to keep in a more intuitive way.
Read the first chapter of this guide here or read the entire series here.





