DEV Community

Pratik Pathak
Pratik Pathak

Posted on • Originally published at pratikpathak.com on

Git – The time machine of Developers

GitbashGitGudGIF

Welcome to the Git Workshop! This workshop is designed to introduce you to Git, a distributed version control system that helps manage and track changes in your code.

Introduction to Git

Git is a distributed version control system created by Linus Torvalds in 2005. It allows multiple developers to work on a project simultaneously, track changes, and collaborate effectively. Git is widely used in the software development industry and is a fundamental tool for version control.

Installation

Windows

Download the Git installer from the official Git website and follow the installation instructions.

Download

macOS

Install Git using Homebrew:

brew install git
Enter fullscreen mode Exit fullscreen mode

Linux

Install Git using the package manager:

sudo apt-get install git # Debian/Ubuntu

sudo yum install git # Fedora
Enter fullscreen mode Exit fullscreen mode

Getting Started with Git

Initializing a Repository

To create a new Git repository:

git init
Enter fullscreen mode Exit fullscreen mode

Cloning a Repository

To clone an existing repository:

git clone <repository-url>
Enter fullscreen mode Exit fullscreen mode

Basic Git Commands

Checking Status

To check the status of your working directory and staging area:

git status
Enter fullscreen mode Exit fullscreen mode

Adding Files

To add files to the staging area:

git add <file-name>
Enter fullscreen mode Exit fullscreen mode

To add all files:

git add .
Enter fullscreen mode Exit fullscreen mode

Committing Changes

To commit changes with a message:

git commit -m "Your commit message"
Enter fullscreen mode Exit fullscreen mode

Pushing Changes

To push changes to the remote repository:

git push origin <branch-name>
Enter fullscreen mode Exit fullscreen mode

Pulling Changes

To pull changes from the remote repository:

git pull origin <branch-name>
Enter fullscreen mode Exit fullscreen mode

Branching and Merging

Creating a Branch

To create a new branch:

git branch <branch-name>
Enter fullscreen mode Exit fullscreen mode

Switching Branches

To switch to a different branch:

git checkout <branch-name>
Enter fullscreen mode Exit fullscreen mode

Merging Branches

To merge a branch into the current branch:

git merge <branch-name>
Enter fullscreen mode Exit fullscreen mode

Working with Remote Repositories

Adding a Remote

To add a remote repository:

git remote add origin <remote-url>
Enter fullscreen mode Exit fullscreen mode

Fetching and Pulling

To fetch changes from the remote repository:

git fetch origin
Enter fullscreen mode Exit fullscreen mode

To pull changes from the remote repository:

git pull origin <branch-name>
Enter fullscreen mode Exit fullscreen mode

Pushing to Remote

To push changes to the remote repository:

git push origin <branch-name>
Enter fullscreen mode Exit fullscreen mode

Advanced Git Commands

Stashing Changes

To stash changes:

git stash
Enter fullscreen mode Exit fullscreen mode

To apply stashed changes:

git stash apply
Enter fullscreen mode Exit fullscreen mode

Rebasing

To rebase your current branch onto another branch:

git rebase <branch-name>
Enter fullscreen mode Exit fullscreen mode

To pick up, squash, or reset your commits :

git rebase HEAD~<No of Commits> -i

## force push
git push --force
Enter fullscreen mode Exit fullscreen mode

Best Practices

  • Commit often with meaningful messages.
  • Use branches for new features or bug fixes.
  • Keep your branch history clean by rebasing and squashing commits.
  • Regularly push your changes to remote repositories.
  • Review code and collaborate using pull requests.

Resources

Q&A

Feel free to ask any questions during the workshop. Happy coding!

Top comments (0)