Version Control GIT DevOps course by Abdul Rahim
Basic Git Commands DevOps course by Abdul Rahim git --version git init git merge git config --list git add <options> git rebase git config --help git rm/mv <options> git push git config --global git commit <options> git pull git log git diff git checkout git status git reset git fetch git stash git branch git add origin git stash apply
Git Branching Strategy / Workflow DevOps course by Abdul Rahim Build your strategy from these three concepts: – Use feature branches for all new features and bug fixes. – Merge feature branches into the main branch using pull requests. – Keep a high quality, up-to-date main branch.
Git Branching Strategy / Workflow DevOps course by Abdul Rahim
Git Branching DevOps course by Abdul Rahim – Independent/isolated line of development – Inexpensive/lightweight as compared to other VCS – Way to work on different version of a repository – Work in parallel – Master is the default branch – Branches are just pointers to commits – Protected branches
Branching Model DevOps course by Abdul Rahim
Git Branching Commands DevOps course by Abdul Rahim – List Branches: git branch – List all Branches: git branch -a – Create Branch: git branch [branch_name] – Delete Branch: git branch -d [branch_name] – Delete remote Branch: git push origin --delete [branch_name] – Create Branch and Switch: git checkout -b [branch_name] – Switch Branch: git checkout [branch_name]
LAB - Branching DevOps course by Abdul Rahim 1. Create Branch 2. List Branches 3. Switch Branch 4. Commit to new Branch 5. Rename Branch 6. Delete Branch
LAB 1- Create Branch DevOps course by Abdul Rahim – Create a new branch called “test-branch” by using the command: git branch test-branch
LAB 2- List Branch DevOps course by Abdul Rahim – List local branches by using the command: – git branch OR git branch --list – List remote branches by using the command: – git branch -r – List all branches by using the command: – git branch -a
LAB 3- Switch Branch DevOps course by Abdul Rahim – In order to commit to new branch, first you will have to switch to newly created branch, using the command: git branch test-branch – To know about current branch run the command: git status
LAB 4- Commit to New Branch DevOps course by Abdul Rahim – You have already switched to testing branch. Next create a file “test-branch.txt” in new branch and commit by using the command: git add test-branch.txt git commit -m "create test-branch.txt file for test-branch"
LAB 5- Rename Branch DevOps course by Abdul Rahim – Rename the “test-branch” branch to “new-test- branch” using the command: git branch -m test-branch new-test-branch
LAB 6- Delete Branch DevOps course by Abdul Rahim – In order to delete the branch first you will have to switch to another branch i.e. master in this case. git checkout master – Delete the “new-test-branch” branch using the command: git branch -d new-test-branch
Git Merging DevOps course by Abdul Rahim
Git Merging DevOps course by Abdul Rahim – Combine multiple branches into one – Combines multiple sequence into unified history – Commands – Merge branch into active branch: git merge [branch-name] – Merge branch into target branch: git merge [source-branch] [target- branch]
MERGING - FAST FORWARD DevOps course by Abdul Rahim
MERGING – 3 Way DevOps course by Abdul Rahim
MERGING – Resolving Conflict DevOps course by Abdul Rahim
LAB - Merging DevOps course by Abdul Rahim 1. Fast Forward merge 2. Three way (3 Way) Merge 3. Resolving Conflict
Lab 1- Fast Forward Merge DevOps course by Abdul Rahim You will first create a new branch and then add commit to it and then merge new branch to master branch. – Create a new branch and checkout: – git checkout -b ffm-branch – Create a new file, add, and commit to branch: – git add merge-ff.txt – git commit -m “add merge-ff.txt”
Lab 1- Fast Forward Merge DevOps course by Abdul Rahim – Merge “ffm-branch” to master branch: – git checkout master – git merge ffm-branch – Check master logs for fast forward merge: – git log --oneline --graph –decorate – Delete “ffm-branch” branch: – git branch -d ffm-branch
Lab 2- Three Way Merge DevOps course by Abdul Rahim – You will first create a branch, then make a commit to it, then switch to master branch and make a commit to it so that it moves ahead from where the new branch was created, then merge new branch to master. – Create a new branch and checkout: – git checkout -b twm-branch master – Create a new file, add, and commit to branch: – git add merge-tw.txt – git commit -m “add merge-tw.txt”
Lab 2- Three Way Merge DevOps course by Abdul Rahim – Move to master branch – git checkout master – Create a new file, add, and commit to branch: – git add master-merge-tw.txt – git commit -m “add master-merge-tw.txt” – Merge “twm-branch” to master branch: – git merge twm-branch – Check master logs for fast forward merge: – git log --oneline --graph –decorate – Delete “ffm-branch” branch: – git branch -d twm-branch
Lab 3- Resolving Conflict DevOps course by Abdul Rahim You will first create a branch and then edit an already existing file, next you will switch to master branch and edit the same file in a way that both changes conflict with each other. – Create a new branch and checkout: – git checkout -b conflict-branch master – Edit an existing file master-merge-tw.txt on conflict branch and append Hello World: – git add master-merge-tw.txt – git commit -m “modified master-merge-tw.txt”
Lab 3- Resolving Conflict DevOps course by Abdul Rahim – Move to master branch – git checkout master – Edit an existing file master-merge-tw.txt on master branch and append Hello: – git add master-merge-tw.txt – git commit -m “modified master-merge-tw.txt on master branch” – Merge “conflict-branch” to master branch: – git merge conflict
Lab 3- Resolving Conflict DevOps course by Abdul Rahim – You will encounter a merge conflict, run the command: – git status – Open the file with the conflict “master-merge-tw.txt” and resolve the conflict. Once conflicts are resolved add and commit file again – git add master-merge-tw.txt – git commit -m “conflict resolved master-merge-tw.txt” – Delete “conflict-branch” branch: – git branch -d conflict-branch
Rebasing DevOps course by Abdul Rahim – Alternative to integrate changes from one branch to another – Changes the base of your branch from one commit to another – Integrates upstream changes into your branch – Linear history – Command: git rebase
Rebasing DevOps course by Abdul Rahim
Rebasing vs Merging DevOps course by Abdul Rahim – Rebase – Streamlines a potentially complex history. – Avoids merge commit “noise” in busy repos with busy branches. – Cleans intermediate commits by making them a single commit, which can be helpful for DevOps teams. – Merge – Simple and familiar. – Preserves complete history and chronological order. – Maintains the context of the branch.
LAB - REBASING DevOps course by Abdul Rahim – You will first create a new branch “rebase-branch”. – Next add a file “rebase-file.txt” commit to “rebase-branch”. – Switch to master branch. – Now edit an existing file “master-merge-tw.txt” on master branch, – Switch back to new branch and then rebase: git rebase – This will move the base of your branch from earlier commit to current master branch commit.
Stashing DevOps course by Abdul Rahim – Why do we need Stashing? – Saves you uncommitted code for later use – Reverts changes from your working copy – You can make changes, create new commits, switch branches, and – perform any other Git operations and then re-apply your stash when – you're ready – Local to your Git Repo
Stashing Commands DevOps course by Abdul Rahim – List Stashes: git stash list – Stash Changes: git stash – Apply Stash: git stash apply or git stash pop – Clear all Stashes: git stash clear – Stash with description: git stash save "message"
LAB - Stashing DevOps course by Abdul Rahim 1. Stash Changes 2. Apply Stashed changes
LAB 1 – Stash Changes DevOps course by Abdul Rahim – You will create a branch and move into it – git branch stash-example – git checkout stash-example – Now make some changes and check state of working directory – git status – Now stash your changes – git stash
LAB 1 – Stash Changes DevOps course by Abdul Rahim – To see stash changes run the command – git stash list – Check state of working directory – git status – After stashing the changes your working directory will be clean
LAB 2 – Apply Stashed changes DevOps course by Abdul Rahim – You will reapply your stashed changes – Check state of working directory – git status – Reapply your stashed changes: – git stash pop – Check state of working directory – git status – You will see the changes you stashed earlier
Tagging DevOps course by Abdul Rahim – Allows you to give commit a name – Mark important checkpoint in the project – Tags – Annotated: extra metadata e.g. tagger name, email, and date. – Lightweight: only tag name – Branch that doesn’t change – You can checkout to them
Tagging Commands DevOps course by Abdul Rahim – List tags: git tag -l – Lightweight tag: git tag – Annotated tag: git tag -a -m – Tag Details: git show – Delete tag: git tag --delete
LAB - Tagging DevOps course by Abdul Rahim 1. Create Lightweight & Annotated Tag 2. List Tags 3. Tag Old Commit 4. Get Information On a Tag 5. Delete Tags
LAB 1- Create Lightweight & Annotated Tag DevOps course by Abdul Rahim – Create lightweight tag on latest commit: git tag "first-tag“ – Create annotated tag with message on latest commit: git tag -a "second-tag" -m "tag-test"
LAB 2- List Tag DevOps course by Abdul Rahim – List tags using commands: git tag -l – You will see the list of tags i.g. ?rst-tag and second-tag
LAB 3- Tag Old Commit DevOps course by Abdul Rahim – List old commit: git log –n3 – This will show list of last three commits – Copy the hash of second last commit and tag it using the command: git tag "third-tag"
LAB 4- Get Information On a Tag DevOps course by Abdul Rahim – Get information on a lightweight tag: git show first-tag – Get information on an annotated tag: git show second-tag
LAB 5- Delete Tags DevOps course by Abdul Rahim – Delete all the created tags: git tag --delete first-tag git tag --delete second-tag git tag --delete third-tag
Pull Request DevOps course by Abdul Rahim – Method of submitting contribution – Makes collaboration easier – Workflow – Create Branch – Make Changes – Create Pull Request – Merge Once Approved
Lab - Pull Request DevOps course by Abdul Rahim – You will first create a new branch “pull-request-branch”. – Next add a file “pull-request-file.txt” commit to “pull-request- branch”. – Now push changes to repository “pull-request-branch” branch. – You will create a Pull Request from pull-request-branch to master branch. Create a pull request for Github repository page and click on "Pull Request" button in the repo header
Forking DevOps course by Abdul Rahim – Copy of a repository – Server-side copy as compared to clone – Allows to freely experiment without affecting the original project – Use Cases – Propose changes to someone else's project – Contribute to open source projects
Lab - Forking DevOps course by Abdul Rahim – You will fork a repository from GitHub this will create your server-side copy of repository, then you will clone repository, then make changes and push them to your copy of remote repository – On GitHub, navigate to the hello repository. In the top-right corner of the page, click Fork – On GitHub, navigate to your fork copy. Under repository name click code button, and copying the link. Go to the git bash and run clone command. – Create, add, and commit and push a new file. – Now create a pull request by using your branch of forked repo.
Contribute to Open Source Projects DevOps course by Abdul Rahim – Fork the Project – Clone your copy of Project – Create an upstream remote and sync local copy – Do the work – Push to your remote repository – Create a new Pull Request in GitHub – Review by Maintainers – Respond to any code review feedback
Best Practices DevOps course by Abdul Rahim – Don't commit directly to master – Create .gitignore file for your projects – Don't store credentials as code/config in GitHub – Write meaningful commit message – Test Your Code Before You Commit – Use Branches – Always pull the latest updates – Protect your project – The more approvals, the better – Rebase your branches periodically – Agree on workflow
Git PlayGround DevOps course by Abdul Rahim – http://git-school.github.io/visualizing-git/ – https://learngitbranching.js.org/?NODEMO – https://learngitbranching.js.org/
Reference DevOps course by Abdul Rahim – https://docs.microsoft.com/en-us/azure/devops/repos/git/git-branching- guidance?view=azure-devops – https://www.w3docs.com/learn-git/git-rebase.html – https://www.git- tower.com/learn/git/glossary/rebase/#:~:text=In%20Git%2C%20the%20rebase %20command,straight%2C%20linear%20succession%20of%20commits.

