blog bg

December 19, 2024

Git Tutorial for Beginners

Share what you learn in this blog to prepare for your interview, create your forever-free profile now, and explore how to monetize your valuable knowledge.

 

What is Git?

Git is a distributed version control system that helps developers track changes in their code and collaborate with others.

 

Key Concepts

  1. Repository (Repo): A directory containing your project files and a hidden .git folder for version tracking.
  2. Commit: A snapshot of your project at a specific point in time.
  3. Branch: A separate line of development.
  4. Remote: A version of your project hosted on the internet (e.g., GitHub, GitLab).
  5. Staging Area: A place to prepare files before committing them.

 

Setup Git

 

Install Git: Download Git

Configure Git:

 

git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"

 

Common Git Commands

 

1. Start a New Repository

 

Initialize a new Git repository

 

 git init  

 

2. Clone an Existing Repository

 

git clone <repository-url>

 

3. Check Repository Status

 

git status

 

4. Add Changes to Staging Area

 

 

git add <file-name> # Or add all changes git add .

 

5. Commit Changes

 

 

git commit -m "Describe your changes"

 

6. View Commit History

 

git log

 

7. Create and Switch to a New Branch

 

git branch <branch-name> 
git checkout <branch-name> # Shortcut git checkout -b <branch-name>

 

8. Merge Branches

 

git checkout <main-branch> git merge <branch-name>

 

9. Push Changes to Remote Repository

 

 

git push origin <branch-name>

 

10. Pull Changes from Remote Repository

 

 

git pull

 

11. Resolve Merge Conflicts

  • Open conflicting files, resolve manually, and mark as resolved:

 

 

git add <file-name>
git commit

 

Working with Remote Repositories

 

Add a remote repository:

 

git remote add origin <repository-url>

 

Verify remotes:

git remote -v

 

 

Tips for Beginners

 

  1. Commit often with meaningful messages.
  2. Use branches for new features or fixes.
  3. Regularly pull updates from the remote repository to stay in sync.
  4. Always test changes before pushing to the main branch.

 

 

81 views

Please Login to create a Question