What is Git?
Git is an open source, free VCS (Version Control System), which means it's a way to track each change made to your software.
More VCS exist, including CVS, SVN, and others. Git, however, is by far the most popular.
To keep track of the changes you make to the code, you can even utilise Git locally on your PC. This is a fantastic approach to keep track of certain items that degrade over time.
I need Git, but why?
However, the fact that it can be distributed—that is, that you may store your Git repository on a third-party platform like GitHub, GitLab, etc.—is significantly more significant.
By doing this, you may provide other developers, coworkers, and teammates the ability to work on projects asynchronously alongside you and view the changes you make.
Git is thus the ideal framework for developers to cooperate and work on a single codebase.
We'll also cover branching, merging, and all the other fancy phrases that Git uses to become even more potent in this series.
Use case for Git
Let's look at how things were before Git to wrap up the important.
Chris would be working on a significant project. Paul then expressed a desire to work on the system.
Chris would have to upload his system over an FTP, USB, or connection.
Paul would then begin working, but in the interim, Chris and Paul might have each made tiny modifications to the same file.
The files will be reshared once they are both finished, but which one is the correct one right now? Surely there will be some combo!
That's a serious issue, to be sure. This might have been avoided with the use of Git because Paul could have obtained the codebase from a distributed Git provider, and Git would have recorded all modifications.
Git would alert them to the conflicting lines when they needed to merge the code.
Thank you Git, you saved my life!
How do you set up Git?
Any machine can easily have Git installed. The following platforms' installation instructions for Git are listed below:
- OSX: brew install git
- Windows: Download the installer
- Linux (apt): sudo apt-get install git
- Linux (yum): sudo yum install git
Run the following command to see if Git was correctly installed once you complete these steps:
git --version
Git Workflow
What is GitHub?
You can host your Git repositories on the website GitHub. But it goes beyond that since it serves as a home for the code of millions of other developers.
On GitHub, many people conduct their whole open source community, using it as their only channel of communication.
GitHub is exceptional at organising team development, bugs, and automated actions.
In addition, it provides us with a fantastic visual overview of our code, branches, and pull requests.
There are four fundamental elements in the Git Workflow.
Working Directory, Staging Area, Local Repository and Remote Repository.
A file in your working directory has three different states that you can think of.
- It may be acted out. Consequently, the changed files are not yet committed to the local repository but are marked to be committed.
- It is modifiable. This indicates that the latest versions of the files are not yet present in the local repository.
- It could be done. Consequently, the modifications you make to your file are securely kept in the local repository.
- A file in the working directory can be added to the staging area using the git add command.
- The command git commit is used to add all staged files to the local repository.
- The git push command transfers all files that have been committed to the local repository to the remote repository. All files and modifications will therefore be accessible to anybody with access to the remote repository.
- Using the git fetch command, files can be transferred from a remote repository to a local one, but not to the working directory.
- To move files from the local repository into the working directory, use the git merge command.
- The git pull command is used to pull files straight into the working directory from a remote repository. It is the same as performing a git fetch and git merge.
Git config command
This command is used to set up git-related settings on your local computer or system. The key configurations and settings that were carried out by this command are listed below. These options can be put up either globally or locally.
NB: Your login and email address must match those on GitHub or any other service you use to save your code, such as Bitbucket.
Name (username) git config --global user.name "username" This command is used to create a username for the computer on a global level. Email (email address)
git config --global user.email "email address" This command is used to create an email address on the computer for the global user. Default Editor (Code Editor)
git config --global core.editor "code" This command sets the specified code editor as your default editor.
Initializing a repository
git init With this command, you can set up a fresh repository on your device or computer in the selected folder or any other folder you choose.
Git add command
This command can do a number of different activities that are flagged at the end. Let's take a closer look at the main 3 activities that this command carries out.
creating a new branch in the repository. There are 3 straightforward primary commands in this assignment itself that, depending on the path you choose, can assist you in achieving your objective. These are them.
git branch < new name > This command creates a new branch in the repository but keeps you at the current branch.
git checkout -b < new name > This command creates a new branch in the repository and takes you to the newly created branch.
git checkout -b < new name > < another name > This command creates a new branch in the repository from another branch.
Adding new changes (staging the changes)
git add < file.ext > This command adds changes to the specified file to the staging area in the repository.
git add . This command adds changes of all files in the repository to the staging area. You can also add a directory to a repo by using the command "git add < directory name >" Adding a remote repo
git remote add < alias name > < URL > This command is primarily used to add a remote repo.
Git commit command
You can submit (or commit, as you may like) all changes to the repository using the commit command. Its two primary applications are as follows:
- git commit -m < message > This command commits all staged changes to the repository. Keep in mind that the message should be a short description of what the snapshot represents.
- git commit -a This command commits all the local changes in tracked files.
Ignoring files
You can also choose not to track specific files in specific directories, such as log files.
< dir name >/* This command enables you to ignore all files in a specified directory. Where there is , you replace it with the name of the directory whose files you want to ignore.
*.< file extension > This command enables the .gitignore file to ignore all files with a specified extension. This must be added to the .gitignore file.
For example, * .css tells git ignore to ignore all CSS files.
.jxt tells git ignore to ignore all files in the JavaScript XML format.
Git rename command
With the help of this command, you can rename URLs, files, and branches that have flags appended to them.
git branch -m < new name of branch > This command allows you to rename the current working branch you are in.
git branch -m < old name of branch > < new name to give branch > This command allows renaming any branch that exits in the repo even though you may not at the moment, be working in that branch.
git mv file_from file_to This command allows renaming a file from its old name to the one you want to give it.
git remote rename < old name > < new name > This command allows you to rename the remote repo from its old name to the new one you want.
Git merge command
To maintain track of changes in any branch or file, the merge command allows merging of two or more files or branches.
git merge < branch name > This command allows you to merge another branch to the current branch.
git checkout < branch name > < path to file > --patch This command allows you to merge a single file from one branch to another.
Git is an open-source, free tool for tracking code changes. Git also enables asynchronous collaboration among developers working on the same code base. Git can be used locally, but a managed solution like GitHub, GitLab, etc. has more capabilities.
I hope you now understand the significance of Git.