The Ultimate Handbook of Git Commands

Unlocking the Power of Git: A Practical Guide to Essential Commands for Version Control Mastery

The Ultimate Handbook of Git Commands

Here are some commonly used Git commands:

  1. Initialize a new Git repository: git init initializes a new Git repository, setting up version control for your project by creating a hidden .git directory to store history and configuration data.

     git init
    
  2. Clone a remote repository: git clone <repository_url> clones an existing remote repository to your local machine, creating a local copy of the entire repository, including all branches and commit history.

     git clone <repository_url>
    
  3. Check the status of the repository: git status shows the current status of your working directory, displaying any modified, staged, or untracked files.

     git status
    
  4. Add changes to the staging area: git add <file_name> stages a specific file, preparing it for inclusion in the next commit.

     git add <file_name>
    
  5. Add all changes to the staging area: git add . stages all changes in the working directory, preparing them for inclusion in the next commit.

     git add .
    
  6. Commit changes to the local repository: git commit -m "commit_message" creates a new commit with the staged changes and a brief commit message describing the changes made.

     git commit -m "commit_message"
    
  7. Push changes to a remote repository: git push <remote_name> <branch_name> pushes the committed changes from the specified branch to the remote repository.

     git push <remote_name> <branch_name>
    
  8. Pull changes from a remote repository: git pull <remote_name> <branch_name> fetches and merges changes from the remote repository into your current branch.

     git pull <remote_name> <branch_name>
    
  9. Create a new branch: git branch <branch_name> creates a new branch with the specified name.

     git branch <branch_name>
    
  10. Switch to a different branch: git checkout <branch_name> switches to the specified branch, making it the active branch.

    git checkout <branch_name>
    
  11. Create and switch to a new branch: git checkout -b <branch_name> creates and switches to a new branch with the specified name.

    git checkout -b <branch_name>
    
  12. Merge a branch into the current branch: git merge <branch_name> merges the specified branch into the current branch, combining the changes from both branches.

    git merge <branch_name>
    
  13. View the commit history: git log displays a detailed commit history, showing the latest commits first.

    git log
    
  14. View the changes in a specific commit: git show <commit_hash> shows the details of a specific commit, including changes, author, date, and commit message.

    git show <commit_hash>
    
  15. Discard changes in a file: git checkout -- <file_name> discards changes in the specified file and reverts it to the last committed state.

    git checkout -- <file_name>
    
  16. Discard all local changes: git reset --hard HEAD discards all local changes and resets the working directory to the last committed state. Use with caution as it permanently deletes uncommitted changes.

    git reset --hard HEAD
    
  17. Add a remote repository: git remote add <remote_name> <repository_url> adds a new remote repository with the specified name and URL to your Git project.

    git remote add <remote_name> <repository_url>
    
  18. View remote repositories: git remote -v shows a list of remote repositories and their corresponding URLs.

    git remote -v
    
  19. Remove a remote repository: git remote remove <remote_name> removes the specified remote repository from your Git project.

    git remote remove <remote_name>
    
  20. Create a new tag: git tag <tag_name> creates a new lightweight tag with the specified name at the current commit.

    git tag <tag_name>
    
  21. Push tags to a remote repository: git push --tags pushes all local tags to the remote repository.

    git push --tags
    
  22. View a summarized commit history: git log --oneline displays a summarized commit history, showing each commit as a single line.

    git log --oneline
    
  23. View a detailed commit history with graph: git log --graph --oneline --all displays a summarized commit history with a graph, showing all branches in a single line format.

    git log --graph --oneline --all
    
  24. View the difference between two branches: git diff <branch1> <branch2> shows the difference between two branches, highlighting the changes made in each branch.

    git diff <branch1> <branch2>
    
  25. Amend the last commit with new changes: git commit --amend allows you to amend the last commit by incorporating new changes or modifying the commit message.

    git commit --amend
    
  26. Revert a specific commit and create a new commit: git revert <commit_hash> creates a new commit that undoes the changes introduced by the specified commit.

    git revert <commit_hash>
    
  27. Interactively stage changes for a commit: git add -p interactively stages changes, allowing you to select portions of a file to add to the next commit.

    git add -p
    
  28. Squash multiple commits into one: git rebase -i HEAD~n allows you to perform an interactive rebase, where you can modify, re-order, squash, or edit commits within the last "n" commits before the HEAD.

    git rebase -i HEAD~n
    
  29. Rewrite commit history (use with caution): git rebase -i <commit_hash> enables interactive rebase, allowing you to modify, re-order, squash, or edit commits from the specified commit onwards.

    git rebase -i <commit_hash>
    
  30. Create and apply a stash of changes: git stash temporarily saves changes. git stash apply retrieves saved changes.

    git stash
    git stash apply
    
  31. View stashed changes: git stash list provides a clear list of all saved stashes, showing their respective identifiers.

    git stash list
    
  32. Cherry-pick a specific commit to the current branch: git cherry-pick <commit_hash> copies the changes introduced by the specified commit and applies them as a new commit on the current branch.

    git cherry-pick <commit_hash>
    
  33. Show the commit history of a file: git log -- <file_name> shows the commit history for the specific file, displaying the changes made to that file over time.

    git log -- <file_name>
    
  34. Create a new branch based on a specific commit: git branch <branch_name> <commit_hash> creates a new branch with the given name at the specified commit. The new branch will start from that commit's state in the commit history.

    git branch <branch_name> <commit_hash>
    
  35. Update your local branch with the latest changes from the remote: git fetch retrieves new data from the remote repository without automatically merging it into your current branch.

    git fetch
    
  36. Rebase your local branch on top of the latest changes from the remote: git rebase origin/<branch_name> integrates changes from the specified branch on the remote repository (origin) into your current local branch, replaying your local commits on top of the updated remote branch.

    git rebase origin/<branch_name>
    
  37. Create an alias for a Git command: git config --global alias.<alias_name> <git_command> creates a global alias for a Git command, allowing you to use a custom shortcut (alias_name) for the specified Git command.

    git config --global alias.<alias_name> <git_command>
    
  38. Ignore files or directories from being tracked by Git: Create a .gitignore file and list the files or directories to ignore.

  39. Configure global Git settings: git config --global user.name "Your Name" sets your global Git username. git config --global user.email "your@email.com" sets your global Git email.

    git config --global user.name "Your Name"
    git config --global user.email "your@email.com"
    
  40. Manage submodules (when working with nested repositories): git submodule add <repository_url> adds a new submodule to your repository.

    git submodule init initializes all submodules that exist in your repository.

    git submodule update updates all submodules to the latest commits.

    git submodule add <repository_url>
    git submodule init
    git submodule update
    
  41. Cherry-pick changes from a specific file in another branch: git checkout <branch_name> -- <file_name> retrieves a specific file from the specified branch and replaces the local version of that file with the one from the branch.

    git checkout <branch_name> -- <file_name>
    

Conclusion:

This handbook of Git commands empowers you to navigate version control confidently, optimize your workflow, and streamline collaboration. Embrace the power of Git and revolutionize the way you build software. Happy coding!

Download cheatsheet: Cheat-sheet-link