Table of contents
Git Branching
Git branching is a powerful feature of the Git version control system that allows developers to work on multiple versions of a project at the same time. In Git, a branch is simply a separate line of development that diverges from the main codebase, allowing developers to make changes to the project without affecting the main codebase until they are ready to be merged back in.
Developers use branching to work on new features, fix bugs, or experiment with new ideas without affecting the main codebase. Each branch can have its own set of commits, which are the changes made to the codebase, and its own history for maintaining multiple versions of a project.
#Creating a Branch git branch <branch> #List all of the branches in your repository. git branch --list. #Delete the specified branch. git branch -d <branch> #Force delete the specified branch, even if it has unmerged changes. git branch -D <branch> #Rename the current branch to <branch>. git branch -m <branch> #List all remote branches. git branch -a #jump into that branch git checkout <branch> git switch <branch>
Git Revert and Reset
Git Revert is a command in Git that undoes a specific commit and creates a new commit to record the undo action. It's used to undo changes that have already been committed to a Git repository without losing the history of those changes.
Git Reset is a command in Git that allows you to move the current branch to a specific commit, and optionally modify the staging area and working directory as well. It can be used to undo changes and move the repository back to a previous state.
#Discard commits in a private branch or throw away uncommited changes git reset <commit_id> #Undo commits in a public branch git revert <commit_id>
Git Rebase
Git rebase is a command in Git that allows you to modify the history of a branch by moving, combining, or removing commits. It's used to apply changes from one branch to another or to clean up the commit history before merging a branch into another.
git rebase <base> #git rebase with the -i flag begins an interactive rebasing session
Git Merge
Git merge is a command in Git that allows you to combine the changes from one branch into another. It's used to integrate changes from one branch into another, either for collaboration purposes or to incorporate feature changes into the main branch of a project.
git merge <branch>
Task 1
Configuration of Remote repository into the local repo.
mkdir advgit cd advgit git clone <git_repo_URL> cd advance-git
# Adding file in the main branch touch file1.txt vim file1.txt :- "file1 added" git add . git commit -m "file 1 added" git push -u origin main # Creating Devops/git folder mkdir -p Devops/git cd Devops/git
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 the local repo first followed by the Remote repo for review. [Hint use your knowledge of Git push and git pull commands here]
vi version01.txt :- "This is first feature of our app"
git checkout -b dev
git add .
git commit -m "This is the first feature"
git log --oneline
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 dev branch
Commit this with the message "Added feature2 in dev branch"
vi version01.txt :- "This is the bug fix in dev branch"
git add .
git commit -m "Added feature2 in dev branch"
git status
git log --oneline
2nd line>> This is gadbad code
Commit this with the message "Added feature3 in dev branch"
vi version01.txt :- "This is gadbad code" git add . git commit -m "Added feature3 in dev branch" git status git log --oneline
3rd line>> This feature will gadbad everything from now.
Commit with a message " Added feature4 in dev branch"
vi version01.txt :: "This feature will gadbad everything from now" git add . git commit -m "Added feature4 in dev branch" git status git log --oneline
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]
git reset <commit_ID> git status git add . git commit -m "Reset Successful" git log --oneline
Revert the code into the correct one by using git Revert and using vi editor to remove the unwanted code
git revert <commit_ID> vim file_name -> remove the unwanted code and arrow heads vim add . git commit -m "wrong code removed" git push -u origin dev
Merge the dev branch into the main branch and push the code from the local to the remote repo
git push -u origin dev git checkout main git merge dev
Task 2
Demonstrate the concept of branches with 2 or more branches with a screenshot.
git branch git checkout -b dev1 git checkout -b dev2
add some changes to dev branch and merge that branch in master
git checkout dev vi devfile.txt git add . git commit -m "devfile merge with main" git checkout main git merge dev git push -u origin main
Push the remaining 2 new branches into GitHub
git switch dev1 git push -u origin dev1 git switch dev2 git push -u origin dev2
Thank You,
Abhisek Moharana