Understanding Git Commands and Workflow
(DevOps Lab)
1. Introduction to Version Control
Version control systems help developers track and manage changes in their code over time. Git is a
distributed version control system widely used in software development. It allows multiple
developers to collaborate on the same project while keeping a complete history of changes. Every
modification made to files can be recorded as a commit, allowing developers to revert to earlier
versions if necessary. Git operates locally on a developer's machine but can also synchronize with
remote repositories hosted on platforms such as GitHub. This makes collaboration efficient and
reliable. Understanding Git commands such as init, add, commit, branch, and merge is essential for
maintaining organized development workflows.
2. Initializing a Repository (git init)
The command 'git init' is used to initialize a new Git repository in a project folder. When this
command is executed, Git creates a hidden folder called '.git'. This folder stores all the metadata
and version history of the repository. After initialization, Git begins tracking changes in the directory.
Developers can then start adding files to the repository and committing their changes. It is important
to run this command only inside the project directory so that Git tracks only the intended files and
not unnecessary system files.
3. Adding Files to the Staging Area (git add)
Git uses a staging area to prepare changes before committing them. The command 'git add .'
stages all modified or newly created files in the current directory. This means Git prepares these
files to be included in the next commit. Developers can also add specific files individually using
commands such as 'git add filename'. The staging area acts as an intermediate step where
developers review which changes should be recorded in the repository history.
4. Recording Changes (git commit)
Once files are staged, the command 'git commit -m "message"' is used to record the changes
permanently in the repository history. Each commit contains a snapshot of the project at a particular
point in time along with a descriptive message explaining the change. Commit messages are
important because they help developers understand what changes were made and why. A
well-written commit history improves collaboration and debugging.
5. Viewing Differences (git diff)
The 'git diff' command is used to view differences between various versions of files. For example,
'git diff' shows differences between the working directory and the staging area. The command 'git
diff --staged' shows differences between staged changes and the last commit. Git diff is helpful for
reviewing modifications before committing them. By examining these differences, developers can
ensure that only the correct changes are committed.
6. Temporarily Saving Changes (git stash)
Sometimes developers need to switch tasks without committing incomplete work. Git provides the
'git stash' command to temporarily store changes. The command 'git stash push -m "message"'
saves current modifications and restores the working directory to a clean state. The command 'git
stash list' shows all saved stashes. To retrieve changes later, the command 'git stash pop' can be
used. This restores the most recent stash and removes it from the stash list. Stashing is useful
when switching branches or experimenting with code without losing work.
7. Working with Branches
Branches allow developers to work on different features or experiments without affecting the main
project. The command 'git branch' lists available branches. A new branch can be created using 'git
checkout -b branch_name'. This creates a new branch and immediately switches to it. Branching
helps maintain organized development because each feature or fix can be developed
independently before being merged into the main branch.
8. Merging Branches
After completing work in a branch, it can be merged into another branch using 'git merge'. For
example, 'git merge branch2' merges changes from branch2 into the current branch. During
merging, Git combines the histories of both branches. If the same lines of code were modified in
different branches, Git may produce a merge conflict that must be resolved manually. Once
conflicts are resolved, the merge can be completed and committed.
9. Pushing Changes to Remote Repositories
Git repositories can be connected to remote repositories hosted on platforms like GitHub. The
command 'git remote add origin ' links the local repository to a remote repository. After committing
changes locally, the command 'git push origin main' uploads the changes to the remote repository.
This allows other collaborators to access the updated code. Remote repositories serve as a backup
and collaboration platform.
10. Viewing Repository History
Git provides commands to visualize the commit history. The command 'git log --oneline --graph --all'
displays commits in a compact format along with a graph showing the branch structure. This helps
developers understand how different branches diverged and merged over time. Reviewing commit
history is useful for debugging, tracking progress, and understanding how the project evolved.