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!
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
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"
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 . //stages new files and modifications, without deletions (on the current directory and its sub directories).
git add -A //stages all changes
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 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
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
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 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 π£
Top comments (11)
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 ofgit 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.
Thank you so much Naman for sharing this!
Very good!
Thanks for sharing this kind of knowledge β€οΈ
Very Good
Hmm, are you sure you use all these commands 'everyday'..?
Yes
Is it git remote add or git add remote. Correct me if I am wrong with this. Thank you
GIT ADD REMOTE
Git it π₯
π₯π₯