50 Git commands:

50 Git commands:

  1. git init Initializes a new Git repository.

     $ git init
     Initialized empty Git repository in /path/to/repository/
    
  2. git clone Clones a remote repository to your local machine.

     $ git clone https://github.com/username/repository.git
     Cloning into 'repository'...
    
  3. git add Stages changes for commit.

     $ git add file.txt
    
  4. git status Shows the status of the working directory and staged changes.

     $ git status
    
  5. git commit Commits staged changes.

     $ git commit -m "Added new feature"
    
  6. git log Displays commit history.

     $ git log
    
  7. git diff Shows differences between working directory and staged changes.

     $ git diff
    
  8. git branch Lists branches.

     $ git branch
    
  9. git checkout Switches branches or restores files.

     $ git checkout branch_name
    
  10. git merge Merges changes from one branch into another.

    $ git merge feature_branch
    
  11. git pull Fetches and integrates changes from a remote repository.

    $ git pull origin master
    
  12. git push Pushes changes to a remote repository.

    $ git push origin master
    
  13. git remote Manages remote repositories.

    $ git remote add origin https://github.com/username/repository.git
    
  14. git fetch Downloads objects and refs from a remote repository.

    $ git fetch origin
    
  15. git stash Temporarily stores changes to work on something else.

    $ git stash
    
  16. git tag Creates and manages tags for specific commits.

    $ git tag v1.0.0
    
  17. git reset Unstages changes or moves the HEAD to a specific commit.

    $ git reset HEAD file.txt
    
  18. git rebase Reapplies commits on top of another base.

    $ git rebase master
    
  19. git config Sets configuration options.

    $ git config --global user.name "Your Name"
    $ git config --global user.email "your.email@example.com"
    
  20. git log --oneline Displays compact commit history.

    $ git log --oneline
    
  21. git show Shows information about a commit.

    $ git show commit_hash
    
  22. git cherry-pick Applies a commit from one branch to another.

    $ git cherry-pick commit_hash
    
  23. git rm Removes files from the working directory and stages the removal.

    $ git rm file.txt
    
  24. git revert Creates a new commit that undoes changes from a previous commit.

    $ git revert commit_hash
    
  25. git reflog Displays the history of HEAD positions.

    $ git reflog
    
  26. git clean Removes untracked files and directories from the working directory.

    $ git clean -n  # Dry-run
    $ git clean -f  # Force removal
    
  27. git tag -a Creates an annotated tag with a message.

    $ git tag -a v1.0.0 -m "Version 1.0.0"
    
  28. git log --graph Displays commit history as a graph.

    $ git log --graph --oneline
    
  29. git config --list Lists all Git configuration settings.

    $ git config --list
    
  30. git log --since / git log --until Displays commit history within a time range.

    $ git log --since="2 weeks ago"
    $ git log --until="2023-07-01"
    
  31. git cherry Shows commits that have not been merged.

    $ git cherry master feature_branch
    
  32. git revert --no-commit Reverts changes interactively without committing.

    $ git revert --no-commit commit_range
    
  33. git log --author Filters commit history by author.

    $ git log --author="John Doe"
    
  34. git log --stat Displays file statistics with commit history.

    $ git log --stat
    
  35. git blame Shows who last modified each line in a file.

    $ git blame file.txt
    
  36. git tag -d Deletes a tag.

    $ git tag -d v1.0.0
    
  37. git log -p Displays commit history with patch diffs.

    $ git log -p
    
  38. git rev-parse Converts a revision string into a SHA-1 hash.

    $ git rev-parse HEAD
    
  39. git remote -v Lists remote repositories and their URLs.

    $ git remote -v
    
  40. git log --decorate Displays references (branches, tags) in commit history.

    $ git log --decorate
    
  41. git bisect Performs a binary search to find a faulty commit.

    $ git bisect start
    $ git bisect good <commit>
    $ git bisect bad <commit>
    $ git bisect reset
    
  42. git log --grep Searches commit messages for a specific keyword.

    $ git log --grep="bug fix"
    
  43. git log --name-only Displays only file names in commit history.

    $ git log --name-only
    
  44. git rebase -i Interactively rewrites commit history.

    $ git rebase -i HEAD~3
    
  45. git log --before / git log --after Displays commit history before/after a specific date.

    $ git log --before="2023-01-01"
    $ git log --after="2022-01-01"
    
  46. git checkout -b Creates a new branch and switches to it.

    $ git checkout -b new_feature
    
  47. git log --cherry-pick Shows commits that have been cherry-picked.

    $ git log --cherry-pick master..feature_branch
    
  48. git log -S Searches for changes that added or removed a specific string.

    $ git log -S "function_name"
    
  49. git reflog expire Expires old reflog entries.

    $ git reflog expire --expire=30.days refs/heads/master
    
  50. git commit --amend Modifies the most recent commit.

    $ git commit --amend