[DevOps Bootcamp Notes] - Version Control with Git


[DevOps Bootcamp Notes] - Mastering Version Control with Git: A Comprehensive Guide

Merge Conflicts - When same line was changed, then merge conflicts will occur. Git can't fix it alone, must be solved manually.

Best practice: Push and pull often from remote repo.

This is called "Continuous Integration", integrate your code changes frequently.

git log - To see the history from local repository

Add a remote repository & push code to remote repository
git remote add origin git@gitlab.com:kishore/test-node-app.git (to connect local and remote repositories)

git push --set-upstream origin master (to connect branches)

To remove branch:
rm -rf .git (the folder name)

To Check Current Branch Name:
git branch

Switch Branch:
git checkout <branch name>

Create and Switch to a new branch:
git checkout -b <branch name> (branch is created locally)
Example: git checkout -b feature/database-connection

Push to feature branch:
git push --set-upstream origin feature/database-connection

Delete a local branch post the remote branch is already deleted:

git checkout master
git pull (Because some changes have been made,we have to pull the master branch locally)
git branch -d feature/database-connection

Eliminate Unnecessary "Merge Branch" Commits using "git pull -r"

In case if the local branch doesn't have the changes made to remote branch and vice versa, we can't push the changes to remote branch, in that case we can fix it using any of the below two methods.

git pull (and then commit it)
git push

[OR]

git pull -r (git pull --rebase -> stacks our commit on top and we can avoid others "merge branch" commits so that our "git log" history will look more clean)
git push

Handling Merge Conflicts:
When the same line in the same file is edited by different members of the team, then merge conflicts will occur. During that time if we try to to "git push" it will be rejected. And if we try to "git pull -r" then we will see "Merge Conflict" error.

For better "merge conflict" visilbility we can use IntelliJ -->Git-->Resolve conflicts.

-->Fix the conflict manually, incase both changes are required, apply both changes and "save the changes and finish merging".
-->git status
-->git rebase --continue
-->git push

gitignore file:
To exclude certain folders and files from git to be tracked.

.gitignore
.idea/*
build/*
node_modules/*

Stop tracking a file (OR) Remove a file/directory that is already being tracked by git:

git rm -r --cached .idea
git rm -r --cached node_modules

git status
git add .
git commit -m "Added gitignore file"
git push

Now, idea and node_modules directories will be removed from remote repository. But they still exist in local and they won't be tracked by git anymore as we have already added them to gitignore file.

git stash
-Save inprogress changes(uncommitted) before checkout to another branch

Assume you have some uncommitted changes in one branch and you want to switch to a different branch. When you do "git checkout master", you will get an error there are uncommitted changes in the current branch.So to save changes, we can use "git stash" and then switch to our desired branch. Once we came back to our last branch where we have saved our changes(with git statsh), to get those changes back we can use "git stash pop" command.

git checkout -b master (will get error if we have uncommitted changes)
git stash (will save and hide our changes in "bugfix" branch)
git status
git checkout master (switch to master branch)
git checkout bugfix (branch where our code changes are saved)
git stash pop (To get the changes back)

Going back to history/ previous commit:
git log - to get history
git checkout commit_hash(49fxld948w3kjcjsi39w83e) - detachecd HEAD message is expected.

We have create a new branch with the reverted state data, git checkout -b reverted_code

git checkout bugfix - To switch to the branch "bugfix" with up to date information.

Undoing Commits:
Scenario1: We made changes to local repository files and remote repository doesn't know about it.

git add .
git commit -m "remove some log lines"
git status

To revert the change/commit that happened in the local respository:

git log (to get last commit hash)
git reset --hard HEAD~1 (hard will revert the last commit and also discards the changes as well in the files)

To revert the commit and correct the changes:

change a file
git add .
git commit -m "removed some log lines again"

git log (to get last commit hash)

git reset HEAD~1 (this is same as "git reset --soft HEAD~1") with this we still have changes in the files for correction.

To update last commit:
Instead of doing new commit we can ammend the changes to the last commit.

change a file by adding/removing lines

git add .
git commit --amend
git status
git log (to see our changes are merged into our last commit)
git push

Revert a commit(that broke something) from a remote repository:

git reset --hard HEAD~1 (to revert the commit and discard all the changes)
git log (to check the commits history)

git push --force (this will remove the commit from remote respository, force is needed as without force we can't do that). It will over wrote the commit with a new commit. DON'T do this in master and develop branch. Do it on the branch ONLY when you are working alone in that branch. Otherwise other folks can't merge their changes to the specific branch untill they fix it with some workaround.

Undo/Reset commit on master/development branch:git revert

make few changes to files
git add .
git commit -m "tesst"
git push

git log (to get specific commit hash)
git revert commit_hash(949vdre38fkdj4j598s)
git push (Creates a new commit to revert the old commit's changes)

git merge:merging branches
When there are few commits missing in a feature branch from master branch (or) vice versa, we usually merge the branches.

Example: We want to merge the changes from master branch to "bugfix" branch.

Steps:
git checkout master
git pull (to make sure master branch is upto date)
git checkout bugfix/user-auth-error (switch to bugfix branch)
git merge (source branch)

Ex: git merge master
git log (to see commits history)
git push (to push the merge into remote bugfix branch)

Push an existing repository from the command line

git remote add origin git@github.com:kishoreuppala/<repo-name>.git

git branch -M main

git push -u origin main




──────── Credits to: TechWorldwithNana & BestTechReads ────────

DISCLAIMER

The purpose of sharing the content on this website is to Educate. The author/owner of the content does not warrant that the information provided on this website is fully complete and shall not be responsible for any errors or omissions. The author/owner shall have neither liability nor responsibility to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the contents of this website. So, use the content of this website at your own risk.

This content has been shared under Educational And Non-Profit Purposes Only. No Copyright Infringement Intended, All Rights Reserved to the Actual Owner.

For Copyright Content Removal Please Contact us by Email at besttechreads[at]gmail.com


Post a Comment

Previous Post Next Post