Git → part 2 Branching , Stashing,confliction etc...in git

What is Branching and Merging in git →

Aakib
6 min readApr 1, 2023

Imagine you have a book that you want to edit. You could make changes directly to the original book, but that would be risky — if you make a mistake or decide you don’t like the changes, you could lose the original version of the book. Instead, you could create a copy of the book and make changes to the copy. This copy is like a branch in Git — it’s a separate version of the book that you can edit without affecting the original.

Now imagine that you want to experiment with different changes to the book. You could create multiple copies of the book, each with its own set of changes. Each copy is like a separate branch in Git — it’s a version of the book that you can edit independently of the others.

Branching and Merging in Git

Once you’re happy with the changes you’ve made in a branch, you can merge the changes back into the main codebase. This is like taking the changes you made in the copy of the book and adding them back into the original. Git provides tools to help you merge branches, resolve conflicts, and keep track of changes to the codebase over time.

commands in branching and Merging→

  1. Create a new branch: To create a new branch, use the command git branch <branch_name>. For example, to create a new branch called "my_feature", you would run git branch my_feature.
  2. Switch to the new branch: To switch to the new branch, use the command git checkout <branch_name>. For example, to switch to the "my_feature" branch, you would run git checkout my_feature.
  3. Make changes to the code: Once you’re on the new branch, you can make changes to the code just like you would on the main branch.
  4. Stage and commit changes: When you’re ready to commit your changes, use the commands git add <filename> to stage the changes, and git commit -m "<commit_message>" to commit the changes. For example, if you changed a file called "index.html", you would run git add index.html to stage the changes, and git commit -m "Updated index.html" to commit the changes.
  5. Switch back to the main branch: To switch back to the main branch, use the command git checkout <main_branch_name>. For example, if your main branch is called "master", you would run git checkout master.
  6. Merge the changes from the new branch: To merge the changes from the new branch into the main branch, use the command git merge <branch_name>. For example, to merge the changes from the "my_feature" branch, you would run git merge my_feature.
  7. Resolve conflicts (if necessary): If there are any conflicts between the code on the new branch and the main branch, Git will prompt you to resolve them. You can use a text editor or a Git tool to resolve the conflicts.
  8. Commit the merge: Once you’ve resolved any conflicts, use the command git commit -m "<commit_message>" to commit the merge. For example, you might use a commit message like "Merged my_feature branch into master".

What is Git Conflict →

In Git, a conflict occurs when two or more people make changes to the same part of a file at the same time, and Git is unable to automatically merge the changes. This can happen when multiple people are working on a project simultaneously and are making changes to the same files.

git conflict

Imagine that you and a friend are working on a project together, and you both make changes to the same line of code in a file. Let’s say you changed the line to say “Hello, world!”, and your friend changed the line to say “Greetings, world!”. When you both try to commit your changes and merge them into the main codebase, Git will detect that there is a conflict.

Git will show you a message that says there was a conflict, and it will highlight the lines of code that were changed. You’ll need to manually resolve the conflict by deciding which version of the code to keep.

To resolve the conflict, you can use a text editor or a Git tool to edit the file and choose which changes to keep. For example, you might decide to keep the line that says “Hello, world!” and delete the line that says “Greetings, world!”. Once you’ve resolved the conflict, you can commit your changes and merge the code into the main codebase.

Conflicts can be frustrating, but they are a normal part of working on collaborative projects with Git. The key is to communicate with your team members and be prepared to resolve conflicts when they occur.

Stashing In Git →

Stashing in Git allows you to temporarily save changes that you’re working on without committing them, so you can switch to a different branch or work on a different feature. It’s like taking a snapshot of your changes and putting them aside for later.

Stashing in git

Let’s say you’re working on a feature branch, and you’ve made some changes to a file but you’re not ready to commit them yet. However, you need to switch to a different branch to work on a different feature. You don’t want to commit your changes because they’re not complete yet, but you don’t want to lose them either.

This is where stashing comes in. You can use the command git stash to stash your changes. Git will take a snapshot of your changes and save them, and then revert your working directory to the last committed state.

You can now switch to a different branch and work on a different feature. When you’re ready to work on the original feature again, you can use the command git stash apply to apply the stash and restore your changes.

If you have multiple stashes, you can use the command git stash listto see a list of your stashes, and git stash apply stash@{n} to apply a specific stash.

Stashing is a useful feature in Git because it allows you to save your changes without committing them, and switch between different branches or features without losing your work. It’s like a “pause” button for your changes, so you can come back to them later when you’re ready.

What is Git Reset →

In Git, reset is a command that allows you to undo changes to your code. It's like rewinding time to a previous state of your code.

going back to orignal one

Let’s say you’ve made some changes to your code and you’ve committed them. Later, you realize that you made a mistake and you want to undo those changes. You can use the git reset command to undo the commit and revert your code back to the state it was in before the commit.

There are several ways to use the reset command in Git, but the most common are --soft, --mixed, and --hard. Here's what each of these options does:

  • --soft: This option undoes the commit but keeps the changes in your code. It's like undoing the commit, but leaving the changes in the staging area so you can make more changes and re-commit them later.
  • --mixed: This option undoes the commit and removes the changes from the staging area, but keeps the changes in your working directory. It's like undoing the commit and un-staging the changes, but leaving the changes in your code so you can make more changes and stage them again.
  • --hard: This option undoes the commit and completely discards all changes since the commit. It's like undoing the commit and deleting all changes that were made after the commit.

When you use the reset command, Git will show you a message that tells you what changes were made. You can use git log to see a list of your commits and their IDs, and then use git reset <commit> to reset your code to a specific commit.

Reset can be a powerful command, but it should be used with caution. It’s important to make sure you understand what changes will be made before using reset, because it can permanently delete changes that you may not be able to recover.

--

--

Aakib

Cloud computing and DevOps Engineer and to be as a fresher I am learning and gaining experiance by doing some hands on projects on DevOps and in AWS OR GCP