Sep 15, 2022
Git is one of the most popular version control systems in the world and it’s no wonder that small and big projects rely on Git for version control. Knowledge of Git commands is a must-have thing to be a “true” developer and, of course, to be able to check and view all the changes in current projects.
The username is needed to link commits with the developer's name and it’s not the same as a GitHub username.
git config is a well-known command for setting or updating the developer’s username. In case you would like to hide your real name you can use any random text as your Git username, for example:
git config –global user.name “Boba Phat”.
git config can be used to update the email address also. After, the entered email address will automatically reflect in any future commits that push from the command line. Example:
git config –global user.email “firstname.lastname@example.org”.
git config –global credential.helper cache is a special command for this. Caching your login credentials help to avoid re-typing the username and password every time you perform a commit.
git init - this one is a command by which you can create or reinitialize an existing one. It will create a hidden directory upon initialization and this directory contains all the objects and references that Git uses and creates as a part of project’s history.
git add singlefile.js - this command is for adding one individual file. In case you need to add all files you should
use git add . then.
git status - line is used to view the status of the current repo, which includes the staged, unstaged, and untracked files.
git commit -m “your short comment about the commit” - by using this command you can add a single line message to your commit (commit parameter and -m flag are important to text in the command line)
git diff is a command for this. By adding to this line the –staged flag (
git diff –staged) you can view the staged changes. If you provide a filename as a parameter you will be able to see the changes of a specific file, for example:
git diff layout.js
The mv parameter is used to rename a file or a directory. This parameter requires a <source> and a <destination> - the source must exist and can be a file, or directory, and the destination must be an existing directory, example:
git mv dirname1/somefile.js dirname2
You can restore the unstaged working tree files by using the checkout parameter, example:
git checkout somefile.js
In case you need to restore staged working tree file you should use reset parameter, like in this example:
git reset HEAD somefile.js
But if you want to unstage all staged files you should simply do not provide the file path:
git reset HEAD
The branch parameter is using in this case, let’s see the example:
-a flag you can see the list of all remote branches:
git branch -a
If you need to remove a branch you need to use the branch parameter, the -d flag and the name of the branch, like in example beneath:
git branch -d existing_branch_name
In case a remote branch must be deleted then this command needs to be executed:
git push origin –delete existing_branch_name
git merge existing_branch_name will combine the specified branch into the main branch.
What if we need to merge a commit? Then it should be used the
git merge –no–ff existing_branch-name
It will not only merge the specified branch into the main branch but generate a merge commit - and this is important to document all merges that occur in repo.
git remote add someapp https://github .com/someurl - this line with the remote add parameter, short name and the url of the remote repository is adding a remote repository
git push origin main - this one is needed to enter in the command line. The above command will help to upload the local changes to a central repository so that other team members can view the changes you have made.
git rebase branch_name - it will change the base of your branch from one commit to another so it makes it appear as if you have created your branch from a different commit. Rebasing is a process to combine or move a sequence of commits to a new base commit.
This is a list of most-used git commands that almost every developer comes across in daily programming and Innovation Feel team isn't an exception 😉