Table of contents
Git Branching
Let's understand the branch first. Whenever we create a repository default Master branch is created. All the latest codes will be available in the Master/Main branch.
Below are the branches we have in our repository:
Master / Main Branch: Latest code will be available here.
Dev-Branch: The dev branch is created from the Master. The developer will use the Dev branch for ongoing activities.
Feature Branch: The feature branch is created from the Dev branch. The new feature will be developed in a feature branch. Once the feature is completed then it is merged into Dev branch.
Release Branch: Production release will do from the Release branch.
Hotfix Branch: If any issue in Master is detected a hotfix branch is created from Master Branch.
Git Revert and Reset
Whenever we want to undo or revert the changes in the working directory or staging area or local repository we need to use some commands. Let's see one by one.
Revert or Undo changes from Working Directory
When we want to undo the changes from the working directory, we need to use the below command :
git checkout -- <filename>
Revert or Undo changes from Staging Area
When we want to remove the file from the staging area we need to use the below command:
git reset <filename>
Revert or Undo changes from the Local Repository
if you want to remove the commit from the local repository need to use the below command. It will also remove changes from the staging area by default
git reset head~1 ==> It will remove our latest commit.
Revert or Undo changes from the remote Repository
git reset is used before commit and git revert will use after committing changes and push into a remote repository. It will not remove the commit it will revert that commit and will create a new commit.
git revert <commit id>
once git revert is done push the code into the central repository.
Git Merge and Rebase
Git merge and git rebase both are used to merge the changes from one branch to another branch.
Merge will preserve the commit history.
Rebase will not preserve commit history.
Task 1:
Add a text file called version01.txt inside the Devops/Git/ with “This is the 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 the local repo first followed by the Remote repo for review. [Hint use your knowledge of Git push and git pull commands here]
Add a new commit in dev
branch after adding the 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 the development branch
Commit this with the message “ Added feature2 in development branch”
2nd line>> This is gadbad code
Commit this with the message “ Added feature3 in the development branch
3rd line>> This feature will gadbad everything from now.
Commit with the message “ Added feature4 in the development branch
Restore the file to a previous version where the content should be “This is the bug fix in the development branch” [Hint use git revert or reset according to your knowledge]
Let's start with the task:
Go To the terminal and type cd DevOps then cd Git
parim@DESKTOP-F85NG1F MINGW64 ~ $ cd Devops parim@DESKTOP-F85NG1F MINGW64 ~/Devops $ cd Git
Create a dev branch.
parim@DESKTOP-F85NG1F MINGW64 ~/Devops/Git (master) $ git checkout -b dev
Create a new file called "version01.txt" and add a line into it "This is the first feature of our application". Use the vi editor to create files and add content to them.
type vi version01.txt then to enter text press i and enter text. then press ESC wq: and enter the button. the file will get saved. To see the content of a file type cat version01.txt.
Now move a file from the working directory to the staging area and check the status of a file with the git status command.
parim@DESKTOP-F85NG1F MINGW64 ~/Devops/Git (dev) $ git add version01.txt parim@DESKTOP-F85NG1F MINGW64 ~/Devops/Git (dev) $ git status
Commit the changes to the dev branch using the "git commit" command with a commit message of "Added new feature".Use the git log command to check the commit.
parim@DESKTOP-F85NG1F MINGW64 ~/Devops/Git (dev) $ git commit -m "Added new feature" parim@DESKTOP-F85NG1F MINGW64 ~/Devops/Git (dev) $ git status parim@DESKTOP-F85NG1F MINGW64 ~/Devops/Git (dev) $ git log
Now push the changes into a central repository.
parim@DESKTOP-F85NG1F MINGW64 ~/Devops/Git (dev) git push origin dev
To add a new commit in the dev branch after adding the below-mentioned content in Devops/Git/version01.txt:
Open the "version01.txt" file using your preferred text editor and add the following lines:
"This is the bug fix in the development branch"
"This is gadbad code"
"This feature will gadbad everything from now."
parim@DESKTOP-F85NG1F MINGW64 ~/Devops/Git vi version01.txt This is first feature of our application This is the bug fix in development branch This is gadbad code This feature will gadbad everything from now.
Now add code to the staging area with git add. command
Commit the changes to the dev branch using the "git commit" command with a commit message of "Added feature2 in development branch".
parim@DESKTOP-F85NG1F MINGW64 ~/Devops/Git git commit -m "Added new feature"
Push the changes to the remote repository using the "git push" command.
parim@DESKTOP-F85NG1F MINGW64 ~/Devops/Git git push origin dev
To restore the file to a previous version where the content should be "This is the bug fix in the development branch":
Find the commit hash of the commit that contains the content you want to restore by using the "git log" command.
Use the "git revert" command followed by the commit hash to revert the commit and restore the file to its previous version. Alternatively, you could use the "git reset" command to reset the branch to the previous commit and then force-push the changes to the remote repository.
To do this, we would use the following command:
parim@DESKTOP-F85NG1F MINGW64 ~/Devops/Git git revert abc123
This will open up the Git commit message editor, allowing you to modify the commit message for the new revert commit. Once you have saved and closed the commit message editor, Git will create a new commit that undoes the changes made in the original commit.
Here is the output you might see when running this command:
[main 342bfs] Revert "Add feature A" 1 file changed, 1 deletion(-)
Thank You for reading this blog
Parimal Pradhan
You can follow me on LinkedIn for my daily updates:- linkedin.com/in/parimal-pradhan-b62021168
Great initiative by the #trainwithshubham community. Thank you Shubham Londhe