Git Cheat Sheet – Git Commands You Should Know

What is a Distributed Version Control System?

Git Commands You Should Know

How to check your Git configuration
How to setup your Git username
How to setup your Git user email
How to cache your login credentials in Git
How to initialize a Git repo
H…


This content originally appeared on DEV Community and was authored by Abdullah

  1. What is a Distributed Version Control System?
  2. Git Commands You Should Know
    1. How to check your Git configuration
    2. How to setup your Git username
    3. How to setup your Git user email
    4. How to cache your login credentials in Git
    5. How to initialize a Git repo
    6. How to add a file to the staging area in Git
    7. How to add all files in the staging area in Git
    8. How to add only certain files to the staging area in Git
    9. How to check a repository’s status in Git
    10. How to commit changes in the editor in Git
    11. How to commit changes with a message in Git
    12. How to commit changes (and skip the staging area) in Git
    13. How to see your commit history in Git
    14. How to see your commit history including changes in Git
    15. How to see a specific commit in Git
    16. How to see log stats in Git
    17. How to see changes made before committing them using “diff” in Git
    18. How to see changes using “git add -p”
    19. How to remove tracked files from the current working tree in Git
    20. How to rename files in Git
    21. How to ignore files in Git
    22. How to revert unstaged changes in Git
    23. How to revert staged changes in Git
    24. How to amend the most recent commit in Git
    25. How to rollback the last commit in Git
    26. How to rollback an old commit in Git
    27. How to create a new branch in Git
    28. How to switch to a newly created branch in Git
    29. How to list branches in Git
    30. How to create a branch in Git and switch to it immediately
    31. How to delete a branch in Git
    32. How to merge two branches in Git
    33. How to show the commit log as a graph in Git
    34. How to show the commit log as a graph of all branches in Git
    35. How to abort a conflicting merge in Git
    36. How to add a remote repository in Git
    37. How to see remote URLs in Git
    38. How to get more info about a remote repo in Git
    39. How to push changes to a remote repo in Git
    40. How to pull changes from a remote repo in Git
    41. How to check remote branches that Git is tracking
    42. How to fetch remote repo changes in Git
    43. How to check the current commits log of a remote repo in Git
    44. How to merge a remote repo with your local repo in Git
    45. How to get the contents of remote branches in Git without automatically merging
    46. How to push a new branch to a remote repo in Git
    47. How to remove a remote branch in Git
    48. How to use Git rebase
    49. How to run rebase interactively in Git
    50. How to force a push request in Git
  3. Conclusion

What is a Distributed Version Control System?

A Distributed Version Control System (DVCS) is a type of version control where the complete codebase—including its full history—is mirrored on every developer's computer. This setup allows for more flexible workflows, including offline work, easier branching, and faster operations compared to centralized version control systems like SVN.

Git Commands You Should Know

Understanding and mastering Git commands is essential for effective version control. Here's a comprehensive guide to help you get started and become proficient with Git.

How to check your Git configuration

Before diving into Git commands, it's crucial to check your Git configuration to ensure everything is set up correctly.

git config --list

How to setup your Git username

Your Git username is used to label your commits with your identity. Set it up using:

git config --global user.name "Your Name"

How to setup your Git user email

Similar to the username, your email address is attached to your commits.

git config --global user.email "you@example.com"

How to cache your login credentials in Git

To avoid entering your credentials repeatedly, you can cache them.

git config --global credential.helper cache

How to initialize a Git repo

Start tracking your project with Git by initializing a repository.

git init

How to add a file to the staging area in Git

Prepare your changes for a commit by adding them to the staging area.

git add <file>

How to add all files in the staging area in Git

Add all your changes to the staging area at once.

git add .

How to add only certain files to the staging area in Git

Sometimes, you only want to stage specific files.

git add <file1> <file2>

How to check a repository’s status in Git

Check the status of your working directory and staging area.

git status

How to commit changes in the editor in Git

Commit your staged changes with an editor for the commit message.

git commit

How to commit changes with a message in Git

Commit your changes directly from the command line with a message.

git commit -m "Your commit message"

How to commit changes (and skip the staging area) in Git

You can commit changes directly without staging them first.

git commit -a -m "Your commit message"

How to see your commit history in Git

Review the history of your commits.

git log

How to see your commit history including changes in Git

View commit history along with the changes introduced.

git log -p

How to see a specific commit in Git

Inspect a particular commit in detail.

git show <commit-hash>

How to see log stats in Git

Get a concise summary of your commit history.

git log --stat

How to see changes made before committing them using “diff” in Git

Compare changes in your working directory.

git diff

How to see changes using “git add -p”

Interactively select changes to stage.

git add -p

How to remove tracked files from the current working tree in Git

Remove files from your working directory and staging area.

git rm <file>

How to rename files in Git

