What is Git
Git is a version control system that helps software developers to track changes in their projects and maintain a complete history of their work.
It allows developers to collaborate together on a project simultaneously.
Features of Git
- Free & Open Source
- It is completely free and open-source which means you can get its source code freely and you can make changes to it.
- It is a scalable tool which means it can work perfectly even if a huge number of users use it.
- It is a distributed version control system which means it allows us to clone the entire project (repository) in our local system. The clone that we create, contains the whole project with previous commits done by other developers.
- Git uses a secure hash function (SHA1), to name and identify objects within its database.
- It means that it is impossible to change files, dates, commit messages, and any other data from the Git database without knowing Git.
- It means git is capable of handling large repositories effectively.
- Git does not rely on the central server, i.e., there is no need to interact with the remote server for every operation.
- Branching and Merging
- Branching allows us to create an independent & similar copy of the current project (repository) for different usage requirements.
- It is widely used in bug fixing and in the addition of new features to an existing project.
- It allows us to divide the project into small modules (features) and each developer can easily work on a separate module and in the end, merge all the modules to the main branch or project.
- Staging Area
- It is an intermediate area where commits can be formatted and reviewed before completion.
It is a git repository hosting platform that allows us to upload our source codes, track changes, and collaboration with other developers.
How Git Works
Firstly, we create a folder or directory where we will keep our project files. That particular folder is referred to as the “working directory”.
Then, we initialize git so that it can track that working directory for changes. It means whatever changes are made in that folder, those will be tracked by git like creating a file, modifying it, deleting it, etc.
Even a single line of change will be tracked by git.
Then, as we complete a specific work in a file, then we can send it to the “staging area”.
You can consider the staging area as a “shopping cart”.
Like in a shopping cart, you keep all those items in a specific quantity that you want to purchase, and then after finalizing your cart items you go for payment.
The same applies here, i.e., as we finish coding in our file, we add it to the staging area which denotes that this file is being tracked and is staged in the staging area. You can remove it from the staging area, modify the file and rewrite it in the staging area, etc.
When all work is done and all files are ready in the staging area then we make a “commit“. You can consider commit as making payment for those items, so that they can be yours.
Commit means to “save changes to the local repository (.git folder)” or “make a checkpoint”.
When you make a commit, you create the first version of your project in your pc. Even if you have made changes to your project you can always return to a specific commit and all your files will be restored as it was when you made the commit.
You can push your local repository or project to a remote repository (online repository) and here Github makes its entry. A “remote repository” is an online folder on the server of a platform like GitHub, GitLab, bitbucket, etc. The benefit of a remote repository is that you can save your data on an online server and share or collaborate with many developers.
Configure Git for first use
Firstly, set the name and email of user:
git config --global user.name “Any name….” ↵
git config --global user.email “Any email….” ↵
Note: “–global” is used to set the name & email globally for all your git projects.
git config --list ↵ // It lists the configuration information.
Use the above command to check whether your name and email has been updated to use git.
Basic Git Commands
- It initializes git for that working directory so that git can track it
git init ↵
- It displays the state of the working directory and staging area.
- It tells which changes are staged & which haven’t.
- It also tells which files are not being tracked by git.
git status ↵
- It adds the files to the staging area.
git add filename.ext ↵ // for one file or git add filename1.ext filename2.ext ↵ // for more than one file or git add . ↵ // for all files
- It creates a checkpoint and saves changes made in the repository.
git commit -m ”your commit message” ↵ or git commit ↵ // opens an editor & will ask for commit message
- “-m” is used to add message while committing.
- A commit without “-m” will open a default editor & will ask for a commit message.
Press the “Esc” key, then “i” key ↵,
then type your message.
Press “Esc” key, then type “:wq” ↵,
to save & exit the editor.
- It lists all commits made to the repository in detail.
git log ↵
- It is used to modify the most recent commit.
- It can add newly staged files to the most recent commit without making a new commit.
- It can be used to change the commit message of the most recent commit.
git commit --amend -m ”your new commit message” ↵ or git commit --amend ↵ // opens an editor & will ask for new commit message
It is not a command but “.gitignore” is a file that contains the file names or a pattern to tell git which file or folder you don’t need to track.
*.log filename.log demo/ /* If above 3 lines are written in .gitignore file then, *.log - All File names with .log extension will be ignored. filename.log - Any file with name & extension `filename.log` will be ignored. demo/ - Demo folder will be ignored. */