Showing posts with label git. Show all posts
Showing posts with label git. Show all posts

Tuesday, February 15, 2022

ASSIGNMENT: git revert assignment by Raman Sharma

 assignment by Raman Sharma, JUMP-IBM mentor


1.  Create a file application.properties in your current local git repo.

2. Modify file by adding content "domain=devopsworld.co.in" and commit the changes in to local repo

3. Revert back the changes of application.properties file

4. Confirm the changes are undone.

5. Delete application.properties file without deleting the commits with git revert command.


A1: 

touch application.properties

git add application.properties

git commit -m "application.properties is added"

git log --oneline


A2: 

echo "domain=devopsworld.co.in" >> application.properties

git status

git add application.properties

git commit -m "application.properties is modified"

git log --oneline

>>ee56bad (HEAD -> master) application.properties is modified

>>9608f6f application.properties is added


A3:

git revert ee56bad 

git log --oneline

>>13327ce (HEAD -> master) Revert "application.properties is added"

>>d906372 Revert "application.properties is modified"

>>ee56bad application.properties is modified

>>9608f6f application.properties is added


A4:

cat application.properties


A5:

git revert 9608f6f 

ls

EXAMPLES: git revert examples


git revert command will undo the changes for a particular commit and unlike git reset command it will not delete the commit details.

Prerequisite:- A local repo where you are running the below commands.

Step1 :-> Create a file called packages.json and commit the changes into your local repo.

              touch packages.json

              git add packages.json

              git commit -m "packages.json file is added"

Step2 :-> Modify packages.json file and commit it again

              echo "{ course: devops, batch: freshers }" >> packages.json

              git add packages.json

             git commit -m "packages.json file is modified"

Step3 :-> Verify the git logs

            git log --oneline

     In my case the commits looks like below

753e97d (HEAD -> master) package.json file is modified

0c94fe2 package.json file is added


Step4 :-> Revert back the changes related to modification of the packages.json file.

              git revert 753e97d

Step5 :-> Check git log entry, the commit should not be deleted and there must be a new commit that is related to revert changes. check packages.json file should be empty.

             git log --oneline

             cat packages.json

Below are my git log details.

466a416 (HEAD -> master) Revert "package.json file is modified"

753e97d package.json file is modified

0c94fe2 package.json file is added


Step6 :-> Bring back the changes of packages.json file.

           git revert 466a416

            git log --oneline

           cat packages.json


EXAMPLES: difference between 'merge' and 'rebase'


 

Merge

 Step1:- Initialize a new git repo

              git init

 Step2:- Create 3 files m1 m2 m3 in master branch

             touch m1 m2 m3

             git add -A

            git commit -m "m1" m1

            git commit -m "m2" m2

            git commit -m "m3" m3

Step3:- Create a branch feature1

            git branch feature1

            git checkout feature1

            touch f1

            git add f1

            git commit -m "f1"

Step 4:- Check the feature1 git log details

            git log --oneline

            c399395 (HEAD -> feature1) f1

            267cedc (master) m3

            ead6829 m2

            0ad4148 m1


Step5:- Goto master branch and commit  a new m4 file.

            git checkout master

             touch m4

             git add .

             git commit -m "m4"

Step6:- Got feature1 branch and merge master branch

              git checkout feature1

              git merge master

               git log --oneline

c2a0841 (HEAD -> feature1) Merge branch 'master' into feature1

 m4

 f1

 m3

 m2

 m1


Rebase

Step1:- Initialize a new git repo

              git init

 Step2:- Create 3 files m1 m2 m3 in master branch

             touch m1 m2 m3

             git add -A

            git commit -m "m1" m1

            git commit -m "m2" m2

            git commit -m "m3" m3

Step3:- Create a branch feature1

            git branch feature1

            git checkout feature1

            touch f1

            git add f1

            git commit -m "f1"

Step 4:- Check the feature1 git log details

            git log --oneline

            c399395 (HEAD -> feature1) f1

            267cedc (master) m3

            ead6829 m2

            0ad4148 m1


