Basic Git Commands – Part 1
Tracking the source code during the development is a tiresome job. Git is a software which helps us in this area. It is a free open source Version Control System. It helps developers to coordinate the source management and saves a lot of time for maintaining it. Git not only helps to keep track of changes made to file, in addition we can revert to previous versions when needed and much more. In this section of Basic Git Commands – Part 1, we will see some of the basic git commands that come in use very frequently.
Basic Git Commands – Part 1
git init
We can use the git init
command to create a new empty git repository or to convert an existing project into a git project in your local. It will create a .git directory in the current working directory which contains all the metadata like refs/heads, objects, refs/tags etc.
$ git init
Initialized empty Git repository in D:/git-basic/first/.git/
However ‘ D:/git-basic/first ‘ is the local path where we have initialized the project.
git clone
We can use the git clone
command to clone a remote repository to a new local repository. This will make the repo available in our local system. Technically this will act as a remote repository URL for our project.
git clone <URL>
$ git clone https://github.com/programeasily/git-basic.git
Cloning into 'git-basic'...
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 6 (delta 0), reused 6 (delta 0), pack-reused 0
Receiving objects: 100% (6/6), done.
git status
It describes the status of the current working directory. git status
shows state of current working directory and staging area. It shows files that are not tracked by git, changes that are staged , which are not staged and deleted files.
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
programEasily.java
nothing added to commit but untracked files present (use "git add" to track)
git stash
git stash
removes the uncommitted changes in a working directory and makes the directory clean. When one has to switch branches we can use git stash. It cleans the working directory and makes a head point to the last commit.
$ git stash
Saved working directory and index state WIP on master: 9a2ef8f hii
git branch
git branch
command create a new branch from the master branch, delete an existing branch and list the branches. Let us see one by one.
create new branch
Firstly, one can use the git branch <newBranchName>
command to create a new branch. Branching is the integral part of the Git version control mechanism. New branch can be created with any branch as reference. The default branch in Git is master.
git branch git-basic-ver1
delete branch
Secondly, we can use git branch -d <branchName>
to remove the branch locally. This will remove the branch only if the changes are committed and pushed to remote repo.
$ git branch -d git-basic-ver1
Deleted branch git-basic-ver1 (was 9a2ef8f).
If your branch contains commits that have not been merged or pushed to remote repo one need to use git branch -D <branchName>
. This will force the branch deletion.
list branch
View the local branches in your repository using the git branch
.
$ git branch
git-basic-ver1
* master
To show the branches in a remote Git repository, one need to use git branch -r
.
$ git branch -r
origin/master
To list the local and remote branches to a repository
$ git branch -a
git-basic-ver1
* master
remotes/origin/master
git checkout
We can use the checkout command to switch from the current active branch to another. While we switch the branch, it may ask to stage and the staged files, commits might be lost.
git checkout <branchname>
$ git checkout git-basic-ver1
Switched to branch 'git-basic-ver1'
One can also create and switch to a new branch using the git checkout command.
$ git checkout -b git-basic-ver2
Switched to a new branch 'git-basic-ver2'
Here It will add untracked files to the new branch unless there is no conflict.
git fetch
git fetch
operation will download new data (commits, refs, branches etc) from remote repository to local repository. A fetch command will never make any change to the local repository. It just provides a view of changes made in remote repo.
$ git fetch origin
git pull
git pull
does the function of fetch command and additionally integrates the downloaded new data from remote repo with the local repository. git pull
should be performed in a clean repo with no uncommitted changes. Since it integrates remote repo changes with local directory merge conflicts may occur which should be taken care.
In the other words, we can say git pull
can refer to the combination of git fetch
followed by a git merge
.
$ git pull
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.
git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=<remote>/<branch> git-basic-ver2
git version
In order to check the current version of git, you can use git --version
command.
$ git --version
git version 2.30.0.windows.2
git add
In order to add the changes from the local workspace to the staging area, we can use the git add
command. It prepares the changes for the next commit. It does not make changes to the remote repository. These changes can include new, existing or deleted files and directories.
git add <filePath>
With the git add
command you have to specify the file or directory path one needs to add to the staging area. You can add single as well as multiple paths.
$ git add basic/programEasily.java
git commit
This command commits the changes added in the staging area. One uses immediately after the git add
command. It can be done with a commit message which helps to understand what this commit is all about easily in future.
git commit -m “message”
Note that, It will records every commit in the master branch.
$ git commit -m 'ver1 code'
[git-basic-ver1 e95c217] ver1 code
1 file changed, 1 insertion(+)
This git command does not commit the changes to the remote repository, but adds to the local one.
git push
git push
command moves the local commit changes to the remote repository. This will make the commits recorded in the master branch and changes can be used by remote the repository users.
git push <branchName>
You should be cautious while pushing the changes to the remote repository because it will overwrite the previous contents.
$ git push Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 4 threads Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 336 bytes | 168.00 KiB/s, done. Total 4 (delta 0),
reused 0 (delta 0), pack-reused 0
To https://github.com/programeasily/git-basic.git 9261bc4..e95c217 git-basic-ver1 -> git-basic-ver1
git amend
We can use the git amend
command to edit the recent commit. It adds new changes with the commit in the staged area and in addition it will create a new commit replacing the old one.
git commit –amend ‘message’
$ git commit --amend -m 'changes'
[git-basic-ver1 6439b3e] changes
Date: Mon Jun 14 11:37:08 2021 +0530
1 file changed, 1 insertion(+), 1 deletion(-)
git diff
It compares and shows differences between commits, branches or changes made in the working directory since the last commit etc.
git diff – shows changes done in the working directory since the last commit.
$ git diff
diff --git a/basic/programEasily.java b/basic/programEasily.java
index 33545ed..b175a51 100644
--- a/basic/programEasily.java
+++ b/basic/programEasily.java
@@ -1 +1 @@
-sample1
+This is a sample file.
\ No newline at end of file
git reset –hard
Removes the changes in the operated repository. In other words, it will remove the changes in the working directory of the staging index and roll back to the previous commit. Since, it is the most unsafe reset, once performed cannot be undone.
$ git reset --hard
HEAD is now at 6439b3e changes
set username and email
git config –global user.name
it shows the username for your git repository installed in your system. Any change or commit you perform will be recorded in this name.
$ git config –global user.name rachel
git config –global user.email
It helps to view the git user email recorded in your system.
$ git config –global user.email rachel@gmail.com
In addition, if one has to change the email use ,
$ git config –global user.email <email>
One can change the username and email for a particular project by removing –global from above commands.
Summary of Basic Git Commands – Part 1
- In conclusion of Basic Git Commands – Part 1, we have seen some of the most commonly used git commands. We will be using these commands commonly in the daily life of a developer.
- In the coming sections we will see about some other useful git commands.