Git Cheat Sheet- 20 commands I Use Everyday
Tracking changes in my programs was always a problem when I first started coding, and...
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.
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.
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
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 "firstname.lastname@example.org"
This command turns a directory into an empty Git repo.
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
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 .
Records the change made for the files in a local repo.
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"
This command shows the commit history for the current repository.
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.
This command expects a commit message to explain why the file was deleted.
git rm filename
This command stages the changes, then it expects a commit message.
git mv oldfile newfile
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
When you want to use a different or a newly created branch you can use this command:
git checkout branch_name
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.
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
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
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.
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.
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 -f
You can transfer completed work from one branch to another using git-rebase.
git rebase branch_name_here
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!