Git and GitHub Commands: A Comprehensive Guide from Basic to Advanced

Git is a distributed version control system used to track changes in source code during software development. GitHub, on the other hand, is a cloud-based hosting service that helps manage Git repositories efficiently. Understanding Git and GitHub commands is crucial for developers, whether they are beginners or experts. This article provides a detailed overview of Git and GitHub commands, from basic to advanced, along with their explanations.


1. Basic Git Commands

These commands are fundamental and help in setting up and initializing Git repositories.

1.1 git --version

Usage: Displays the installed Git version.
Why? To check if Git is installed on the system.

git --version

1.2 git config

Usage: Configures Git settings like username and email.
Why? Essential for associating commits with the correct user.

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

1.3 git init

Usage: Initializes a new Git repository in a directory.
Why? Converts an existing directory into a Git-tracked project.

git init

1.4 git clone

Usage: Clones a remote repository to the local system.
Why? Used to copy a GitHub repository to a local machine.

git clone <repository_url>

2. Working with Git

These commands help in managing files and tracking changes in a repository.

2.1 git status

Usage: Displays the state of the working directory and staging area.
Why? Helps in checking modified, new, and deleted files.

git status

2.2 git add

Usage: Adds files to the staging area.
Why? Prepares changes for committing.

git add <file_name>   # Adds a specific file
git add .             # Adds all changes in the directory

2.3 git commit

Usage: Records changes in the repository.
Why? Saves modifications along with a descriptive message.

git commit -m "Initial commit"

2.4 git log

Usage: Displays the commit history.
Why? Useful for tracking previous commits.

git log

2.5 git diff

Usage: Shows changes between commits, branches, and working directories.
Why? Helps in understanding differences before committing changes.

git diff

3. Branching and Merging

Branching is essential in Git for managing features and versions.

3.1 git branch

Usage: Lists, creates, or deletes branches.
Why? Helps in managing multiple development lines.

git branch        # List all branches
git branch <name> # Create a new branch
git branch -d <name> # Delete a branch

3.2 git checkout

Usage: Switches between branches.
Why? Allows developers to work on different features separately.

git checkout <branch_name>

3.3 git merge

Usage: Merges branches into the current branch.
Why? Combines completed work from different branches.

git merge <branch_name>

3.4 git rebase

Usage: Integrates changes from one branch into another.
Why? Keeps a linear commit history.

git rebase <branch_name>

4. Remote Repositories and Collaboration

These commands help in managing remote repositories.

4.1 git remote

Usage: Manages remote connections.
Why? Allows linking with GitHub or other repositories.

git remote add origin <repository_url>
git remote -v  # Lists connected remotes

4.2 git push

Usage: Uploads local commits to a remote repository.
Why? Syncs local changes with the remote repository.

git push origin <branch_name>

4.3 git pull

Usage: Fetches and merges changes from a remote repository.
Why? Keeps the local branch updated.

git pull origin <branch_name>

4.4 git fetch

Usage: Downloads changes from a remote repository but does not merge.
Why? Allows reviewing changes before merging.

git fetch origin

5. Undoing Changes and Resetting

These commands help in undoing changes and reverting commits.

5.1 git reset

Usage: Resets changes to a previous commit.
Why? Useful for discarding unwanted commits.

git reset --soft <commit_id>  # Keeps changes in the working directory
git reset --hard <commit_id>  # Deletes changes permanently

5.2 git revert

Usage: Creates a new commit that undoes a previous commit.
Why? A safer alternative to git reset.

git revert <commit_id>

5.3 git stash

Usage: Temporarily saves changes without committing.
Why? Useful when switching branches without committing changes.

git stash        # Stashes changes
git stash pop    # Restores stashed changes

6. Advanced Git Commands

These commands help in optimizing workflows.

6.1 git cherry-pick

Usage: Applies a specific commit from one branch to another.
Why? Useful for selectively merging commits.

git cherry-pick <commit_id>

6.2 git reflog

Usage: Shows a log of all references to commits.
Why? Helps recover lost commits.

git reflog

6.3 git bisect

Usage: Finds the commit that introduced a bug.
Why? Useful for debugging issues.

git bisect start
git bisect bad <commit_id>
git bisect good <commit_id>

