Version control with git

Concepts

  • Alternative: Apache Subversion (SVN)
  • Repository: where code lives
    • working directory
    • staging area
    • local repository
    • remote repository
  • Git client
    • An app or CLI used to interact with git

Setup and git repo

  • Git repositories
    • GitHub
    • Gitlab
    • Bitbucket
  • Companies have their own git servers

Concept of branches

  • Naming standard
    • feature
      • feature/user-auth
    • bugs
      • bugfix/user-auth-error
  • 1 branch per bug fix or feature
  • Teams mainly have
    • main: ready for production
    • development
  • Best practice in devops
    • have just master branch

Rebase

  • Let's say you want to update a local branch without merge commits
#rebase
git pull -r

#after conflict
git rebase --continue

Gitignore

  • Excludes files from git tracking

remove folder form git cache

git rm -r --cached <file/folder>

git stash

git stash

git stash pop

Going back in history

git log

git checkout hash

git checkout branch - back to head (latest)

Undoing commits

  • Remove last one commit
git log
git reset --hard HEAD~1

Correct a commit

  • Keep your changes but the commit is gone
git reset --soft HEAD~1
git reset HEAD~1

Amend

git commit --amend

Remove commit locally and remotely

git reset --hard HEAD~1
git push --force
  • Don't do this in the main or development branch
  • Do this only when working alone

Alternative: Revert

  • Creates new commit
git revert hash
git push

Merging Branches

  • GIT for DevOps
    • Working with IaaC
      • Many K8S config files
        • Deployment to K8S
      • Terraform and Ansible config files
      • Bash and Python Scripts
    • CI/CD Pipeline and Build Automation
      • Checkout code, test, and build app
      • Need to set up integration with build automation tool and git repo
  • Files should be tracked
  • Securely stored in one place