DEV Community

Cover image for Git Cheat Sheet- 20 commands I Use Everyday
Tabassum Khanum
Tabassum Khanum

Posted on • Updated on

Git Cheat Sheet- 20 commands I Use Everyday

Hey Coders!
When I started coding, tracking changes in my codes were always painful and the fear of losing files was always there. But now we have Git to tackle these problems, it has everything you need to make your coding environment safe and easy. Git is so important for every programmer's daily life especially when you are working with a team. The software industry widely uses it but mastering all the commands can take time, so keep practising!

gif

Why Git?

Git is a Distributed Version Control System that helps us to keep track of changes we've made to files in our projects.

Now you can easily revert to a previous version of our code if anything goes wrong, which is already there in our local machine.

When working on a team git makes collaborations and management easy. Every team member has full access to the code of the repositories on their local machine. All the changes and upgrades can be witnessed by every person. With the help of BitBucket, GitHub or GitLab we can store all the repos in a single platform. Git has many different commands, so below are some of the most often used commands.

1. How to check your Git configuration:

The git config command is a convenience function that is used to set Git configuration values on a global or local project level.

git config -l
Enter fullscreen mode Exit fullscreen mode

2. Setup your Git username and Email Id:

There are many configurations and settings possible. Git config is how to assign these settings.2 important settings are username and user email.

Name and Email address assigned to commit from local computer.

git config --global user.name "Tabassum"
git config --global user.email "tabassum@gmail.com"
Enter fullscreen mode Exit fullscreen mode

3. Initialize a Git repo:

This command turns a directory into an empty Git repo.

git init
Enter fullscreen mode Exit fullscreen mode

4. Add a file to the staging area in Git:

The command below will add a file to the staging area. Just replace filename_here with the name of the file you want to add to the staging area.

git add filename_here
Enter fullscreen mode Exit fullscreen mode

5. Add all files in the staging area in Git:

If you want to add all files in your project to the staging area, you can use a wildcard. and every file will be added for you.

git add . //stages new files and modifications, without deletions (on the current directory and its sub directories).
Enter fullscreen mode Exit fullscreen mode
git add -A //stages all changes
Enter fullscreen mode Exit fullscreen mode

6. Commit changes in the editor:

Records the change made for the files in a local repo.

git commit
Enter fullscreen mode Exit fullscreen mode

You can add a commit message without opening the editor. This command lets you only specify a short summary for your commit message.

git commit -m "first commit"
Enter fullscreen mode Exit fullscreen mode

7. See your commit history:

This command shows the commit history for the current repository.

git log
Enter fullscreen mode Exit fullscreen mode

8. Git status:

This command returns the current status of the repo. If a file is in the staging area, but not committed, it shows, with git status.

git status
Enter fullscreen mode Exit fullscreen mode

9. Remove tracked files from the current working tree:

This command expects a commit message to explain why the file was deleted.

git rm filename
Enter fullscreen mode Exit fullscreen mode

10. Rename files:

This command stages the changes, then it expects a commit message.

git mv oldfile newfile
Enter fullscreen mode Exit fullscreen mode

11. Create a new branch:

By default, you have one branch, the main branch. With this command, you can create a new branch. Git won't switch to it automatically – you will need to do it manually with the next command.

git branch branch_name
Enter fullscreen mode Exit fullscreen mode

12. Switch to a newly created branch:

When you want to use a different or a newly created branch you can use this command:

git checkout branch_name
Enter fullscreen mode Exit fullscreen mode

13. List branches:

You can view all created branches using the git branch command. It will show a list of all branches and mark the current branch with an asterisk and highlight it in green.

git branch
Enter fullscreen mode Exit fullscreen mode

14. create a branch in Git and switch to it immediately:

In a single command, you can create and switch to a new branch right away.

git checkout -b branch_name
Enter fullscreen mode Exit fullscreen mode

15. Merge two branches:

To merge the history of the branch you are currently in with the branch_name, you will need to use the command below:

git merge branch_name
Enter fullscreen mode Exit fullscreen mode

16. Add a remote repository in Git:

This command adds a remote repository to your local repository (just replace https://repo_here with your remote repo URL).

git add remote https://repo_here
Enter fullscreen mode Exit fullscreen mode

17. Cloning other repos:

Git clone is a command for downloading existing source code from a remote repository (like Github, for example). In other words, Git clone basically makes an identical copy of the latest version of a project in a repository and saves it to your computer.

git clone
Enter fullscreen mode Exit fullscreen mode

18. Pull changes to a remote repo:

The git pull command allows you to download updates from a remote repository. Using this command, you execute both git fetch and git merge operations, which means local changes are updated and updates are uploaded to remote repositories

git pull 
Enter fullscreen mode Exit fullscreen mode

19. Push changes to a remote repo:

After committing your changes, the next thing you want to do is send your changes to the remote server. Git push uploads your commits to the remote repository.

git push
Enter fullscreen mode Exit fullscreen mode

force push:

git push -f
Enter fullscreen mode Exit fullscreen mode

20. How to use Git rebase:

You can transfer completed work from one branch to another using git-rebase.

git rebase branch_name_here
Enter fullscreen mode Exit fullscreen mode

Conclusion

There are still hundreds of git commands that coders use but these were the frequently used ones. I hope these commands help to improve your productivity in Git.

Thanks for Reading!
Happy Coding 🐣

gif2

Top comments (11)

Collapse
 
namantam1 profile image
Naman Tamrakar

Very good article but I want to point out one thing that for adding the files in the staging area one must use git add -A instead of git add . as the later command stage only files that are in curent working directory or its subdirectory but not its parent directory files if you are running command from any sub directory of your project.
Read more here.

Collapse
 
codewithtee profile image
Tabassum Khanum

Thank you so much Naman for sharing this!

Collapse
 
shadowruge profile image
izaias

Very good!

Collapse
 
vishal8236 profile image
vishal patidar

Thanks for sharing this kind of knowledge ❀️

Collapse
 
007_dark_shadow profile image
Asif Vora

Very Good

Collapse
 
yashank09 profile image
Yashank Varshney

Hmm, are you sure you use all these commands 'everyday'..?

Collapse
 
vrrajasekaran profile image
vrrajasekaran

Yes

Collapse
 
rohithv07 profile image
Rohith V

Is it git remote add or git add remote. Correct me if I am wrong with this. Thank you

Collapse
 
vrrajasekaran profile image
vrrajasekaran

GIT ADD REMOTE

Collapse
 
andrewbaisden profile image
Andrew Baisden

Git it πŸ”₯

Collapse
 
codewithtee profile image
Tabassum Khanum

πŸ”₯πŸ”₯