Version control Git -Lecture-2

  • 1.
  • 2.
    Basic Git Commands DevOpscourse by Abdul Rahim git --version git init git merge git config --list git add <options> git rebase git config --help git rm/mv <options> git push git config --global git commit <options> git pull git log git diff git checkout git status git reset git fetch git stash git branch git add origin git stash apply
  • 3.
    Git Branching Strategy/ Workflow DevOps course by Abdul Rahim Build your strategy from these three concepts: – Use feature branches for all new features and bug fixes. – Merge feature branches into the main branch using pull requests. – Keep a high quality, up-to-date main branch.
  • 4.
    Git Branching Strategy/ Workflow DevOps course by Abdul Rahim
  • 5.
    Git Branching DevOps courseby Abdul Rahim – Independent/isolated line of development – Inexpensive/lightweight as compared to other VCS – Way to work on different version of a repository – Work in parallel – Master is the default branch – Branches are just pointers to commits – Protected branches
  • 6.
  • 7.
    Git Branching Commands DevOpscourse by Abdul Rahim – List Branches: git branch – List all Branches: git branch -a – Create Branch: git branch [branch_name] – Delete Branch: git branch -d [branch_name] – Delete remote Branch: git push origin --delete [branch_name] – Create Branch and Switch: git checkout -b [branch_name] – Switch Branch: git checkout [branch_name]
  • 8.
    LAB - Branching DevOpscourse by Abdul Rahim 1. Create Branch 2. List Branches 3. Switch Branch 4. Commit to new Branch 5. Rename Branch 6. Delete Branch
  • 9.
    LAB 1- CreateBranch DevOps course by Abdul Rahim – Create a new branch called “test-branch” by using the command: git branch test-branch
  • 10.
    LAB 2- ListBranch DevOps course by Abdul Rahim – List local branches by using the command: – git branch OR git branch --list – List remote branches by using the command: – git branch -r – List all branches by using the command: – git branch -a
  • 11.
    LAB 3- SwitchBranch DevOps course by Abdul Rahim – In order to commit to new branch, first you will have to switch to newly created branch, using the command: git branch test-branch – To know about current branch run the command: git status
  • 12.
    LAB 4- Committo New Branch DevOps course by Abdul Rahim – You have already switched to testing branch. Next create a file “test-branch.txt” in new branch and commit by using the command: git add test-branch.txt git commit -m "create test-branch.txt file for test-branch"
  • 13.
    LAB 5- RenameBranch DevOps course by Abdul Rahim – Rename the “test-branch” branch to “new-test- branch” using the command: git branch -m test-branch new-test-branch
  • 14.
    LAB 6- DeleteBranch DevOps course by Abdul Rahim – In order to delete the branch first you will have to switch to another branch i.e. master in this case. git checkout master – Delete the “new-test-branch” branch using the command: git branch -d new-test-branch
  • 15.
  • 16.
    Git Merging DevOps courseby Abdul Rahim – Combine multiple branches into one – Combines multiple sequence into unified history – Commands – Merge branch into active branch: git merge [branch-name] – Merge branch into target branch: git merge [source-branch] [target- branch]
  • 17.
    MERGING - FAST FORWARD DevOpscourse by Abdul Rahim
  • 18.
    MERGING – 3Way DevOps course by Abdul Rahim
  • 19.
  • 20.
    LAB - Merging DevOpscourse by Abdul Rahim 1. Fast Forward merge 2. Three way (3 Way) Merge 3. Resolving Conflict
  • 21.
    Lab 1- FastForward Merge DevOps course by Abdul Rahim You will first create a new branch and then add commit to it and then merge new branch to master branch. – Create a new branch and checkout: – git checkout -b ffm-branch – Create a new file, add, and commit to branch: – git add merge-ff.txt – git commit -m “add merge-ff.txt”
  • 22.
    Lab 1- FastForward Merge DevOps course by Abdul Rahim – Merge “ffm-branch” to master branch: – git checkout master – git merge ffm-branch – Check master logs for fast forward merge: – git log --oneline --graph –decorate – Delete “ffm-branch” branch: – git branch -d ffm-branch
  • 23.
    Lab 2- ThreeWay Merge DevOps course by Abdul Rahim – You will first create a branch, then make a commit to it, then switch to master branch and make a commit to it so that it moves ahead from where the new branch was created, then merge new branch to master. – Create a new branch and checkout: – git checkout -b twm-branch master – Create a new file, add, and commit to branch: – git add merge-tw.txt – git commit -m “add merge-tw.txt”
  • 24.
    Lab 2- ThreeWay Merge DevOps course by Abdul Rahim – Move to master branch – git checkout master – Create a new file, add, and commit to branch: – git add master-merge-tw.txt – git commit -m “add master-merge-tw.txt” – Merge “twm-branch” to master branch: – git merge twm-branch – Check master logs for fast forward merge: – git log --oneline --graph –decorate – Delete “ffm-branch” branch: – git branch -d twm-branch
  • 25.
    Lab 3- ResolvingConflict DevOps course by Abdul Rahim You will first create a branch and then edit an already existing file, next you will switch to master branch and edit the same file in a way that both changes conflict with each other. – Create a new branch and checkout: – git checkout -b conflict-branch master – Edit an existing file master-merge-tw.txt on conflict branch and append Hello World: – git add master-merge-tw.txt – git commit -m “modified master-merge-tw.txt”
  • 26.
    Lab 3- ResolvingConflict DevOps course by Abdul Rahim – Move to master branch – git checkout master – Edit an existing file master-merge-tw.txt on master branch and append Hello: – git add master-merge-tw.txt – git commit -m “modified master-merge-tw.txt on master branch” – Merge “conflict-branch” to master branch: – git merge conflict
  • 27.
    Lab 3- ResolvingConflict DevOps course by Abdul Rahim – You will encounter a merge conflict, run the command: – git status – Open the file with the conflict “master-merge-tw.txt” and resolve the conflict. Once conflicts are resolved add and commit file again – git add master-merge-tw.txt – git commit -m “conflict resolved master-merge-tw.txt” – Delete “conflict-branch” branch: – git branch -d conflict-branch
  • 28.
    Rebasing DevOps course byAbdul Rahim – Alternative to integrate changes from one branch to another – Changes the base of your branch from one commit to another – Integrates upstream changes into your branch – Linear history – Command: git rebase
  • 29.
  • 30.
    Rebasing vs Merging DevOpscourse by Abdul Rahim – Rebase – Streamlines a potentially complex history. – Avoids merge commit “noise” in busy repos with busy branches. – Cleans intermediate commits by making them a single commit, which can be helpful for DevOps teams. – Merge – Simple and familiar. – Preserves complete history and chronological order. – Maintains the context of the branch.
  • 31.
    LAB - REBASING DevOpscourse by Abdul Rahim – You will first create a new branch “rebase-branch”. – Next add a file “rebase-file.txt” commit to “rebase-branch”. – Switch to master branch. – Now edit an existing file “master-merge-tw.txt” on master branch, – Switch back to new branch and then rebase: git rebase – This will move the base of your branch from earlier commit to current master branch commit.
  • 32.
    Stashing DevOps course byAbdul Rahim – Why do we need Stashing? – Saves you uncommitted code for later use – Reverts changes from your working copy – You can make changes, create new commits, switch branches, and – perform any other Git operations and then re-apply your stash when – you're ready – Local to your Git Repo
  • 33.
    Stashing Commands DevOps courseby Abdul Rahim – List Stashes: git stash list – Stash Changes: git stash – Apply Stash: git stash apply or git stash pop – Clear all Stashes: git stash clear – Stash with description: git stash save "message"
  • 34.
    LAB - Stashing DevOpscourse by Abdul Rahim 1. Stash Changes 2. Apply Stashed changes
  • 35.
    LAB 1 –Stash Changes DevOps course by Abdul Rahim – You will create a branch and move into it – git branch stash-example – git checkout stash-example – Now make some changes and check state of working directory – git status – Now stash your changes – git stash
  • 36.
    LAB 1 –Stash Changes DevOps course by Abdul Rahim – To see stash changes run the command – git stash list – Check state of working directory – git status – After stashing the changes your working directory will be clean
  • 37.
    LAB 2 –Apply Stashed changes DevOps course by Abdul Rahim – You will reapply your stashed changes – Check state of working directory – git status – Reapply your stashed changes: – git stash pop – Check state of working directory – git status – You will see the changes you stashed earlier
  • 38.
    Tagging DevOps course byAbdul Rahim – Allows you to give commit a name – Mark important checkpoint in the project – Tags – Annotated: extra metadata e.g. tagger name, email, and date. – Lightweight: only tag name – Branch that doesn’t change – You can checkout to them
  • 39.
    Tagging Commands DevOps courseby Abdul Rahim – List tags: git tag -l – Lightweight tag: git tag – Annotated tag: git tag -a -m – Tag Details: git show – Delete tag: git tag --delete
  • 40.
    LAB - Tagging DevOpscourse by Abdul Rahim 1. Create Lightweight & Annotated Tag 2. List Tags 3. Tag Old Commit 4. Get Information On a Tag 5. Delete Tags
  • 41.
    LAB 1- CreateLightweight & Annotated Tag DevOps course by Abdul Rahim – Create lightweight tag on latest commit: git tag "first-tag“ – Create annotated tag with message on latest commit: git tag -a "second-tag" -m "tag-test"
  • 42.
    LAB 2- ListTag DevOps course by Abdul Rahim – List tags using commands: git tag -l – You will see the list of tags i.g. ?rst-tag and second-tag
  • 43.
    LAB 3- TagOld Commit DevOps course by Abdul Rahim – List old commit: git log –n3 – This will show list of last three commits – Copy the hash of second last commit and tag it using the command: git tag "third-tag"
  • 44.
    LAB 4- GetInformation On a Tag DevOps course by Abdul Rahim – Get information on a lightweight tag: git show first-tag – Get information on an annotated tag: git show second-tag
  • 45.
    LAB 5- DeleteTags DevOps course by Abdul Rahim – Delete all the created tags: git tag --delete first-tag git tag --delete second-tag git tag --delete third-tag
  • 46.
    Pull Request DevOps courseby Abdul Rahim – Method of submitting contribution – Makes collaboration easier – Workflow – Create Branch – Make Changes – Create Pull Request – Merge Once Approved
  • 47.
    Lab - PullRequest DevOps course by Abdul Rahim – You will first create a new branch “pull-request-branch”. – Next add a file “pull-request-file.txt” commit to “pull-request- branch”. – Now push changes to repository “pull-request-branch” branch. – You will create a Pull Request from pull-request-branch to master branch. Create a pull request for Github repository page and click on "Pull Request" button in the repo header
  • 48.
    Forking DevOps course byAbdul Rahim – Copy of a repository – Server-side copy as compared to clone – Allows to freely experiment without affecting the original project – Use Cases – Propose changes to someone else's project – Contribute to open source projects
  • 49.
    Lab - Forking DevOpscourse by Abdul Rahim – You will fork a repository from GitHub this will create your server-side copy of repository, then you will clone repository, then make changes and push them to your copy of remote repository – On GitHub, navigate to the hello repository. In the top-right corner of the page, click Fork – On GitHub, navigate to your fork copy. Under repository name click code button, and copying the link. Go to the git bash and run clone command. – Create, add, and commit and push a new file. – Now create a pull request by using your branch of forked repo.
  • 50.
    Contribute to OpenSource Projects DevOps course by Abdul Rahim – Fork the Project – Clone your copy of Project – Create an upstream remote and sync local copy – Do the work – Push to your remote repository – Create a new Pull Request in GitHub – Review by Maintainers – Respond to any code review feedback
  • 51.
    Best Practices DevOps courseby Abdul Rahim – Don't commit directly to master – Create .gitignore file for your projects – Don't store credentials as code/config in GitHub – Write meaningful commit message – Test Your Code Before You Commit – Use Branches – Always pull the latest updates – Protect your project – The more approvals, the better – Rebase your branches periodically – Agree on workflow
  • 52.
    Git PlayGround DevOps courseby Abdul Rahim – http://git-school.github.io/visualizing-git/ – https://learngitbranching.js.org/?NODEMO – https://learngitbranching.js.org/
  • 53.
    Reference DevOps course byAbdul Rahim – https://docs.microsoft.com/en-us/azure/devops/repos/git/git-branching- guidance?view=azure-devops – https://www.w3docs.com/learn-git/git-rebase.html – https://www.git- tower.com/learn/git/glossary/rebase/#:~:text=In%20Git%2C%20the%20rebase %20command,straight%2C%20linear%20succession%20of%20commits.