DEV Community

KAMAL KISHOR
KAMAL KISHOR

Posted on

Git Complete Tutorial: A Step-by-Step Guide for Beginners

Git is a distributed version control system that helps developers track changes in their code, collaborate with others, and manage software projects efficiently. Whether you are a beginner or just brushing up on your skills, this tutorial will walk you through everything you need to know to master Git.


Table of Contents

  1. What is Git?
  2. Installing Git
  3. Initial Configuration
  4. Creating and Cloning Repositories
  5. Basic Git Workflow
  6. Branching and Merging
  7. Working with Remote Repositories
  8. Undoing Changes
  9. Using .gitignore
  10. Advanced Git Commands
  11. Git GUI Tools
  12. Conclusion

What is Git?

Git is a version control system used for tracking changes in source code during software development. It allows multiple developers to work on a project simultaneously, without overwriting each other's work.


Installing Git

Windows / macOS / Linux:

  • Download and install Git from git-scm.com.
  • Verify installation:
git --version
Enter fullscreen mode Exit fullscreen mode

Initial Configuration

Set your identity:

git config --global user.name "Your Name"
git config --global user.email "you@example.com"
Enter fullscreen mode Exit fullscreen mode

View your configuration:

git config --list
Enter fullscreen mode Exit fullscreen mode

Creating and Cloning Repositories

Create a new local repository:

mkdir my-project
cd my-project
git init
Enter fullscreen mode Exit fullscreen mode

Clone an existing repository:

git clone https://github.com/username/repo-name.git
Enter fullscreen mode Exit fullscreen mode

Basic Git Workflow

Check status:

git status
Enter fullscreen mode Exit fullscreen mode

Stage files:

git add filename     # Add specific file
git add .            # Add all changes
Enter fullscreen mode Exit fullscreen mode

Commit changes:

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

View commit history:

git log
git log --oneline
Enter fullscreen mode Exit fullscreen mode

Branching and Merging

Create a new branch:

git branch new-branch
Enter fullscreen mode Exit fullscreen mode

Switch to a branch:

git checkout new-branch
Enter fullscreen mode Exit fullscreen mode

Create and switch in one step:

git checkout -b new-branch
Enter fullscreen mode Exit fullscreen mode

Merge a branch:

git checkout main
git merge new-branch
Enter fullscreen mode Exit fullscreen mode

Delete a branch:

git branch -d new-branch
Enter fullscreen mode Exit fullscreen mode

Working with Remote Repositories

Add remote:

git remote add origin https://github.com/username/repo.git
Enter fullscreen mode Exit fullscreen mode

Push changes:

git push -u origin branch-name
Enter fullscreen mode Exit fullscreen mode

Pull changes:

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

Undoing Changes

Unstage a file:

git reset filename
Enter fullscreen mode Exit fullscreen mode

Undo last commit (keep changes):

git reset --soft HEAD~1
Enter fullscreen mode Exit fullscreen mode

Discard all local changes:

git checkout -- .
Enter fullscreen mode Exit fullscreen mode

Using .gitignore

Create a .gitignore file to exclude files/folders from version control:

node_modules/
.env
dist/
*.log
Enter fullscreen mode Exit fullscreen mode

Advanced Git Commands

Stash changes:

git stash
Enter fullscreen mode Exit fullscreen mode

Apply stashed changes:

git stash apply
Enter fullscreen mode Exit fullscreen mode

Rebase:

git rebase branch-name
Enter fullscreen mode Exit fullscreen mode

Cherry-pick a commit:

git cherry-pick commit-id
Enter fullscreen mode Exit fullscreen mode

Git GUI Tools

  • GitHub Desktop
  • Sourcetree
  • GitKraken
  • VS Code Source Control Panel

Conclusion

Git is an essential tool for modern software development. With this step-by-step guide, you can start using Git confidently for your projects. Keep practicing, explore advanced commands, and soon you'll be managing branches, resolving merge conflicts, and collaborating like a pro.

Happy coding! 🚀

Top comments (2)

Collapse
 
nevodavid profile image
Nevo David

pretty cool seeing all the basics laid out step by step - gotta admit git can be rough at first but once you get it, it sticks

Collapse
 
manjunathpatat profile image
Manjunath Patat

Thanks