Basic Git Commands you must know.
Git basics
In the present day, git is a name that we hear frequently. As a tech person, it will be a big lack if you have no idea about git. So let’s get to know some mostly used git commands. To get a start, let’s first get to know what git is.
What is git?
Git is simply an opensource distributed version controlling system. It is designed for coordinating work among the developers and for tracking the changes in the source code. Git promotes collaborative coding. Some goals of git are improving speed, enhancing data integrity, providing distribution support and promoting non-linear workflow.
What is version controlling and why so important?
A version control system is a kind of software tool that is designed to manage the source code by a group of people over time. A very special and an important feature in this systems is, it keeps track of each and every modification done in the source code. Therefore in case of a mistake or an issue, developers can rollback changes or can compare with previous versions and easily fix them. This is extremely important and useful in the field of software developments as this minimizes disruptions to the developers. The main benefits from a version controlling system are, the availability of a complete long-term change history of every file, branching and merging ability and traceability.
Basic git commands
- git init
This will create a local git repository in the current directory. See an example below.
First, go to a directory where you want to create a new project and open a command line and specify the path to the required directory. Run the command git init. This will create a local git repository in the current directory.
git init
Or you can create a project folder and a local git repository at the same time as follows.
git init <Project_folder_name>
2. git remote add origin <host-or-remoteURL>
This connects the local repository to a remote server repository. For that, you simply have to give the link to the server repository. You can get the repository link in GitHub as follows.
Go to the repository you want and copy the following URL.
Then type git remote add origin copiedURL_here.
git remote add origin <remote_repository_url>
#git remote add origin https://github.com/OWNER/REPOSITORY.git
3. git remote -v
This command will show you a list of all the repositories connected along with their URLs.
git remote -v
4. git remote rm <name>
This will remove the remote repository connection from your local repository.
First, check the connected repositories using the command git remote -v.
git remote -v origin https://github.com/OWNER/REPOSITORY.git (fetch)
origin https://github.com/OWNER/REPOSITORY.git (push)
destination https://github.com/FORKER/REPOSITORY.git (fetch)
destination https://github.com/FORKER/REPOSITORY.git (push)
After git remote rm destination,
git remote rm destination#Check whether it is removed
git remote -v
origin https://github.com/OWNER/REPOSITORY.git (fetch)
origin https://github.com/OWNER/REPOSITORY.git (push)
5. git clone <remote_repository_URL or local_repository_path>
This is used to copy a repository to the local machine. The repository can be remote or local.
#clone a remote repository
git clone https://github.com/OWNER/REPOSITORY.git
OR#clone a local repository
git clone local/path/Repository
6. git add
This will add new files or changes in the working directory to the staging area of your local repository. It tells that we want to update a particular file in the next commit. This will not affect the repository until you commit them.
git add <file>
The above command will add the changes in this single file to the staging area.
git add <directory>
Above will add changes in the whole directory to the staging area.
git add .
Above will add all the unstaged changes of the local repository to the staging area.
7. git commit –m “Message to go with the commit here”
This will save a snapshot of all the changes that were added to the staging area(with git add command) in the git repository. This will only save changes in the local repository and do not affect the remote repository.
git commit -m “commit message”
8. git status
This will show a list of changed files including the filled to be added for staging and files yet to be committed.
git status
Before adding files to be staged or before committing, you can use this command to check the status of the changed files.
Branches
9. git checkout
This will help you to create new branches in the repository and to navigate in between them.
git checkout -b <branch-name>
This will create a new branch and will automatically switch to that branch. That means, now you are working in this newly created branch.
git checkout <branch-name>
With this command, you can navigate to any other branch by simply typing the branch name you want to navigate in to.
10. git branch
git branch
This will list out all the branches present in the repository. And this will also show you the branch you are currently working in a different colour.
You can delete a branch simply with the following command.
git branch –d <branch-name>
11. git push
With this command, we can send the local commits to the defined branch of the remote repository.
git push origin <branch-name>
If we want to send the local commits to the master branch then replace the branch_name with master. You can send the changes to any desired branch by simply replacing master with the desired branch name.
git push origin master#push into the branch branch1 as follows
git push origin branch1
12. git pull
This will fetch and merge all the changes in the remote repository into the local working directory.
git pull
13. git diff
With git diff, we can list all the unmerged changes against files or branches of the remote repository.
#List down all the unmerged changes in all branches
git diff#List conflicts aginst the filename
git diff --base <filename>#List changes/conflicts between two branches
git diff <sourcebranch> <targetbranch>
14. git merge
With this command, we can integrate the contents of a branch into the active branch.
git merge <branch-name>
If you are in the master branch, with the above command you will merge the given branch into the master branch. As a good practice always remember to check the conflicts with git diff commands and resolve conflicts before merging them.
15. git config
This command is used to tell git who we are. With this, we can set user-specific configurations such as user name, email.
#Configure your global email adress.
git config --global user.email youremail@example.com#Configure your global user name.
git config --global user.name "Your_User_Name"
With the above commands, you can set a global email address and a global username that is common to be used with all your commits. The global flag tells that you are going to use that email and user name for all the repositories.
If you want to use different emails or user names in different repositories you can use the local flag.
#Configure your local email adress.
git config --local user.email youremail@example.com#Configure your local user name.
git config --local user.name "Your_User_Name"
Git tags
16. git log
We can get the commit id of the working branch using this command. Commit Id is the leading characters of the changeset ID, up to 10.This is unique. The commit ID will be useful in adding tags.
git log
17. git tags
By using the git tags you can mark a special changeset such as a new release.
git tag 1.0.0 <commitID>
We can send all the tags to the remote repository with the following command.
git push --tags origin
18. git reset
This is used to undo local changes done to a local repository. This is simply a rollback.
First, we can find the commit logs using git log command and then rollback using the commit id as follows.
git reset <commit_id>
Or else you can reset by giving a relative value as follows.
git reset current~2
#This will rollback the last two commits.
The following command will rollback all the uncommitted changes and bring the local repository to the last committed state.
git reset --hard HEAD
#overwrites any local changes you haven't committed
git reset --soft HEAD
#reset the pointer in the repository
git reset --mixed HEAD
#reset the pointer and the staging area
19. git revert
This is also same as the git reset command. But there is a slight difference. When revert command runs, it removes the uncommitted changes and at the same time add a commit at the end of the chain to “cancel” changes.
git revert HEAD
20. git stash
This will save the currently uncommitted changes temporarily. So that the user can just save the changes and commit later.
git stash
21. git show
This will show information about the git objects.
git show
22. git-cat-file
This will provide content or type and size information of repository objects.
git cat-file
23. git gc
This will clean up the unnecessary files and optimize the local repository. This deletes objects that are no longer part of any branch.
git gc
Well, these are some basic commands that are frequently used. But there are a lot more to know. Hope this will help you to have some quick guidance on your day today git works.
Good luck and happy coding!!!!!