Git Branching
Git branching allows developers to create separate "branches" within a repository to work on different features, bug fixes, or experiments without affecting the main codebase. This feature is one of the core strengths of Git, enabling collaboration and parallel development.
Git Revert and Reset
1. Git Revert
Purpose:
To undo a specific commit by creating a new commit that reverses the changes introduced by the original commit. This method is safe for collaborative workflows.
How It Works:
It does not alter the history of the repository.
Instead of removing the commit, it creates a new commit that negates the changes.
2. Git Reset
Purpose:
To move the HEAD (and optionally the working directory and staging area) to a specified commit. This method alters history and is generally used in local development environments.
How It Works:
It changes the commit history, making it appear as if specific commits never happened.
The effect depends on the reset mode:
--soft
,--mixed
, or--hard
.
Modes:
Soft:
- Moves the HEAD but keeps changes in the staging area.
Hard:
- Moves the HEAD and discards changes from the staging area and working directory.
What Is Git Rebase?
Git rebase
is a powerful Git command used to integrate changes from one branch into another by transferring commits. Unlike git merge
, which creates a merge commit, git rebase
rewrites the commit history to create a linear sequence of commits.
What Is Git Merge?
Git merge
is a Git command used to integrate changes from one branch into another. It combines the histories of two branches, creating a new commit that represents the result of merging.
Merging is typically used when developers want to incorporate changes from one branch (e.g., a feature branch) into another branch (e.g., the main branch).
Task 1:
Add a text file called version01.txt inside the Devops/Git/ with “This is first feature of our application” written inside. This should be in a branch coming from master
, [hint try git checkout -b dev
], switch to dev
branch ( Make sure your commit message will reflect as "Added new feature"). [Hint use your knowledge of creating branches and Git commit command]
- version01.txt should reflect at local repo first followed by Remote repo for review. [Hint use your knowledge of Git push and git pull commands here]
Add new commit in dev
branch after adding below mentioned content in Devops/Git/version01.txt: While writing the file make sure you write these lines
1st line>> This is the bug fix in development branch
Commit this with message “ Added feature2 in development branch”
2nd line>> This is wrong code
Commit this with message “ Added feature3 in development branch
3rd line>> This feature will impact everything from now.
Commit with message “ Added feature4 in development branch
Restore the file to a previous version where the content should be “This is the bug fix in development branch” [Hint use git revert or reset according to your knowledge]
We have logged into our local machine and we have cloned the remote repo into local. After that we have created a new branch ‘dev’ and switched to it.
After adding a new file into dev branch and making changes to it, we pushed the changes from local machine to remote repo in git hub successfully
After that we used git revert to remove the commits we made earlier. For our case it was throwing an conflict error which we resolved by manually removing the lines from the file which we don’t want
Commands used:
Add remote repo into local machine : git remote add origin “repo url”
Check if remote repo is copied into local machine : git remote -v
How to create and switch to a new branch : git checkout -b branch name. We can also use git switch branch name to switch to new branch we created
Checking if ssh key is working or not : ssh -T git@github.com
Pushing the changes to remote : git push origin branch name
Task 2:
Demonstrate the concept of branches with 2 or more branches with screenshot.
add some changes to
dev
branch and merge that branch inmaster
as a practice try git rebase too, see what difference you get.