Git Basics: A Comprehensive Guide

Listen to this Post

Git is a powerful version control system that allows developers to track changes, collaborate, and manage code efficiently. Below is a detailed guide on Git basics, including essential commands and steps to get started.

Creating a New Repository

git init
git add --all
git commit -m "Initial commit"
git remote add origin https://github.com/<user>/<repo>.git
git push -u origin master

Working with Local Changes

git status
git add --all
git add -p <file>
git commit -m "Your message here"

Viewing History

git log
git log -p <file>
git blame <file>

Branching and Merging

git branch -av
git branch <new-branch>
git checkout <branch>
git merge <branch>

Undoing Changes

git reset --hard HEAD
git checkout HEAD <file>
git revert <commitId>

Cloning a Repository

git clone https://github.com/<user>/<repo>.git

Pulling Changes from Remote

git pull origin master

Pushing Changes to Remote

git push origin <branch>

Stashing Changes

git stash # Stash changes
git stash list # List all stashes
git stash apply # Apply the latest stash
git stash drop # Drop the latest stash

Tagging

git tag <tag-name> # Create a new tag
git tag # List all tags
git push origin <tag-name> # Push tag to remote

Viewing Differences

git diff # Show changes between working directory and index
git diff HEAD # Show changes between working directory and last commit

Deleting Branches

git branch -d <branch> # Delete a local branch (only if merged)
git branch -D <branch> # Force delete a local branch (even if unmerged)

Working with Remotes

git remote -v # List all remote repositories
git remote add <name> <URL> # Add a new remote repository
git remote remove <name> # Remove a remote repository

You Should Know:

  • Always use `git status` to check the current state of your repository.
  • Use `git add -p` to interactively stage changes.
    – `git stash` is useful for temporarily saving changes without committing them.
  • Use `git log -p` to view detailed changes in the commit history.
    – `git revert` is safer than `git reset` for undoing changes, as it creates a new commit.

What Undercode Say:

Git is an indispensable tool for developers, enabling efficient collaboration and version control. Mastering these commands will significantly enhance your workflow. For more advanced Git operations, refer to the official Git documentation. Practice these commands in a test repository to build confidence and proficiency. Happy coding!

References:

Reported By: Rani Dhage – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅

Join Our Cyber World:

💬 Whatsapp | 💬 TelegramFeatured Image