Learn_Git_Command_with_Practical_Examples_on_Linux
Learn_Git_Command_with_Practical_Examples_on_Linux
In this tutorial we’ll get familiar with Git. This will be hands on guide.
This tutorial assumes that Git is installed on your system.
User identity
In Git we can specify user identity by providing its name and e-mail
address. This information will be used during each commit operation.
Execute below command in terminal to assign identity:
Editor
This setting configures editor, which will be used while providing commit
message:
Compression
This setting configures compression level to be used. Valid range for
compression is -1 to 9. -1 value indicates zlib compression and is default
compression level. 0 value means no compression, and 1 to 9 are various
speed/size tradeoffs, 9 being slowest.
$ git config --global core.compression 2
Diff tool
This setting configures diff viewer tool. For example, below command
configures vimdiff as a diff tool:
$ git config -l
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
core.editor=vim
core.compression=2
user.name=Linuxtechi User
user.email=linuxtechiuser@linuxtechi.com
diff.tool=vimdiff
Git repositories
Repository is a location where source code is stored. We can either
create new repository or use existing repository. To create new empty
repository execute below command:
$ mkdir my-repo.git
$ cd my-repo.git
$ git init
This method will be useful when you are starting with new project.
Another method is to use existing repository. Such a repository is hosted
on remote server like GitHub. To download remote repository use clone
command as follows:
Git workflow
In this section we’ll discuss git workflow.
$ git status -s
M README
?? AUTHORS
$ git status -s
A AUTHORS
M README
Above output shows A before AUTHORS which indicates that this file is
newly added under Git. We can add any number of files using this
command.
$ git status -s
M README
?? AUTHORS
Commit changes
In Git, files which are part of changeset will form a commit. Each commit
will get unique ID. Let us create changeset first
Review changes
In this section we’ll discuss commands which will allow us to review
repository changes.
$ git log
View commit
Commit ID is associated with each changeset. We can use this ID with
show command to view commit contents.
index 0000000..e69de29
--- a/README
+++ b/README
@@ -1 +1,2 @@
Hello World!
+New Contents
View diff
Diff command allows us to review changes before creating changeset.
Diff command shows the differences between repository and local
workspace. Let us modify README file and view differences
$ git diff
--- a/README
+++ b/README
@@ -1,2 +1,3 @@
Hello World!
New Contents
+Generating diff
In above command:
$ git push
$ git pull
$ git log
If you observe above output carefully then we can see new commit
message, its ID and new timestamp.
To remove all above untracked file use clean command with -f option as
follows:
$ git clean -f
Removing delete-me-1
Removing delete-me-2
Removing delete-me-3
Please note that this command will remove files permanently hence use it
with caution.
In above command:
$ git diff
Above command will not show any difference as file is move to staging
area. Let us use –staged operation to view differences:
--- a/README
+++ b/README
@@ -1,3 +1,4 @@
Hello World!
New changes
Conclusion
In this tutorial we discussed basic operations of Git with simple
examples. This tutorial is good starting point for Git newbies. Shortly we
are coming with Part 2…