Introduction to Git & Github

Introduction to Git & Github

If you want to last in the world of DevOps or Software development, you must know how to work with version control systems such as git. Version control is an essential tool that allows teams to collaborate effectively, track changes, and maintain the integrity of their repository or codebase.

Git, a distributed version control system, has become the default standard for managing projects of all sizes. In this guide, we'll explore everything you need to know to become proficient in version control and move one step forward in your DevOps journey.

What is Version Control?

Version control systems (VCS) play a critical role in modern software development by enabling teams to manage changes to their codebase systematically. We can track modifications, see history of revisions, collaborate with team members to streamline the software development life cycle.

Git, developed by Linus Torvalds in 2005, revolutionized the way developers collaborate on projects. According to Linus, git can mean anything depending on your mood. It is also a mispronunciation of 'get'. Git is distributed, meaning every developer has a complete copy of the repository, including its entire history. This distributed nature offers several advantages, such as improved performance, offline access, and enhanced flexibility in workflows.

Setting Up GIT In Your Machine

  • Before moving forward we need to install git in our system to be able to use it. Git can be installed on windows, Mac and Linux. If you are on Linux there is a chance that git comes preinstalled on your system. Otherwise, follow this guide from Github and install it on your machine. Windows & Mac users can follow that guide as well.

  • After you have installed git, go ahead and create an account on Github. Remember your email id and username. Then open your terminal and pass in your email id and username with below commands. This will register your name and email id globally in the git config file.

git config --global user.name "Your Name"
git config --global user.email "your@email.com"
  • To initialize a new Git repository in your project directory, cd to the project directory and type the below command. Congrats, you have created an empty git repository to track changes made to your project.
cd my-project
git init

Terms We Need to Know

Repositories: A repository, or repo, is a collection of files and directories managed by Git. Each project typically has its own repository, which stores the project's history and metadata.

Commits: A commit represents a snapshot of the project at a particular point in time. It includes changes to one or more files and a commit message that describes the modifications.

Branches: Branches allow developers to work on isolated features or fixes without affecting the main codebase.

Remotes: Remotes are references to repositories hosted on remote servers, such as GitHub or GitLab. Developers can push and pull changes between their local repository and remote repositories to synchronize their work with collaborators.

Common Git Commands

Here's a detailed table listing commonly used Git commands along with their descriptions and examples:

CommandDescriptionExample
git initInitializes a new Git repository in the current directory.git init
git clone <url>Clones a remote repository into a new directory.git clonehttps://github.com/user/repo.git
git add <file>Adds changes in a file to the staging area for the next commit.git add index.html or git add .
git commitRecords changes to the repository along with a commit message.git commit -m "Add index.html"
git statusDisplays the status of files in the working directory and the staging area.git status
git logDisplays the commit history in reverse chronological order.git log
git branchLists, creates, or deletes branches.git branch
git checkout <branch>Switches branches or restores files in the working directory.git checkout main
git merge <branch>Merges changes from one branch into another.git merge feature
git pullFetches changes from a remote repository and merges them into the current branch.git pull origin main
git pushPushes changes from the local repository to a remote repository.git push origin main
git remote add <name> <url>Adds a new remote repository.git remote add originhttps://github.com/user/repo.git
git fetchDownloads objects and refs from another repository.git fetch origin
git stashTemporarily shelves changes that are not ready to be committed.git stash save "Work in progress"
git reset <file>Resets the current HEAD to the specified state.git reset HEAD index.html
git diffShows changes between commits, commit and working tree, etc.git diff HEAD~1 HEAD
git rm <file>Removes files from the working directory and staging area.git rm unwanted_file.txt
git mv <old> <new>Moves or renames files in the working directory and staging area.git mv old_file.txt new_file.txt
git tag <tag_name>Creates, lists, deletes, or verifies tags.git tag -a v1.0 -m "Version 1.0"
git configSets configuration values for Git.git config --globaluser.name"John Doe"
git branch -d branch_nameDelete a merged branch.(local)git branch -d feature-branch
git push origin --delete branch_nameDelete a remote branchgit push origin --delete feature-branch

Advanced Git Commands

git rebase

Suppose you have a feature branch feature-branch that you want to integrate into the main branch. Instead of merging the changes, you want to reapply the commits from feature-branch onto main to maintain a linear commit history.

# Switch to the branch you want to rebase onto (e.g., main)
git checkout main

# Perform the rebase
git rebase feature-branch

git cherry-pick

Suppose you have a commit with the ID abcdef in another branch other-branch, and you want to apply this commit to your current branch.

# Switch to your current branch
git checkout main

# Cherry-pick the commit from other-branch
git cherry-pick abcdef

git bisect

Suppose you have a bug in your project, but you're not sure which commit introduced it. You can use git bisect to perform a binary search through the commit history to find the culprit.

# Start the bisect process
git bisect start

# Mark the current commit as bad
git bisect bad

# Mark a known good commit (e.g., a previous release)
git bisect good v1.0

# Git will automatically check out a commit for testing
# Test your project to determine if the bug is present
# Mark the commit as either good or bad using bisect good/bad

# Repeat testing and marking until Git identifies the first bad commit

git reflog

Suppose you accidentally delete a branch, but you want to recover it. You can use git reflog to find the commit reference before the deletion and restore the branch.

# View the reflog to find the commit reference before deletion
git reflog

# Restore the branch using the commit reference
git checkout -b branch_name abcdef

git filter-branch

Suppose you have a file containing sensitive information that you accidentally committed to your repository. You can use git filter-branch to remove it from the commit history.

# Remove the file from the entire commit history
git filter-branch --tree-filter 'rm -f sensitive_file.txt' -- --all

Conclusion

These commands will help you get started using git to track progress of your projects. Comment if you have any questions or suggestions. See you in the next one.

Did you find this article valuable?

Support ansuman by becoming a sponsor. Any amount is appreciated!