Cheat Sheet of most used Git commands and how to use them.

 

1. **git init**: Initializes a new Git repository in the current directory.

$ git init
Initialized empty Git repository in /path/to/your/repository/.git/

 

2. **git clone**: Copies an existing Git repository from a remote server to your local machine.

$ git clone https://github.com/username/repository.git

3. **git add**: Adds files or changes to the staging area, preparing them for commit.

$ git add file1.txt # Add a specific file
$ git add directory/ # Add all files in a directory
$ git add . # Add all changes in the current directory

 

4. **git status**: Shows the current state of the repository, including tracked/untracked files and changes in the staging area.

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: file1.txt
modified: file2.txt

Untracked files:
(use "git add <file>..." to include in what will be committed)
file3.txt

 

5. **git diff**: Shows the differences between the working directory and the staging area.

$ git diff

 

6. **git diff --staged**: Shows the differences between the staging area and the last commit.

$ git diff --staged

 

7. **git commit**: Records the changes in the staging area to the repository.

$ git commit -m "Add new feature"

 

8. **git commit --amend**: Modifies the last commit by adding staged changes or changing the commit message.

$ git add forgotten_file.txt
$ git commit --amend

# This opens your text editor to modify the commit message

 

9. **git push**: Uploads local commits to the remote repository.

$ git push origin master

 

10. **git pull**: Downloads the latest changes from the remote repository to your local repository.

$ git pull origin master

 

11. **git fetch**: Downloads the latest changes from the remote repository without merging them.

$ git fetch origin

 

12. **git branch**: Lists all branches in the repository.

$ git branch
* master
feature-branch
development

 

13. **git branch <branch_name>**: Creates a new branch.

$ git branch new-feature

 

14. **git checkout**: Switches between branches or restores working tree files.

$ git checkout feature-branch # Switch to the "feature-branch"
$ git checkout -b new-branch # Create and switch to a new branch

 

15. **git merge**: Combines changes from one branch into another.

$ git checkout master # Switch to the branch you want to merge into (e.g., "master")
$ git merge feature-branch # Merge changes from "feature-branch" into "master"

 

16. **git remote**: Lists remote repositories.

$ git remote -v
origin https://github.com/username/repository.git (fetch)
origin https://github.com/username/repository.git (push)

 

17. **git remote add**: Adds a new remote repository.

$ git remote add upstream https://github.com/upstream_user/upstream_repository.git

 

18. **git remote remove**: Removes a remote repository.

$ git remote remove upstream

 

19. **git log**: Shows a history of commits.

$ git log
commit 53bdf43d31a073e5111ed71c0e39a460d9b9a3c2
Author: John Doe <This email address is being protected from spambots. You need JavaScript enabled to view it.>
Date: Tue Jul 20 12:34:56 2023 +0300

Add new feature

commit 2f7a2e5bf4c2d7c2e8fe0ed9838776373a6e5cc4
Author: Jane Smith <This email address is being protected from spambots. You need JavaScript enabled to view it.>
Date: Mon Jul 19 09:12:34 2023 +0300

Fix bug in login page

 

20. **git show**: Displays information about a commit.

$ git show 53bdf43d31a073e5111ed71c0e39a460d9b9a3c2

 

21. **git log --oneline**: Shows a concise history of commits.

$ git log --oneline
53bdf43 Add new feature
2f7a2e5 Fix bug in login page

 

22. **git revert**: Creates a new commit that undoes a previous commit.

$ git revert 53bdf43d31a073e5111ed71c0e39a460d9b9a3c2

 

23. **git reset**: Resets the repository to a previous commit, potentially discarding changes.

$ git reset --hard HEAD~2

 

24. **git tag**: Lists tags in the repository or creates a new tag.

$ git tag # List all tags
$ git tag v1.0 # Create a new tag "v1.0" for the current commit

 

25. **git show**: Displays information about a commit or a tag.

$ git show 53bdf43d31a073e5111ed71c0e39a460d9b9a3c2 # Show commit information
$ git show v1.0 # Show tag information

 

26. **git stash**: Temporarily stores changes that are not ready to be committed.

$ git stash

 

27. **git stash list**: Lists all stashed changes.

$ git stash list
stash@{0}: WIP on feature-branch: 2f7a2e5 Fix bug in login page

 

28. **git stash apply**: Applies the latest stashed changes.

$ git stash apply

 

29. **git config**: Sets or displays configuration options.

$ git config --global user.name "John Doe"
$ git config --global user.email "This email address is being protected from spambots. You need JavaScript enabled to view it."

 

These are some of the fundamental Git commands you can use in your day-to-day workflow. Git provides a rich set of features, so there are many other advanced commands and options available for various scenarios.