Rename and move files within your repository.

git mv <oldfile> <newfile>

How to ignore files in Git

Use a .gitignore file to exclude files from tracking.

# .gitignore
node_modules/
*.log

How to revert unstaged changes in Git

Discard changes in your working directory.

git checkout -- <file>

How to revert staged changes in Git

Unstage changes while retaining them in your working directory.

git reset <file>

How to amend the most recent commit in Git

Modify the last commit with new changes.

git commit --amend

How to rollback the last commit in Git

Undo the last commit while keeping changes in the working directory.

git reset --soft HEAD~1

How to rollback an old commit in Git

Revert to a specific commit.

git revert <commit-hash>

How to create a new branch in Git

Branching allows you to work on different features or fixes.

git branch <branch-name>

How to switch to a newly created branch in Git

Move to your new branch.

git checkout <branch-name>

How to list branches in Git

See all branches in your repository.

git branch

How to create a branch in Git and switch to it immediately

Create and switch to a new branch in one command.

git checkout -b <branch-name>

How to delete a branch in Git

Remove branches that you no longer need.

git branch -d <branch-name>

How to merge two branches in Git

Combine changes from different branches.

git merge <branch-name>

How to show the commit log as a graph in Git

Visualize your commit history as a graph.

git log --graph

How to show the commit log as a graph of all branches in Git

See the commit history graph for all branches.

git log --graph --all

How to abort a conflicting merge in Git

If a merge goes wrong, you can abort it.

git merge --abort

How to add a remote repository in Git

Link your local repository to a remote one.

git remote add origin <url>

How to see remote URLs in Git

List the URLs of your remote repositories.

git remote -v

How to get more info about a remote repo in Git

Detailed information about your remotes.

git remote show origin

How to push changes to a remote repo in Git

Upload your local changes to a remote repository.

git push origin <branch-name>

How to pull changes from a remote repo in Git

Fetch and merge changes from a remote repository.

git pull

How to check remote branches that Git is tracking

List the remote branches your local repository is tracking.

git branch -r

How to fetch remote repo changes in Git

Download changes from a remote repository without merging.

git fetch

How to check the current commits log of a remote repo in Git

View the commit log for a remote repository.

git log origin/<branch-name>

How to merge a remote repo with your local repo in Git

Combine changes from a remote repository into your local one.

git merge origin/<branch-name>

How to get the contents of remote branches in Git without automatically merging

Fetch remote branches without merging them.

git fetch origin

How to push a new branch to a remote repo in Git

Share your new branch with the remote repository.

git push origin <branch-name>

How to remove a remote branch in Git

Delete a branch from the remote repository.

git push origin --delete <branch-name>

How to use Git rebase

Rebase your current branch onto another branch.

git rebase <branch-name>

How to run rebase interactively in Git

Use interactive rebase for a more controlled process.

git rebase -i <commit-hash>

How to force a push request in Git

Forcefully push changes to a remote repository.

git push --force

Conclusion

Mastering Git commands empowers you to manage your codebase efficiently, collaborate with others, and maintain a robust history of your project's development. Practice these commands and explore more advanced features as you grow more comfortable with Git.


This content originally appeared on DEV Community and was authored by Abdullah


Print Share Comment Cite Upload Translate Updates
APA

Abdullah | Sciencx (2024-06-21T14:49:37+00:00) Git Cheat Sheet – Git Commands You Should Know. Retrieved from https://www.scien.cx/2024/06/21/git-cheat-sheet-git-commands-you-should-know/

MLA
" » Git Cheat Sheet – Git Commands You Should Know." Abdullah | Sciencx - Friday June 21, 2024, https://www.scien.cx/2024/06/21/git-cheat-sheet-git-commands-you-should-know/
HARVARD
Abdullah | Sciencx Friday June 21, 2024 » Git Cheat Sheet – Git Commands You Should Know., viewed ,<https://www.scien.cx/2024/06/21/git-cheat-sheet-git-commands-you-should-know/>
VANCOUVER
Abdullah | Sciencx - » Git Cheat Sheet – Git Commands You Should Know. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/06/21/git-cheat-sheet-git-commands-you-should-know/
CHICAGO
" » Git Cheat Sheet – Git Commands You Should Know." Abdullah | Sciencx - Accessed . https://www.scien.cx/2024/06/21/git-cheat-sheet-git-commands-you-should-know/
IEEE
" » Git Cheat Sheet – Git Commands You Should Know." Abdullah | Sciencx [Online]. Available: https://www.scien.cx/2024/06/21/git-cheat-sheet-git-commands-you-should-know/. [Accessed: ]
rf:citation
» Git Cheat Sheet – Git Commands You Should Know | Abdullah | Sciencx | https://www.scien.cx/2024/06/21/git-cheat-sheet-git-commands-you-should-know/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.