DevOps from Zero #4: Git and GitHub, beyond add, commit and push
I'm Muhammad Khizr Khan , a computer science student and full-stack/DevOps developer, learning DevOps in public one class at a time. This is Class 04. Every CI/CD pipeline, every automatic deploy and every code review starts in the same place: a Git push . So this class goes beyond add , commit and push . Git vs GitHub Git is a program on your computer that saves the history of a folder. It works…
Muhammad Khizr Khan is a computer science student and full-stack/DevOps developer who is learning DevOps through a series of classes. In Class 04, he covers Git and GitHub, beyond the basic add, commit, and push commands.
Git is a program on a computer that saves the history of a folder, working offline. GitHub (or GitLab, Bitbucket) is a website that keeps a shared copy, along with pull requests, issues, and CI/CD. Final_v2_REAL_final.zip is not a version control system, but Git is.
To set up Git, a user needs to configure their name and email, and set the default branch to 'main'. Once the repository is initialized, files can be cloned from an existing repository or created locally. The hidden '.git/' folder contains the repository, and it should never be edited or deleted manually.
The three areas of Git are:
1. The working directory, where edited files live
2. The staging area, where changes chosen for the next commit are stored
3. The repository, where saved snapshots, called commits, reside
The daily loop in Git includes checking the status with 'git status', viewing the changes with 'git diff', staging changes with 'git add', reviewing staged changes with 'git diff --staged', committing changes with 'git commit -m "message"', and viewing commit history with 'git log --oneline --graph --all'.
Git allows for small, separate commits for each idea, with a message explaining the change. It is recommended to use .gitignore to exclude unnecessary files and to handle secrets, such as environment variables or certificates, carefully.
If a sensitive file, like a key or password, is accidentally committed, it's essential to create a new key, revoke the old one, and clean the history. Branches allow for parallel timelines for different features, with 'git switch -c feature/x' creating a new branch and moving to it. To delete a branch after merging, use 'git branch -d feature/x'.
Merging involves integrating changes from one branch into another. To resolve conflicts, open the conflicted file, keep the desired code, delete the marker lines, and commit the changes. To cancel a merge, use 'git merge --abort'.
Rebasing replays a branch on top of another for a linear history, but should only be used on branches not shared with others. Stashing allows users to park unfinished work and return to it later with 'git stash' and 'git stash pop'. Undoing changes can be done with various Git commands, such as 'git restore', 'git commit --amend', 'git reset', or 'git revert'.
Remote repositories can be added using 'git remote add origin', and pushed with 'git push -u origin main'. Fetching downloads only, while pulling downloads and merges changes. Pull requests are used to review and merge changes, with branch protection preventing direct pushes to the main branch.
Trunk-based development and GitFlow are popular Git strategies, with GitFlow being more common in older teams. Semantic versioning, using the format 'major.minor.patch', helps track releases, and git tags can mark specific releases, such as 'v1.4.2'.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to