Learn Git tool in 15 mins Tutorials with examples
Git Tutorials Basics
Git
is an open-source and distributed version control tool to track changes in code or documents. Nowadays it is popular among open-source codebases. During application development, we have several questions.
- How do you track changes on a code base?
- How do multiple people work on the same code or module?
- How do you maintain several versions of the codebase?
- How do you roll back code changes to the previous version if any issue in production?
- How do multiple people merge the code?
- How is code released to production?
Answers to the above questions are the features of GIT.
In development, Developers write a codebase in their local system. Once everything is ready, the Commit code changes to a remote repository. Then other developers can get the latest code changes.
Git Features
Opensource It is free to use under a GPL license. You can download and install client and server track your code
Performance
is good. The developer does changes to the local environment. No need for the internet to work. When you are done with local changes, You will commit changes to the remote repository.Distributed
tool Multiple teams can work on the same code base.
There are three repositories.
The repository is a set of files and directories and the history of each file.
- Remote repository It is a code on a central repository or server where multiple users can work by cloning into the local repository.
Remote Repository hosts on a server that can be accessed by multiple teams across the intranet.
- Local repository: It is code on your local system which cloned from a remote repository. Each user has a copy of the codebase.
User work on local changes for his work.
- Staging area: It is a temporary area where the user adds the files to the repository. Files can be of different states like files can be of tracked, untracked, committed, and staged types.
Install and Setup
- on Windows
First, download the window installer from here🔗.click on the installer and follow the steps. Once done successfully, You’re able to access the git tool.
- On Ubuntu/Debian Installation is easy using the apt-get package. It will install the latest git command.
apt-get install git
- on Linux Use the yum command to install the git tool
yum install git
Once git is installed on your respective operating system. You can check the installation by using the version below. To check the Version of a git tool.
C:\>git --version
git version 2.15.0.windows.1
One-time Setup Once it is installed successfully, You need to do some global settings, and It is a one-time step. Please do this via the command line as below.
It needs to enable username and email initially
git config --global user.name "Kiran"
git config --global user.email "[email protected]"
Popular git commands
We will see various examples of the git command. These are frequently used commands by the developer.
How to initialize an empty repository
It is starting command for new project creation in git.
git init command
initializes the git empty directory. It creates the .git folder in the empty directory.
C:\gitcommands>git init
Initialized empty Git repository in C:/gitcommands/.git/
How to add files to the empty repository
It adds files to the staging area/working tree. It is a temporary area for keeping files before committing changes to the remote repository.
git add {list of files or directory by space}
How to Commit files in a git repository?
Once files are added, you need to use this command to commit changes to the local repository. Provides user message with -m option
git commit -m "Message"
get repository status
Using the git status
command, You can get each of the file statuses, files status are added
, Modified
etc.
git status
How to create a branch in the git repository?
The branch is a separate repository from the main repository where multiple teams work on.
git branch branchname // Create a new branch
git branch -a list of all branches
git branch -d delete branch
How to clone a git repository?
the git clone
is used to get a copy of the remote repository into your local repository. It is just a copy of your remote repository application.
git clone {gitrepourl} {folder}
gitrepourl
is the git repository URL. the folder is optional and takes the repository name as a folder if the folder is not provided.
git configuration list example
To find a list of available configurations.
C:\gitcommands>git config --list
core.symlinks=false
core.autocrlf=true
core.fscache=true
color.diff=auto
color.status=auto
color.branch=auto
color.interactive=true
help.format=html
http.sslcainfo=C:/Program Files (x86)/Git/mingw32/ssl/certs/ca-bundle.crt
diff.astextplain.textconv=astextplain
rebase.autosquash=true
http.sslcainfo=C:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt
http.sslbackend=openssl
diff.astextplain.textconv=astextplain
credential.helper=manager
user.email=[email protected]
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.symlinks=false
core.ignorecase=true
How to get global settings
git config --global --list
git Commit history log
Every commit task you do locally will record changes .git folder. You can see history using the below command.
git log
gitignore file
In your project, It is good practice to have the .gitignore file.
In code, the code compiles and generates temporary folders like a target
in the maven java project, and a dist
folder for JavaScript applications.
These folders are not required to commit to the repository. In that case, Please add this folder to a .gitignore file. These folder changes will not be committed as well not be shown with the git status command. Each line in this file represents a folder or file path to an application.
cat .gitignore
/target/*
How to checkout branch in git?
git checkout
is used to work with multiple branches for the same repository. Multiple branches are used by multiple developers for the same repository.
git checkout revisionid - check the changes of commit id
git checkout branchname - checkout existing branch
git checkout -b branchname - Create and checkout new branch
git remote origin
`git remote command is used to connect between remote repository with the local repository for names reflecting. Origin is the name of the original branch
git remote add origin {remote-name} {git-url}
B:\Workspace\angularspree>git remote -v
origin https://github.com/aviabird/angularspree.git (fetch)
origin https://github.com/aviabird/angularspree.git (push)
How to pull the latest changes in the git repository?
git pull
is used to get the latest changes of a remote repository. Once this command is fired. It gets the content from the remote repository to the local repository.
git pull branchname remote-url
git pull origin master
How to push local files to the remote repository?
It is the reverse of the git pull command
command. The git push
command pushes changes from the local to the remote repository. It is the final step once you add/commit changes.
git push origin master
Conclusion
To summarize, You learned the basics of git tool installation and learned frequently used commands.