List of Git commands

1. Configuration

  • git config: Set Git configuration values (user name, email, etc.)
    • git config --global user.name "Your Name": Set global user name
    • git config --global user.email "youremail@example.com": Set global user email
    • git config --list: List all Git configuration values

2. Repository Initialization

  • git init: Initialize a new Git repository in the current directory
  • git clone <repository-url>: Clone an existing repository

3. Checking Repository Status

  • git status: Show the status of changes (modified, staged, etc.)
  • git diff: Show changes between commits, commit and working directory, etc.
    • git diff --staged: Show differences between staged changes and the last commit

4. Branching

  • git branch: List all branches
    • git branch <branch-name>: Create a new branch
    • git branch -d <branch-name>: Delete a branch (if fully merged)
    • git branch -D <branch-name>: Force delete a branch (even if not merged)
    • git branch -m <new-branch-name>: Rename the current branch
    • git branch -m <old-branch-name> <new-branch-name>: Rename a non-current branch
  • git checkout <branch-name>: Switch to a different branch
  • git checkout -b <branch-name>: Create and switch to a new branch
  • git switch <branch-name>: Switch to a different branch (alternative to git checkout)

5. Committing Changes

  • git add <file>: Stage changes (specific file)
  • git add .: Stage all changes in the current directory
  • git commit -m "message": Commit changes with a message
  • git commit --amend: Amend the last commit (edit commit message or add changes)
  • git commit --no-edit --amend: Amend the last commit without changing the commit message

6. Viewing Commit History

  • git log: View the commit history
    • git log --oneline: View the commit history in a compact format
    • git log --graph --oneline --decorate: View a graphical representation of the commit history
  • git show <commit-hash>: Show the details of a specific commit

7. Remote Repositories

  • git remote -v: List remote repositories and their URLs
  • git remote add <name> <repository-url>: Add a new remote repository
  • git remote remove <name>: Remove a remote repository
  • git push <remote> <branch>: Push local changes to a remote repository
  • git push <remote> --delete <branch>: Delete a remote branch
  • git pull <remote> <branch>: Fetch and merge changes from a remote repository
  • git fetch <remote>: Fetch changes from a remote repository without merging
  • git fetch --all: Fetch all branches from all remotes
  • git push --set-upstream <remote> <branch>: Set the upstream branch for the current branch

8. Merging and Rebasing

  • git merge <branch>: Merge the specified branch into the current branch
  • git rebase <branch>: Rebase the current branch onto the specified branch
  • git merge --abort: Abort a merge process (if there are conflicts)
  • git rebase --abort: Abort a rebase process

9. Stashing Changes

  • git stash: Stash changes (save them temporarily)
  • git stash pop: Apply the most recent stash and remove it from the stash list
  • git stash apply: Apply a stash without removing it
  • git stash list: List all stashed changes
  • git stash drop: Remove a specific stash from the list

10. Resolving Conflicts

  • git mergetool: Launch the merge tool to resolve conflicts
  • git diff: Show the differences between conflicting changes

11. Undoing Changes

  • git reset <commit>: Reset the current branch to a specific commit
    • git reset --soft <commit>: Keep changes staged
    • git reset --mixed <commit>: Keep changes but unstage them
    • git reset --hard <commit>: Discard all changes
  • git revert <commit-hash>: Create a new commit that undoes a specific commit
  • git checkout -- <file>: Discard changes to a file (revert it to the last committed state)

12. Viewing Differences

  • git diff: Show changes between the working directory and the index (staging area)
  • git diff --staged: Show changes between the staged files and the last commit

13. Cleaning Up

  • git clean -f: Remove untracked files
  • git clean -fd: Remove untracked files and directories
  • git clean -f -n: Show which files would be removed by git clean (dry run)

14. Tags

  • git tag: List all tags
  • git tag <tag-name>: Create a new tag
  • git tag -d <tag-name>: Delete a tag
  • git push origin <tag-name>: Push a tag to the remote repository
  • git push --tags: Push all tags to the remote repository

15. Configuration and Aliases

  • git config --global <key> <value>: Set a global Git configuration key
  • git config --global alias.<alias-name> <command>: Create a custom Git alias

16. Troubleshooting and Diagnostics

  • git fsck: Verify the integrity of the Git repository
  • git reflog: Show the history of the HEAD and where it pointed at different times
  • git show-ref: Show references to all commits, tags, and branches

17. Interactive Commands

  • git rebase -i <commit-hash>: Interactive rebase to rewrite history
  • git add -p: Add changes interactively

Summary of Common Commands

ActionCommand
Initialize a repositorygit init
Clone a repositorygit clone <repository-url>
Check statusgit status
Stage a filegit add <file>
Commit changesgit commit -m "message"
View commit historygit log
Create a new branchgit branch <branch-name>
Switch branchesgit checkout <branch-name>
Merge branchesgit merge <branch-name>
Push changes to remotegit push origin <branch-name>
Pull changes from remotegit pull origin <branch-name>
Fetch updates from remotegit fetch origin
Revert a commitgit revert <commit-hash>
Stash changesgit stash
Apply stashed changesgit stash pop
Remove untracked filesgit clean -f
Create a taggit tag <tag-name>

These commands should cover most of your Git needs. For detailed information on any specific command, you can use git <command> --help.

No images available.