What is Git?
Git is a distributed version control system that enables developers to track changes in their codebase, collaborate with others, and manage project history efficiently. It is a fundamental tool for modern software development.
Git and GitHub
Git is the version control tool, while GitHub is a web-based platform that hosts Git repositories. GitHub facilitates collaboration by providing a central location for code storage, issue tracking, and collaboration among multiple contributors.
Getting Started with Git
A repository is a storage space for a project’s files, often referred to as a codebase. In Git, repositories are hosted on platforms like GitHub. The
.git folder appears when you initialize a Git repository using the command
Basic Git Commands
git status to view the status of your repository. Untracked files are files that Git is not currently monitoring.
Adding Changes to Staging
To add all files to the staging area, use
git add .. The files in the staging area turn green in the status output.
git add .
After staging changes, commit them using
git commit -m "commit message". Commits create a snapshot of the project at a specific point in time.
git commit -m "Added new file"
git log to see a detailed history of commits. Each commit has a unique identifier, commit message, and other metadata.
Rolling Back Changes
Rollback commits using
git reset <commit id>. To unstage changes, use
git restore --stage <file.name>.
git reset <commit id>
git restore --stage <file.name>
Git stash is used to temporarily save changes that are not ready to be committed. Use
git stash pop, and
git stash clean to manage stashed changes.
git stash pop
git stash clean
Collaborative Development with Git
Working with Remote Repositories
Creating a Repository on GitHub
Create a new repository on GitHub and link it to your local Git repository using
git remote add origin <url>.
git remote add origin <url>
Pushing Changes to GitHub
git push origin <branch> to push changes to the GitHub repository.
git push origin main
Branching and Merging
Create and manage branches using
git branch. Merge branches with
git merge <branchname>.
git branch feature
git merge feature
Git Clone and Git Fork
git clone to copy a repository, and
git fork to create a personal copy of a repository.
git clone <repository-url>
Pull Requests on GitHub
Create branches for features, make changes, and create pull requests on GitHub to merge changes into the main branch.
git fetch --all --prune
Advanced Git Operations
Reorganize commit history with
git rebase -i <commit id>. Use pick and squash to combine and reorder commits.
git rebase -i <commit id>
Merge Conflict Resolution
Resolve conflicts with
git merge or
git rebase. Conflicts occur when changes conflict between branches.
git diff <branchname>
.gitignore file specifies files or directories to be ignored by Git, preventing them from being tracked.
Hooks in the
.git/hooks directory execute custom scripts before or after specific Git actions.
Cherry-pick allows you to apply specific commits from one branch to another.
git cherry-pick <commit-id>
Git Merge vs. Git Rebase
Choose between merging branches with
git merge or reorganizing commit history with
Git Fetch vs. Git Pull
git fetch retrieves changes from a remote repository, while
git pull fetches changes and merges them into the current branch.
git commit --amend to modify the last commit, combining staged changes with the previous commit.
git commit --amend
Mastering Git is essential for effective collaboration and version control in software development. By understanding the basics and exploring advanced features, DevOps engineers can optimize their workflow and contribute to successful project management.