Day 12: Git Cheat Sheet

Day 12: Git Cheat Sheet

Introduction

Git is a powerful version control system used by developers worldwide to manage their source code efficiently. Whether you're a beginner or an experienced developer, having a comprehensive understanding of Git commands is essential for effective collaboration and project management. This cheat sheet provides a detailed overview of the most commonly used Git commands, organized into categories for easy reference. From basic operations like initializing a repository to advanced techniques like rebasing and cherry-picking, this guide covers everything you need to know to become a Git pro.

Commands

Basic Operations:

  1. git init: Initialize a new Git repository.

  2. git clone [url]: Clone a repository from a remote server.

  3. git add [file]: Add file changes to the staging area.

  4. git commit -m "[message]": Commit staged changes with a descriptive message.

  5. git status: Display the current status of the repository.

  6. git log: View commit history.

  7. git diff [file]: Show changes made to a file.

Branch Management:

  1. git branch: List all branches.

  2. git branch [branch_name]: Create a new branch.

  3. git checkout [branch_name]: Switch to a different branch.

  4. git merge [branch]: Merge changes from a branch into the current branch.

  5. git branch -d [branch_name]: Delete a branch.

Remote Operations:

  1. git remote add origin [url]: Add a remote repository.

  2. git remote -v: List all remote repositories.

  3. git pull [remote] [branch]: Fetch changes from a remote repository and merge them.

  4. git push [remote] [branch]: Push changes to a remote repository.

Undoing Changes:

  1. git reset [file]: Unstage changes for a file.

  2. git checkout -- [file]: Discard changes made to a file.

  3. git revert [commit]: Create a new commit to undo changes made in a specific commit.

  4. git reset --hard [commit]: Reset the repository to a specific commit.

Advanced Operations:

  1. git rebase [branch]: Reapply commits from one branch onto another.

  2. git cherry-pick [commit]: Apply changes from a specific commit to the current branch.

  3. git stash: Temporarily store changes that are not ready to be committed.

  4. git bisect: Use binary search to find the commit that introduced a bug.

  5. git submodule: Manage external repositories as submodules within your own repository.

Miscellaneous:

  1. git config: Set configuration options.

  2. git tag: Create, list, delete, or verify a tag object signed with GPG.

  3. git show: Show various types of objects.

  4. git fetch: Download objects and refs from another repository.

  5. git merge --abort: Abort an ongoing merge.

  6. git log --oneline: Display commits in a condensed format.

  7. git remote rm [remote_name]: Remove a remote repository from the list.

  8. git clean -f: Remove untracked files from the working tree.

  9. git push --tags: Push all tags to the remote repository.

  10. git grep [search_term]: Search the working directory for lines matching a regular expression.

  11. git mv [old_file] [new_file]: Move or rename a file, directory, or symlink.

  12. git show-branch: Show branches and their commits.

  13. git rebase -i [branch]: Interactively rebase the current branch onto another.

  14. git describe: Output the most recent tag reachable from a commit.

  15. git pull --rebase: Fetch the latest changes from the remote repository and rebase the current branch.

Conclusion

This Git commands cheat sheet covers a wide range of functionalities, from basic operations like initializing a repository and committing changes to more advanced tasks like branching, managing remote repositories, and undoing changes. Mastering these commands will empower you to effectively manage version control in your projects and collaborate seamlessly with your team.