๐Ÿ“ Essential Git Commands for Local and Production Environments

Author: neptune | 16th-Aug-2025
๐Ÿท๏ธ #Github #IT

Version control with Git is crucial for managing your codebase efficiently. This article covers important Git commands used in production and local environments with practical examples.

โœ… 1. Checking Commit Differences Between Local and Remote

Remote repo : origin/feature/2025

Local repo : feature/2025

๐Ÿ”ท a. View Commits Only in Local

git log origin/feature/2025..feature/2025 --oneline

๐Ÿ” Explanation: Shows commits that exist locally but not in the remote branch.

โœ”๏ธ Example Output:

8e3c1db Updated home page styles

d2a4f32 Added new API endpoint for views

๐Ÿ”ท b. View Commits Only in Remote

git log feature/2025..origin/feature/2025 --oneline

๐Ÿ” Explanation: Shows commits that exist in the remote but not locally.

โœ”๏ธ Example Output:

f6d7aab Bug fix in auth middleware

c8e9b23 Updated README with deployment steps


ย 2. Visualizing Branch Differences

git log --oneline --graph --decorate --abbrev-commit origin/feature/2025..feature/2025

๐Ÿ” Explanation: Visualizes commits only in your local branch compared to remote with a graph view.

โœ”๏ธ Example Output:

* d2a4f32 (HEAD -> feature/2025) Added new API endpoint for views

* 8e3c1db Updated home page styles

โœ… 3. Checking File Differences Between Local and Remote

๐Ÿ”ท a. Local vs Remote

git diff origin/feature/2025..feature/2025

๐Ÿ” Explanation: Shows file changes in local that are not in remote.

๐Ÿ”ท b. Remote vs Local

git diff feature/2025..origin/feature/2025

๐Ÿ” Explanation: Shows file changes in remote that are not in local.


โœ… 4. Fetch Remote Changes Without Merging

git fetch

๐Ÿ” Explanation: Downloads updates from remote branches without merging them into your local branch.

โœ… 5. Pulling Changes with Different Strategies

๐Ÿ”ท a. Merge (Default)

sudo git pull --no-rebase

Merges remote changes into local branch, creating a merge commit if necessary.

๐Ÿ”ท b. Rebase

sudo git pull --rebase

Reapplies your local commits on top of remote commits for a linear history.

๐Ÿ”ท c. Fast-Forward Only

sudo git pull --ff-only

Updates your branch only if it can be fast-forwarded; otherwise, it aborts.

๐Ÿ”ท d. Configure Pull Behavior Permanently

git config pull.rebase true ย  # Always rebase on pull

git config pull.rebase falseย  # Always merge on pull

Note : if you want to keep remote changes only discard local then

git reset --hard origin/feature/2025

โœ… 6. Removing a File from Git Tracking but Keeping It Locally

๐Ÿ”ท Example: Ignoring db.sqlite3

sudo git rm --cached mysite/db.sqlite3

echo "/mysite/db.sqlite3" >> .gitignore

sudo git add .gitignore

sudo git commit -m "Stop tracking db.sqlite3 and update .gitignore"

sudo git push

โœ”๏ธ Explanation:

  • git rm --cached untracks the file while keeping it locally.

  • Adds it to .gitignore to prevent future tracking


โœ… 7. How to avoid typing your GitHub username + PAT (Personal Access Token) every time you git fetch/pull/push.

โœ… Recommended (Secure) Setup: Use PAT instead of password

1. Update your remote URL to include your username

Run this inside your repo:

git remote set-url origin https://github.com/Neptune998/<your-repo>.git

โš ๏ธ Donโ€™t hardcode the token here โ€” weโ€™ll let Git credential helper handle it.

2. Use Git Credential Manager (secure cache)

Install credential helper if not already installed:

git config --global credential.helper store

Now, when you run git fetch or git push for the first time, Git will ask for credentials:

Username: Neptune998

Password: <your PAT>

โœ… Alternative: Set credentials at system level

You can configure it permanently for all repos on that server:

git config --global user.name "Neptune998"

git config --global user.email "[email protected]"

git config --global credential.helper store

Then push once with username + token, and Git wonโ€™t ask again.

โœ… More Secure Option (Best Practice)

Instead of storing a PAT in plaintext:

Use SSH keys for authentication (preferred for servers).

Youโ€™d set up an SSH key on your server and add the public key to your GitHub account โ†’ then no need for username/token at all.

โœ… 8. Other Important Git Commands for Production and Local

Task

Command

Check current branch

git branch

Switch branch

git checkout branch_name

Create new branch

git checkout -b new_branch

Delete branch

git branch -d branch_name

View remote branches

git branch -r

View all branches

git branch -a

Stage all changes

git add .

Commit changes

git commit -m "message"

View status

git status

View last commit

git log -1

Force push (use with caution)

git push --force

Discard local changes

git checkout -- file_name

Reset to remote state (dangerous)

git reset --hard origin/branch_name

๐Ÿ” Summary

These Git commands help you:

  • Manage divergent branches

  • Check commit differences

  • Pull and merge efficiently

  • Avoid database merge conflicts in production

  • Maintain clean Git practices

Keep this as your Git command cheat sheet for daily development, deployments, and server management.