Git & githubProgramming

Git reference – commands

1. Setup & Configuration

  • git config --global user.name "Your Name" – Set your global Git username. [ref]
  • git config --global user.email "you@example.com" – Set your global Git email. [ref]
  • git config --global core.editor nano – Set your preferred editor. [ref]
  • git config --list – Show all current Git configuration values. [ref]

2. Creating & Cloning Repositories

  • git init – Create a new Git repository in the current folder. [ref]
  • git clone <url> – Clone an existing repository from a remote source. [ref]
  • git clone <url> <folder> – Clone into a specific folder name. [ref]

3. Staging & Committing Changes

  • git status – Show changed files and what’s staged or unstaged. [ref]
  • git add <file> – Stage a specific file. [ref]
  • git add . – Stage all modified and new files. [ref]
  • git commit -m "message" – Commit staged changes with a message. [ref]
  • git commit -am "message" – Stage and commit all tracked file changes. [ref]
  • git restore <file> – Discard unstaged changes in a file. [ref]
  • git restore --staged <file> – Unstage a file without discarding changes. [ref]

4. Branching & Merging

  • git branch – List branches. [ref]
  • git branch <name> – Create a new branch. [ref]
  • git checkout <branch> – Switch to another branch. [ref]
  • git checkout -b <name> – Create and switch to a new branch. [ref]
  • git switch <branch> – Modern command for switching branches. [ref]
  • git merge <branch> – Merge another branch into the current one. [ref]
  • git branch -d <name> – Delete a branch that has been merged. [ref]
  • git branch -D <name> – Force-delete a branch. [ref]

5. Working With Remotes

  • git remote -v – Show remotes linked to your project. [ref]
  • git remote add origin <url> – Add a remote repository. [ref]
  • git push -u origin <branch> – Push a branch and set upstream tracking. [ref]
  • git push – Push committed changes to the remote. [ref]
  • git pull – Fetch and merge changes from the remote. [ref]
  • git fetch – Download changes without merging. [ref]
  • git push origin --delete <branch> – Delete a remote branch. [ref]

6. Viewing History & Inspecting Changes

  • git log – Show commit history. [ref]
  • git log --oneline --graph --decorate – Compact, visual commit history. [ref]
  • git show <commit> – Show details of a specific commit. [ref]
  • git diff – Show unstaged changes. [ref]
  • git diff --staged – Show staged changes. [ref]
  • git blame <file> – Show who last modified each line. [ref]

7. Undoing & Fixing Mistakes

  • git reset <file> – Unstage a file. [ref]
  • git reset --hard – Reset index and working tree to last commit (destructive). [ref]
  • git reset --soft <commit> – Move HEAD but keep changes staged. [ref]
  • git revert <commit> – Create a new commit that undoes a previous one. [ref]
  • git checkout <commit> -- <file> – Restore a file from a previous commit. [ref]

8. Stashing Work

  • git stash – Save uncommitted changes temporarily. [ref]
  • git stash list – List stashes. [ref]
  • git stash apply – Reapply the most recent stash. [ref]
  • git stash pop – Reapply and remove the most recent stash. [ref]
  • git stash drop – Delete a stash. [ref]

9. Rebasing & Advanced Operations

  • git rebase <branch> – Replay commits on top of another branch. [ref]
  • git rebase -i <commit> – Interactive rebase to edit/squash/reorder commits. [ref]
  • git cherry-pick <commit> – Apply a specific commit from another branch. [ref]
  • git tag <name> – Create a tag. [ref]
  • git tag -a <name> -m "message" – Create an annotated tag. [ref]
  • git push origin --tags – Push tags to the remote. [ref]

10. Cleaning Up

  • git clean -n – Show which untracked files would be removed. [ref]
  • git clean -f – Remove untracked files. [ref]
  • git clean -fd – Remove untracked files and directories. [ref]

11. Useful Shortcuts & Utilities

  • git shortlog -sn – Show commit counts by author. [ref]
  • git reflog – Show movements of HEAD (great for recovering lost commits). [ref]
  • git describe --tags – Show the nearest tag for the current commit. [ref]
  • git archive – Create a tar/zip archive of the repository. [ref]

Leave a Reply

Your email address will not be published. Required fields are marked *