Git
A small definition of Git: it’s a tool that helps developers keep track of changes in their code!
Imagine you’re working on a document, and you want to save different versions as you make changes. In a typical situation, you might create separate files like “document1,” “document2,” and so on. However, with Git, you can save these changes more efficiently using specific commands that will be explained later on.
You may be confused or find yourself mixing up Git and GitHub. As mentioned earlier, Git is a tool that assists in tracking changes in your source code during software development. It helps manage collaboration among multiple developers and maintains a history of your project. On the other hand, GitHub is a web-based platform that utilizes Git for version control. It offers additional features beyond Git, including a graphical interface, issue tracking, pull requests, and more. GitHub enables developers to host their Git repositories in the cloud.
Now, let’s start with the basic Git commands. It’s important to note that there are many Git commands, each serving a specific purpose.
git init: This command initializes a new Git repository. You can use git init --help to get more information.
git clone: This command typically creates a copy of a remote repository on your machine.
git add: This command is used to add changes in your working directory to your repository. Its options include:
-A: Similar togit add ., adds all changes, including untracked and removed files, to your remote repository.-i: Opens an interactive mode, allowing you to choose which changes to add.
git commit: This command is used to set a comment or a commit message for your push.
Get Anas Bouzanbil’s stories in your inbox
Join Medium for free to get updates from this writer.
git status: Shows the status of changes as untracked or modified.
git push: This command pushes committed changes to a remote repository. Its options vary, such as:
-f: Forcefully pushes your changes.--all origin: Pushes to all branches.
git pull: This command is used to fetch and integrate changes from a remote repository into your current branch. It combines two actions:git fetch, which retrieves the changes from the remote repository, andgit merge, which incorporates those changes into your local branch. This is particularly useful when collaborating with others, as it helps you stay up-to-date with the latest changes in the remote repository.git checkout: Thegit checkoutcommand is versatile and serves multiple purposes:
- Switching branches: Use
git checkout <branch-name>to switch from your current branch to another existing branch. - Creating a new branch: Use
git checkout -b <new-branch>to create and switch to a new branch in one command. - Discarding changes: Use
git checkout -- <file>to discard changes made to a specific file in your working directory.
git merge: This command integrates changes from one branch into another. Typically, you use it to combine the changes made in a feature branch into the main branch. After fetching changes with git pull and switching to the target branch with git checkout, you can use git merge <source-branch> to merge the changes. This creates a new commit that consolidates the changes from both branches. In cases where there are conflicting changes, Git provides tools to resolve these conflicts before finalizing the merge.