6.4 git submodule

Usage: Manages submodules in a repository.
Why? Helps in including external repositories within a project.

git submodule add <repository_url>

7. GitHub-Specific Commands

These commands help in working with GitHub.

7.1 gh auth login

Usage: Authenticates GitHub CLI.
Why? Allows performing GitHub operations via the terminal.

gh auth login

7.2 gh repo create

Usage: Creates a new GitHub repository from the terminal.
Why? Automates repo creation without visiting GitHub.

gh repo create <repo_name>

7.3 gh issue create

Usage: Creates an issue on GitHub.
Why? Useful for reporting bugs or feature requests.

gh issue create --title "Bug Report" --body "Description of the issue"

Conclusion

Mastering Git and GitHub commands is essential for version control and collaboration. This guide covered everything from basic commands like git init and git commit to advanced ones like git rebase and git cherry-pick. By integrating these commands into your workflow, you can efficiently manage source code, collaborate with teams, and maintain a clean project history.

Most Frequently Asked Questions (FAQs) About Git & GitHub

Q1: Pichle 2 ya 5 din pehle kiye gaye code ko revert kaise karein?

Agar aap apne previous code ko revert ya wapas lana chahte hain, toh aap git reset, git revert, aur git checkout jaise commands ka use kar sakte hain.

Method 1: git log se Commit ID nikalna

Sabse pehle aapko check karna hoga ki kaunsa commit aapko revert karna hai:

git log --oneline

Ye command aapko commit history dikhayegi, jisme commit ID milegi.


Method 2: git reset Command ka Use (Permanent Change)

Agar aap last 5 din pehle ka code wapas lana chahte hain aur naye changes hata dena chahte hain:

git reset --hard <commit_id>

Savdhani:

  • --hard flag se jo bhi changes aapne kiye hain, wo permanently delete ho jayenge.

  • Agar aap sirf history reset karna chahte hain bina files delete kiye, toh --soft ka use karein:

git reset --soft <commit_id>

Method 3: git revert Command ka Use (Safer Option)

Agar aap commit delete kiye bina sirf changes wapas lana chahte hain, toh git revert ka use karein:

git revert <commit_id>

Savdhani:

  • Ye command ek naya commit create karti hai jo pehle wale changes ko undo karti hai.

  • Ye method safer hai kyunki purane commits safe rahenge.


Method 4: git checkout ka Use (Temporary Change)

Agar aap kisi purane commit ko sirf dekhna chahte hain bina changes apply kiye:

git checkout <commit_id>

Savdhani:

  • Aap checkout se kisi purane commit par ja sakte hain, lekin naye commits push karne ke liye wapas branch par aana hoga:
git checkout main

Q2: Git use karte waqt kaun-si savdhaniyan rakhni chahiye?

1. Code Revert ya Reset Karte Waqt:

  • git reset --hard use karne se pehle commit ID confirm karein, warna data loss ho sakta hai.

  • Agar sure nahi hain, toh git revert ka use karein jo safer option hai.

2. Remote Repository pe Changes Push Karne Se Pehle:

  • git status aur git diff check karein taki galti se galat files push na ho jayein.

  • git pull origin main karke latest changes fetch karein, taki kisi aur ke changes overwrite na ho jayein.

3. Branching Ka Sahi Use Karein:

  • Directly main ya master branch pe kaam na karein, feature branch bana kar kaam karein.

  • git branch feature-xyz se ek naya branch banakar usme changes karein.

4. Regularly Commits Karein:

  • Badi changes ek saath commit na karein, chhoti-chhoti changes commit karein taaki revert karna easy ho.

  • Commit message clear aur descriptive hone chahiye jaise:

git commit -m "Fixed login bug in authentication module"

5. Backup aur Remote Tracking:

  • Har kaam ke baad git push origin <branch_name> se remote repository pe backup bana kar rakhein.

  • Agar accidentally kuch delete ho gaya, toh git reflog se commits wapas la sakte hain.


Conclusion:

Agar aap previous code wapas lana chahte hain, toh git reset, git revert, aur git checkout commands ka sahi tariqe se use karein. Git use karte waqt hamesha proper branching, commits, aur backups ka dhyan rakhein taki kisi bhi data loss se bacha ja sake. 🚀