Git Cheatsheet

Git Cheatsheet

This cheat sheet will help you understand/revise important and commandly used Git commands in a very easy way.

Git Basics

image.png

Here I have created a folder/directory Learning Git and 3 files 1,2,3 inside that folder. Using this example, we will learn all git commands. I am assuming that you have installed git and git bash and we will use git bash here to run all git commands.

1. git init

This command will initialize your new repository into your current directory. As you run this command, it will show this message.

Initialized empty Git repository in C:/Users/PRATYUSH/Desktop/Learning Git/.git/

2. git status

This command shows the status of the current repository including staged,unstaged, and untracked files.
Here I have created 3 files which is in upstaging area so it shows

image.png

3. git add filename_here

This command will add your file from local area to the staging area. Just replace the filename_here with your file name and it will add that file.

Here I am adding 1.txt file into my staging area using this command git add 1. This will do the above. After that, if I check the git status it will show

image.png

4. git add .

This command is a shortcut for adding all the files inside your folder to the staging area rather than writing each file name one by one.

After using this command, all files moved into the staging area and the git status will be

image.png

5. git add myfile*

This command is used for adding all the files whose name starts with myfile to your staging area.

5. git commit

This command will open the text editor to write a commit message for saving your changes in the staging area. To save your commit and close the editor press ESC then type :wq.

6. git commit -m "your message"

Using this command, we can directly add our files into the staging area with a commit message.

image.png

7. git commit -am"commit message here"

This command is the shortcut for git add and commit. Once you have made changes to your all files, then write this command rather than git add . and git commit -m "your message", this will add and save your changes into the staging area.

image.png

8. git log

This command shows the commit history of the current directory. It shows the SHA-1 key key of 40 characters like b3c1aebbe753f67a183f394d144394c8855005fc , author name , date modified and commit message. Press q to exit the log details. You can also try the git log -p and git log --oneline command to get brief details.

9. git show [SHA]

This command shows you the log details of the specific file using a unique SHA.

image.png

10. git diff

This command is used to track the changes made inside your unstaged files. To track changes in a specific file, mention its name after git diff.

image.png - before the line denotes no changes and + before the line shows changes made.

You also try git diff --staged to track changes in the staging area.

11 git add -p

This command opens a prompt and asks if you want to stage changes or not.

image.png

12 git rm filename

This command is used to remove the tracked files from a current working tree in git.

13 git mv oldfilename newfilename

This command is used to rename your file inside the current directory.

14. .gitignore

To ignore/hide your files in git, create a file name it .gitignore. Inside that, you can mention those files which need to be ignored or hidden.

15. git checkout filename

This will revert the unstaged changes of your file.

16. git reset HEAD filename

This will revert your staged changes in git.

17. git commit --amend

This command is used to modify and add changes in your most recent commit.

18. git branch

This command will list all the branches and * shows your current branch.

image.png

19. git branch branchname

This command is used to create a new branch in git. By default, you have already one branch named as main branch or master branch.

image.png

image.png

20. git checkout branch name

This command is used to switch from your current branch to the newly created branch.

image.png

21. git checkout -b branch_name

To create a new branch and switch immediately to that new branch, use this command.

22. git branch -d branch_name

This command is used to delete the branch. Make sure while deleting the branch, you should currently stay in a different branch.

23. git merge branch_name

To merge your current branch with the branch you want (branch_name), use this command.

24. git add remote repo_here

To add your remote repository to your local repository, use this command. Replace repo_here with your URL .

25. git push

To push all the changes into your remote repo, use this command.

26. git pull

When you want to see the changes made by your teammate, you can pull back the file into your local repo with this command.

27. git fetch

This command will download the changes from a remote repo but will not perform a merge on your local branch (as git pull does that instead).

28. git push -u origin branch_name

To push your branch from local to the remote repo, use this command.

Β