DEV Community

Cover image for Git Commands Cheat Sheet
sourav-ojha
sourav-ojha

Posted on • Originally published at sourav-ojha.hashnode.dev

Git Commands Cheat Sheet

Git Vocabulary

  1. Bare Repository

    • A repository that doesn’t have a working directory.
  2. Branch

    • An active area of development in Git. The newest commit displays the end of the branch.
  3. Blame

    • Refers to the most recent alteration to every line in the file. Displays Author, Revision, and Time.
  4. Commit

    • Record of a moment in Git history containing specifications of a changeset.
  5. Checkout

    • Process whereby a particular commit is chosen from the repository, and the condition of the file associated with it and the directory tree are reproduced in the working directory.
  6. Diff

    • The difference in changes between saved changes or two Commits.
  7. Detached Head

    • State in which a specific commit is checked out rather than a branch.
  8. Fetch

    • Retrieves the most recent changes in the branch and the local or remote repositories.
  9. Hash

    • Unique SHA1 code for each Commit.
  10. Head

    • Name of the Commit at the end of a Branch.
  11. Fork

    • When you Fork the repository, you can add Commits and add Pull Requests.
  12. Index

    • A group of files that hold state information.
  13. Merge

    • Includes changes from named commits (from when their histories split from the current branch) into the current branch.
  14. Master

    • Git’s default development Branch.
  15. Pull Request

    • Suggests changes to the Master Branch.
  16. Repository

    • A group of Commits, Branches, and Tags to identify Commits.
  17. Push

    • Pushes new changes once they’ve been committed.
  18. Working Tree

    • The directory of files that you are currently working on.
  19. Origin

    • Default Upstream Repository.

Git Setup

  1. Initialize an empty Git repository:

    git init
    
  2. Clone a repository (local or remote via HTTP/SSH):

    git clone [repo / URL]
    
  3. Clone the repository from GitHub to the project folder:

    git clone [repo / URL] [folder]
    

Git Configuration

  1. Configure the username to be used for all actions:

    git config --global user.name "[your_name]"
    
  2. Configure the email to be used for all actions:

    git config --global user.email "[email_address]"
    
  3. Create an alias for a Git command:

    git config --global alias.[alias_name] [git_command]
    
  4. Set a default text editor:

    git config --system core.editor [text_editor]
    
  5. Open Git’s global configuration file:

    git config --global --edit
    
  6. Enable the helpful colorization of command line outputs:

    git config --global color.ui auto
    

Managing Files

  1. List which files are staged, unstaged, and untracked:

    git status
    
  2. Show history of changes:

    git log
    
  3. List all commits from all branches:

    git log --all
    
  4. Compare two branches by showing which commits from the first branch are missing from the second branch:

    git log [branch1]..[branch2]
    
  5. To see the difference between the last commit and the working directory:

    git diff HEAD
    
  6. Display the content and metadata of an object (blob, tree, tag, or commit):

    git show [object]
    
  7. To see the difference between the working directory and the index:

    git diff
    
  8. To see the difference between the last commit and the index:

    git diff --cached
    

Git Branches

  1. List all branches in the repository:

    git branch
    
  2. Create a new branch:

    git branch [branch]
    
  3. List all branches - local and remote:

    git branch -a
    
  4. List all remote branches:

    git branch -aa
    
  5. Switch to a branch under a specified name:

    git checkout [branch]
    
  6. Rename a branch:

    git branch -m [new_branch_name]
    
  7. Delete a local branch:

    git branch -d [branch]
    
  8. Merge a branch:

    git merge [branch]
    

Making Changes

  1. Add the current changes of a file to staging:

    git add [file/directory]
    
  2. Add the entire directory changes to staging:

    git add .
    
  3. Commit the changes with a message:

    git commit -m "[descriptive_message]"
    

Rewriting History

  1. Replace the last commit with a combination of the staged changes and the last commit combined:

    git init
    
  2. Rebase the current branch with the specified base (It can be a branch name, reference to a head, tag, or a commit ID):

    git rebase [base]
    
  3. Lists changes made to the HEAD of the local repository:

    git reflog
    

Undoing Changes

  1. Undo all changes in the specified file/directory by creating a new commit and applying it to the current branch:

    git revert [file/directory]
    
  2. Unstage the specified file without overwriting changes:

    git reset [file]
    
  3. Undo all changes that happened after a specified commit:

    git reset [commit]
    
  4. See which files should be removed from the current directory:

    git clean -n
    
  5. Remove the unnecessary files in the directory:

    git clean -f
    

Remote Repositories

  1. Remove the unnecessary files in the directory:

    git remote add [name] [URL]
    
  2. Fetch a branch from a remote repository:

    git fetch [remote_repo] [branch]
    
  3. Fetch the specified repository and merge it with the local copy:

    git pull [remote_repo]
    
  4. Push a branch to a remote repository with all its commits and objects:

    git push [remote_repo] [branch]
    

In the fast-paced world of software development, mastering Git is an invaluable skill. With this Git Commands Cheat Sheet, you have a powerful resource at your fingertips to streamline your version control workflow, whether you're a beginner or an experienced developer. Git empowers collaboration, helps you manage your project history, and ensures code integrity.

Remember, Git is not just a tool; it's a journey. As you continue to explore its capabilities and best practices, you'll unlock new ways to work more efficiently and collaboratively with your team. So, keep this cheat sheet handy, and may your coding adventures be filled with success and innovation.

Happy coding! 🚀👨‍💻👩‍💻

Top comments (0)