Git Cheat Sheet- 20 commands I Use Everyday

Tracking changes in my programs was always a problem when I first started coding, and...

Git Cheat Sheet- 20 commands I Use Everyday

Tracking changes in my programs was always a problem when I first started coding, and the danger of losing files was always present. But today we have Git to help us solve these issues; it offers everything you need to keep your development environment secure and simple. Git is essential for any programmer’s day-to-day work, especially when working in a group. It’s widely used in the software business, but understanding all of the commands takes time, so keep practicing.

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 as part of a team, git promotes coordination and management. On their local PC, each team member has full access to the code of the repositories. Every person may see all of the upgrades and adjustments. We can use BitBucket, GitHub, or GitLab to store all of our repositories on a single platform. There are a lot of commands in Git, so here are some of the most used ones.

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

2. Setup your Git username and Email Id:

There are numerous combinations and options to choose from. These settings are assigned via Git config. Username and user email are two important factors.

Name and Email address assigned to commit from local computer.

git config --global user.name "Tabassum"
git config --global user.email "tabassum@gmail.com"

3. Initialize a Git repo:

This command turns a directory into an empty Git repo.

git init

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

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 .

6. Commit changes in the editor:

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

git commit

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"

7. See your commit history:

This command shows the commit history for the current repository.

git log

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

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

10. Rename files:

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

git mv oldfile newfile

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

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

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

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

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

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

17. Cloning other repos:

Git clone is a command that allows you to obtain source code from a remote repository (like Github, for example). In other words, Git clone saves an exact copy of the most recent version of a project from a repository to your machine.

git clone

18. Pull changes to a remote repo:

You can use the git pull command to get updates from a remote repository. This command performs both git fetch and git merge operations, which means local modifications are updated and updates to remote repositories are posted.

git pull 

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

force push:

git push -f

20. How to use Git rebase:

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

git rebase branch_name_here

Conclusion

There are hundreds of git commands used by programmers, however, these are the most commonly used. I hope these commands assist you in increasing your Git productivity.

These commands can dramatically improve your productivity in Git. You don’t have to remember them all – that’s why I have written this cheat sheet. Bookmark this page for future reference or print it if you like.

Thanks for Reading!