What is Git ?
Git is a version control system that tracks file changes. Using Git allows you to keep a record of all changes and return to specific versions as needed. It is simple to use, takes up little space, and is extremely productive. Its branching model distinguishes it from nearly every other SCM available. The ability to merge changes from multiple people into a single source is what makes Git so simple. You can use GitHub or other online hosts to store backups of your files as well as their revision history.
Git’s main components
To me, Git is a wonderful tool to use in team projects because it helps to avoid confusion in code and brings a simple yet effective system to work. Here, I’d like to cover up the main components of Git:
Repository
A Git repository (or simply repo) contains all of the project files as well as the entire revision history. You'll take an ordinary folder of files (such as the root folder of a website) and tell Git to turn it into a repository. This creates a .git subfolder in which all of the Git metadata for tracking changes is stored. Simply put, a repository is a place where you keep your code.
Commit
To add new code to the repository, you need to make acommit, which is a snapshot of your repository at a particular point in time. commits a specific change, or series of changes, to a file in the repository. Git's history is made up of successive commits.
Branch
A branch is used to store your changes until they are ready. While the main branch (master) remains stable, you can work on a branch. When you're finished, you can merge it with the master. The great advantage is that you can have a few branches in one repository and merge them whenever you need.
Pull requests
This is a technique used in Git for discussing changes before they are merged into your codebase. A pull request is more than just a notification; it's a dedicated discussion forum for the proposed feature. This is especially convenient when several people are working on the same code, allowing developers to check each other's work.
Now that we have briefly discussed the main theoretical Git components, I want to list few basic Git commands that every developer must know before starting to work with Git.
Below is the example of the follows:->
git init: Initializes a new Git repository in the current directory.
git init
git clone: Copies an existing Git repository from a remote server to your local machine.
git clone <https://name-of-the-repository-link>
git add: Adds changes to the staging area, ready to be committed.
git add <file or directory name>
git commit: Commits changes to the repository, creating a new version of the code with a commit message.
git commit -m " Commit Message"
git status: Shows the status of the current repository, including which files have been changed, added, or deleted.
git status
git branch: Shows a list of all branches in the repository, or creates a new branch.
git branch <branch-name>
git checkout: Switches to a different branch, or creates a new branch and switches to it.
git checkout <branch-name>
git merge: Combines changes from one branch into another branch.
git merge <branch-name>
git push: Pushes committed changes from your local repository to a remote repository.
git push [variable name] master
git pull: Pulls changes from a remote repository to your local repository.
git pull <remote>
git log: Shows a list of all commits in the repository, including the author, date, and commit message.
git log # show commits
git diff: Shows the differences between two versions of a file, or between the working directory and the most recent commit.
Just specify your local branch1 or remote origin/branch2
git diff branch1 origin/branch2 #show diff referenced from second branch
git diff branch1 branch2 file.txt #show diff only between file.txt
So far this is my first post, these are the main commands that everyone who works with Git must know. In fact, Git is extremely easy to use, and the number of commands is quite large. But to remember these commands is not a tough task—you simply need to start working with Git, and most of the commands will be remembered intuitively.
Top comments (0)