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!
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.
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 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 "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 . //stages new files and modifications, without deletions (on the current directory and its sub directories).
git add -A //stages all changes
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
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 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.
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
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 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 🐣