Git is a popular version control tool. There are other version control tools such as SVN, Mercurial. But Git is much more popular and very widely used. Git allows programmers to easily maintain projects and collaborate with other software developers. There are many services that provide remote server to host Git repositories. Some of them are Github, Gitlab, Bitbucket etc.
In this article, I would like to share some of the common Git commands that are used frequently.
Initializing a git repository. Run the following command in the project folder.
git initAdding your name in global configuration. You need to do this only once.
git config --global user.name "abdullah"Adding your email in global configuration. You need to do this only once.
git config --global user.email "contact@abdullahhassan.dev"Adding a file into the staging area. For example, assume the file is index.html
git add index.htmlAdding all changed files into the staging area.
git add .Add a new commit. Imagine commit as a milestone in a project. Commit messages should be meaningful and describe what you have done in the project. Some companies have specific format for writing commit messages. There are best practice guides available online too.
git commit -m "added a new hero section in index.html"Change the default branch name. By default, git names the default branch as master. However, you can change it with the following command. Let’s say, we change it to main.
git branch -M mainCreate a new branch named abdullah-v1. However, this command does not immediately switch to the newly created branch.
git branch abdullah-v1Switch to the new branch named abdullah-v1
git checkout abdullah-v1We can do the preceding two tasks that is creating a new branch named abdullah-v1 and immediately switching to it with a single command.
git checkout -b abdullah-v1Let’s say, you have opened a Github account and created a blank repository there. Now, you want to push your local code to the Github repository. We need to add the remote repository URL to do that. If you are using the ssh URL then you need to set up SSH key first. You can use the https URL without setting up any ssh key. But, you need to enter your password every time you push your code to the remote repository.
git remote add origin https://github.com/example-urlIf you need to change the remote URL use the following command.
git remote set-url origin https://github.com/example-urlYou can also add multiple URL using different tag names. Tag name can be anything you want.
git remote add server2 https://github.com/example-urlList all remote URLs.
git remote -vWe can push our commits to the remote repository with the following command. Here, we are pushing the code from the branch we are currently in to the remote URL with tag origin and in the remote branch named main.
git push origin mainWe can also pull code from the remote repository. The following command pulls code from the remote URL with tag origin and the remote branch abdullah-v1. It will create a local branch with the same name if it does not already exist or update the existing branch. We can then checkout to the pulled branch.
git pull origin abdullah-v1Temporarily remove changes from work tree and store it in a stack.
git stashRestore changes from the stack.
git stash popReset your work tree to the last commit. The following command does soft reset which is it removes the code changes but does not necessarily modify the work tree.
git resetCompletely hard reset to the last commit including work tree.
git reset --hardLet’s say, we have two branches abdullah-v1 and main. We want to merge abdullah-v1 into main. First, we need checkout to main. Then we write the following command.
git merge abdullah-v1Merge conflict may arise and will be marked in your code. Just remove the unwanted code and keep the right one. Then try to merge again.
Check all commits.
git logThere are many more commands that I have left out. The above commands are what we mostly need to get started with Git and those I use daily. If you want a nice cheat-sheet made by the wonderful people at Github, please hit me up with an email. Thanks!😊