Step5:- Goto master branch and commit  a new m4 file.

            git checkout master

             touch m4

             git add .

             git commit -m "m4"

Step6:- Got feature1 branch and merge master branch

              git checkout feature1

              git rebase master

               git log --oneline

c2a0841 (HEAD -> feature1) Merge branch 'master' into feature1

 f1

 m4

 m3

 m2

 m1


EXAMPLES: .gitignore examples


touch 1.exe 2.jar 3.exe 4.dmg 5.msi


 vi .gitignore


*.exe

*.jar

* all other extensions that you want to ignore

include also:

.gitignore


exit vi editor


git status

the files that are being ignored are not inside

EXAMPLES: git branches (merge and merging conflicts)

Git branches

   Virtual directories get created for each branch. main/master branch is considered as the branch which has the latest and greatest code. We create branches so each developer can work on its own branch and once the development is finished on a particular stage then it can be reviewed by the code owner and after that, it gets merged into the master branch.

By Default, the master branch gets created when you have your first commit.

Step 1:- List the branches of your local repository. (The branch which has * symbol in front of it is the current branch where we are working).

                git branch

I have only one branch which is master branch and it is my current branch

                *master

Step 2:- List all the commits of your master branch

                 git log --oneline

Step3:- Create a branch for senior developer and name it sdev1.

                git branch sdev1

Step 4:- Create one more branch for junior developer and name it jdev1

                git branch jdev1

Step 5:- List all the branches

                git branch

Step 6:- Switch to sdev1 branch and it should be your current.

                git checkout sdev1

                git branch

Step 7:- List all the files and commits of sdev1 branch. It should have all the commits and files which are present in master branch.

                 ls

                 git log --oneline

Step 8:- Create a new file sdev1.java and commit it in sdev1 branch. It should have a new commit id once commit is done successfully.

                 touch sdev1.java

                 git add sdev1.java

                 git commit -m "sdev1.java file is added in sdev1 branch"

                 git log --oneline  # it should have a new commit

                 ls                       # it show the sdev1.java file

Step 9:- Verify that master branch does not have the commit details which is for sdev1.java file and also sdev1.java file should exist in master branch.

                 git checkout master

                 git log --oneline # it should not show the sdev1.java commit id

                 ls   # sdev1.java file should not be there.

Step 10:- sdev1 wants that his/her code is to be merged into master branch.After merging commit id for sdev1.java addition should be reflected in git log and also sdev1.java file should exist in master branch

                git merge sdev1

                git log --oneline # it should show the sdev1.java commit id 

                ls  # sdev1.java file should be in master branch

Step 11:- Switch to jdev1 branch and commit a new file jdev1.java.

                git checkout jdev1

                touch jdev1.java

                git add jdev1.java

                git commit -m "jdev1.java file is added"

                git log --oneline

Step 12:- Merge jdev1 code into master branch.

               git checkout master

               git merge jdev1

Step 13:- Update sdev1 and jdev1 branch with latest code (master branch)

                git checkout sdev1

                git merge master

                ls

                git checkout jdev1

                git merge master

                ls

Step 14:- jdev is reporting to sdev and all the code changes by jdev is first reviewed by sdev and they merge into master branch and later jdev1 branch get the lastet code from master branch.

          git checkout jdev1

          touch test.java

          git add test.java

          git commit -m "test.java file is added"

                 

# To merge with sdev1 branch

           git checkout sdev1

           git merge jdev1

         

# To merge with master branch

            git checkout master

            git merge sdev1


Now all the branches are in sync.


Merge Conflict

Conflicts generally arise when two people have changed the same lines in a file, or if one developer deleted a file while another developer was modifying it. In these cases, Git cannot automatically determine what is correct. 

Conflicts only affect the developer conducting the merge, the rest of the team is unaware of the conflict. Git will mark the file as being conflicted and halt the merging process. It is then the developers' responsibility to resolve the conflict.

Step 1:- Let's create a new branch jdev2 and he is reporting to sdev1

               git checkout sdev1

               git branch jdev2

