📓 GIT

In [1]:

Basic Git and GitHub Commands

2025-10-18 03:08

Basic Git and GitHub Commands

This document provides a guide to essential Git commands for version control and collaboration using GitHub.


1. Configuration

git config --global user.name "Your Name" — Sets your name for Git commits globally. Replace “Your Name” with your actual name.

git config --global user.email "your.email@example.com" — Sets your email address for Git commits globally. Replace “your.email@example.com” with your actual email.


2. Initialization & Cloning

git init — Initializes a new Git repository in the current directory.

git clone <repository_url> — Clones a remote repository to your local machine. Replace <repository_url> with the URL of the repository.


3. Staging & Committing

git status — Shows the status of your working directory, including modified and staged files.

git add <file> — Stages a specific file for commit. Replace <file> with the filename.

git add . — Stages all modified and untracked files in the current directory.

git commit -m "Commit message" — Commits the staged changes with a descriptive message. Replace “Commit message” with your own.


4. Branching & Merging

git branch — Lists all local branches in your repository.

git branch <branch_name> — Creates a new branch named <branch_name>.

git checkout <branch_name> — Switches to the branch <branch_name>.

git checkout -b <branch_name> — Creates and switches to a new branch <branch_name>.

git merge <branch_name> — Merges the specified branch into the current branch.

git branch -d <branch_name> — Deletes the branch <branch_name>.


5. Remote Repositories

git remote add origin <repository_url> — Adds a remote repository named origin with the URL <repository_url>.

git remote -v — Lists configured remote repositories.

git push origin <branch_name> — Pushes the local branch <branch_name> to the remote origin.

git pull origin <branch_name> — Pulls changes from the remote branch <branch_name> into your local branch.

git fetch — Retrieves metadata from the remote repository without merging changes.


6. Undoing Changes

git revert <commit_hash> — Creates a new commit that reverts changes from a specific commit. Replace <commit_hash> with the commit hash.

git reset HEAD <file> — Unstages a file. Replace <file> with the filename.

git checkout -- <file> — Discards changes to a specific file in your working directory. Replace <file> with the filename.


7. Inspecting History

git log — Shows commit history.

git log --oneline — Displays compact one-line commit history.

git diff — Shows differences between working directory and last commit.

git diff <file> — Shows differences for a specific file.

git show <commit_hash> — Shows details of a specific commit. Replace <commit_hash> with the hash.


8. Collaboration (GitHub Specific)

Forking a Repository: Create a copy of a repository under your own GitHub account.

Creating a Pull Request: Submit changes from your forked repository to the original repository.

Reviewing Pull Requests: Provide feedback and suggestions on others’ pull requests.


9. Merging Branches (Specific Example)

To merge changes from dev_master into dev_preview:

git checkout dev_preview

git merge dev_master

This switches to the dev_preview branch and merges changes from dev_master into it.

After merging, push to the remote repository:

git push origin dev_preview

Note: Resolve any merge conflicts that arise during the merge process.