Skip to main content

Git Commands

Git Setup

  • Configure Git for the first time:

    git config --global user.name "Your Name"
    git config --global user.email "youremail@domain.com"
  • Check your configuration:

    git config --list

Starting with Git

  • Initialize a new Git repository:

    git init
  • Clone an existing repository:

    git clone https://github.com/username/repository.git

Basic Git Commands

  • Add changes to staging area:

    git add filename

    or add all changes:

    git add .
  • Commit changes to the repository:

    git commit -m "Commit message"
  • View the commit history:

    git log
  • Check the status of your files:

    git status

Branching and Merging

  • Create a new branch:

    git branch branch_name
  • Switch to a branch:

    git checkout branch_name

    or create and switch in one command:

    git checkout -b branch_name
  • Merge a branch into the current branch:

    git merge branch_name
  • Delete a branch:

    git branch -d branch_name

Remote Repositories

  • View remote repositories:

    git remote -v
  • Add a remote repository:

    git remote add origin https://github.com/username/repository.git
  • Push changes to the remote repository:

    git push origin master

    or push a specific branch:

    git push origin branch_name
  • Pull changes from the remote repository:

    git pull origin master

Miscellaneous

  • Stash changes:

    git stash

    Apply stashed changes:

    git stash pop
  • Reset changes: Reset to the last commit, discarding all changes:

    git reset --hard

    Unstage changes:

    git reset
  1. Viewing Changes

    • Show modified files:
      git diff
    • Show changes in staged files:
      git diff --cached
  2. Undoing Changes

    • Undo the last commit, keeping changes:
      git reset --soft HEAD~1
    • Revert a commit by creating a new commit:
      git revert commit_hash
  3. Working with Remotes

    • Fetch changes from remote without merging:
      git fetch origin
    • Set up a new remote:
      git remote add remote_name remote_url
    • Remove a remote:
      git remote remove remote_name
  4. Tags

    • List all tags:
      git tag
    • Create a new tag:
      git tag -a tag_name -m "tag message"
    • Push tags to remote:
      git push origin --tags
  5. Advanced Merging

    • Abort a conflicting merge:
      git merge --abort
    • Use the 'theirs' strategy during a merge:
      git checkout --theirs filename
    • Use the 'ours' strategy during a merge:
      git checkout --ours filename
  6. Cleaning Up

    • Clean untracked files (dry run):
      git clean -n
    • Clean untracked files for real:
      git clean -f
  7. Stashing

    • List stashes:
      git stash list
    • Stash including untracked files:
      git stash -u
    • Apply a specific stash:
      git stash apply stash@{n}
  8. Inspecting & Comparing

    • Show the commit log as a graph:
      git log --graph
    • Show changes over time for a specific file:
      git log -p filename
    • Compare branches:
      git diff branch1..branch2

Visual representation of the commands

Every Bit of Support Helps!

If you have enjoyed this post, please consider buying me a coffee ☕ to help me keep writing!