Step 2:- sdev1 has asked both junior developers (jdev1 and jdev2) to work on different modules of test.java file and once they finish their work then that code will be reviewed and merge in sdev1 branch and then master branch.

               git checkout jdev1

               echo "This code is added by jdev1" >> test.java

               git add test.java

               git commit -m " test.java file is modified by jdev1"


               git checkout jdev2

               echo "This code is added by jdev2" >> test.java

               git add test.java

               git commit -m " test.java file is modified by jdev2"

Step3:- Merge jdev1 and jdev2 code changes into sdev1 branch if there is any conflict occurs then resolve that conflict.

               git checkout sdev1

               git merge jdev1

               git merge jdev2 # Merge conflict

Step4:- Resolve Merge conflict 

               git mergetool

               update the changes # test.java.orig will be auto-created (make sure to delete)

               cat test.java

               rm test.java.orig

               git commit -m "final test.java file"

Step5:- Update all the branches (jdev1, jdev2 then master) after approval from sdev1

# to merge with jdev1

               git checkout jdev1

               git merge sdev1

               cat test.java


# to merge with jdev2

               git checkout jdev2

               git merge sdev1

               cat test.java

# to merge with master

               git checkout master

               git merge sdev1

               cat test.java

Now all brances are in sync.

EXAMPLES: git tag




Tags are used for defining the version or release of your code.

Prerequisites:- Local git repo should be created and have some files in it.

Branch:- working on the main branch


Step1:-> Switch to the main branch

             git checkout main


Step2:-> Consider the code which is in the main branch is the first to release code, so let's tag it with version v1.0 ( need to define the tag)

             git tag   # show all the tags

             git tag v1.0

             git tag # it will show v1.0


Step3:-> Create some db files(emp.db, company.db) and commit it and these files will be part of the second release, so we need to define one more tag for this release.

           touch emp.db company.db

           git add -A

           git commit -m "db files has been added"

           git tag V2.0

           git tag # show all the tags


Step4:-> Create some dependencies files( junit.dep,sel.dep) into your project and make the part minor release of the second version.

           touch junit.dep sel.dep

           git add -A

           git commit -m "dependencies files has been added"

           git tag V2.1

           git tag


Step5:-> Display all the files which are released in V1.0 ( it should not show db and dep files)

            git checkout V1.0

            ls


Step6:-> Display all the files which are released in V2.0 ( it should show db files along with V1.0 files but should show the dep files)

           git checkout V2.0

           ls


Step7:-> Display all the files which are released in V2.1 ( it should show all the files)

           git checkout V2.1

           ls


Step 8:-> Push a V1.0 tag to remote repo (GitLab/GitHub)

          git push origin V1.0


Step 9:-> Push all the tags to the remote repo

          git push --tags


Step10:-> Check remote Repo (GitLab/GitHub) and you will find the tag files in the tags section and if you want you can download those releases or you can edit the release notes and if these releases are not required on remote repo then you can delete it.


Step11:-> Let's delete the tags in local repo

             git tag -d V2.1   # To delete V2.1 tag.

             git tag -d `git tag | grep -E '.'` # to delete all the tags


Step12:-> delete the version files from the local repo.

            git reset --hard HEAD~2

Thursday, February 10, 2022

GIT Basic Commands

  1. git init
  2. git add <<filename>>
  3. git commit -m "your text here" 
  4. git status 
  5. git log 
    1.    git log --oneline 
  6. git reset HEAD~1/2/3 (Deleting the commits)
    1.     git reset --mixed HEAD~1 
    2.     git reset --soft HEAD~1 
    3.     git reset --hard HEAD~1
  7. git revert <<commit ID>> (not deleting the commits)
  8. git diff <commit ID1>> <<commit ID2>>
  9. git stash
    1. git stash save "stashmessage is stashed"
    2. git stash list
    3. git stash pop

Fluentd

Open-source log data collector > why logs? - for compliance (auditing, company, business) - for security (transparency, monitoring, admin...