A brief introduction about git ,2024

Git is a version control system that is widely used for software development and other version control tasks. It provides a way to track changes to source code and other files, as well as to manage and collaborate on projects with other developers.

One of the key features of Git is that it allows developers to work on multiple versions of a file or project concurrently, without the need to constantly sync their changes with a central repository. This makes it easier for developers to work independently, as well as to collaborate with others on the same project.

Git stores its data in a distributed manner, meaning that each copy of a Git repository contains a complete history of all the changes that have been made to the repository. This makes it easy to share changes with other developers and to roll back changes if necessary.

Git basic commands

  • Git Config command
  • Git init command
  • Git clone command
  • Git add command
  • Git commit command
  • Git status command
  • Git push Command
  • Git pull command
  • Git Branch Command
  • Git Merge Command
  • Git log command
  • Git remote command

1). Git Config command

The git config command is used to set configuration options for your Git installation. You can use it to set things like your user name and email address, which are used to identify you as the author of commits you make. You can also use git config to set other options that control how Git behaves.

Here is an example of using git config to set your user name and email address:

git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"


The –global option tells Git to set the configuration option for all repositories on your computer. If you omit this option, the configuration option will only be set for the current repository.

There are many other options that you can set with git config. You can see a list of all available options by running git config –help.

2). Git init command

The git init command is used to create a new Git repository. It is typically used when you want to start version controlling an existing project that is not already under version control.

To use git init, navigate to the directory that contains the project you want to track, and then run the git init command. This will create a new subdirectory called .git, which is where Git stores all the information about the repository, including the version history and the files that are being tracked.

For example, if you have a project called myproject, you can create a new Git repository for it by running the following commands:

cd myproject
git init

After running git init, your project is now under version control, and you can start using Git to track changes to the files in the project.

You can also use the git init command to create a new repository from scratch, by running git init in a new directory that you create specifically for your repository. This can be useful if you want to start a new project from scratch, and track its entire history from the very beginning using Git.

3). Git clone command

The git clone command is used to create a copy of an existing Git repository. It is typically used to download a repository from a remote server, such as a repository hosted on GitHub.

To use git clone, you will need the URL of the repository you want to clone. You can then run the git clone command, followed by the repository URL, like this:

git clone https://github.com/user/repo.git

This will create a new directory called repo, and it will download all the files in the repository, along with the entire version history, into this new directory. The git clone command also automatically creates a new remote called “origin” that points to the repository you cloned from, so you can easily fetch updates from the original repository or push your own changes back to it.

You can also specify a different name for the local repository by providing it as an additional argument after the repository URL:

git clone https://github.com/user/repo.git myrepo

This will create a new directory called myrepo and clone the repository into it.

The git clone command is a convenient way to create a local copy of a remote repository, and it is especially useful when you want to start working on an existing project that is hosted on a remote server.

4). Git add command

The git add command is used to add new or modified files to the staging area in Git. The staging area is a virtual place where you can group files together before you decide whether to commit them to the repository.

To use git add, you can specify the file or files you want to add as arguments to the git add command. For example, to add a file called myfile.txt to the staging area, you can run the following command:

git add myfile.txt

You can also use wildcards to add multiple files at once. For example, to add all the files in the current directory with the .txt extension, you can run the following command:

git add *.txt

You can also use the git add command to add the changes you have made to a file to the staging area, rather than adding the file itself. To do this, you can use the -p option, which stands for “patch”. This will allow you to review the changes you have made to the file and choose which ones you want to add to the staging area.

git add -p myfile.txt

Once you have added the files you want to the staging area, you can use the git commit command to commit them to the repository. This will create a new snapshot of the repository that includes the changes you have made.

4). Git commit command

The git commit command is used to save your changes to a Git repository. It creates a new snapshot of the repository that includes the changes you have made, and stores it in the repository’s history.

To use git commit, you need to first add the changes you want to commit to the staging area using the git add command. For example, to add a file called myfile.txt to the staging area and commit it to the repository, you would run the following commands:

git add myfile.txt
git commit

The git commit command will open your default text editor, and prompt you to write a commit message. A commit message is a short description of the changes you have made, and it helps other people who are working on the project understand what you have done.

Once you have written your commit message, save the file and exit the text editor. This will create a new commit with your changes, and add it to the repository’s history.

You can also use the -m option to specify the commit message on the command line, like this:

git commit -m "Add myfile.txt"

This can be convenient if you don’t want to open a text editor to write the commit message. Just be sure to enclose the message in quotation marks if it contains multiple words.

The git commit command is an important part of the Git workflow, and it is used to save changes to the repository and share them with others.

6). Git status command

The git status command is used to show the status of a Git repository. It displays a list of files that have been modified and are not yet staged, as well as a list of files that are not being tracked by Git.

For example, if you have modified a file called myfile.txt and you haven’t added it to the staging area yet, git status will show it as an “unstaged” change:

On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   myfile.txt

If you have added the changes to myfile.txt to the staging area using git add, git status will show it as a “staged” change:

On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   myfile.txt

git status can also be used to show you which branch you are currently on, and whether there are any conflicts between your local changes and the latest version of the repository.

You can use git status at any time to see the current status of your repository, and to decide which changes you want to commit or discard. It is a useful command to run before you run git commit, to make sure you are only committing the changes you intended to.

7). Git push Command

The git push command is used to upload your local repository to a remote repository. It is used to send your committed changes to a shared repository, such as a project hosted on GitHub, so that other people can access your code and collaborate with you.

To use git push, you need to specify the name of the remote repository you want to push to, as well as the branch you want to push. The syntax for git push is as follows:

git push <remote> <branch>

For example, if you want to push your local master branch to the origin remote, you can run the following command:

git push origin master

This will upload all the commits you have made on the master branch to the origin remote repository.

You can also use the git push command to create a new branch on the remote repository and push your local commits to it. To do this, you can specify a new branch name as the target for the push, like this:

git push origin mynewbranch

This will create a new branch called mynewbranch on the origin remote, and push all the commits from your local mynewbranch branch to it.

The git push command is an essential part of the Git workflow, and it is used to share your changes with other people and collaborate on projects.

8). Git pull command

The git pull command is used to download changes from a remote repository and merge them into your local repository. It is the opposite of the git push command, which is used to upload local changes to a remote repository.

To use git pull, you need to specify the name of the remote repository you want to pull from, as well as the branch you want to merge the changes into. The syntax for git pull is as follows:

git pull <remote> <branch>

For example, if you want to pull changes from the origin remote and merge them into your local master branch, you can run the following command:

git pull origin master

This will download the changes from the origin remote, and try to automatically merge them into your local master branch. If there are any conflicts between your local changes and the remote changes, Git will prompt you to resolve them.

You can also use the git pull command to create a new branch on the remote repository and pull the changes into a new local branch with the same name. To do this, you can specify a new branch name as the target for the pull, like this:

git pull origin mynewbranch

This will create a new local branch called mynewbranch and pull the changes from the mynewbranch branch on the origin remote into it.

The git pull command is a convenient way to download and merge changes from a remote repository, and it is an important part of the Git workflow. It is often used to keep your local repository up to date with the latest changes made by other people.

9). Git Branch Command

The git branch command is used to create, list, and delete branches in a Git repository. A branch is a separate line of development in a repository, and it allows you to work on new features or bug fixes without affecting the main branch (usually called master).

To create a new branch, you can use the git branch command followed by the name of the new branch. For example, to create a new branch called mynewbranch, you can run the following command:

git branch mynewbranch

This will create a new branch called mynewbranch, but it will not switch to it. To switch to the new branch and start working on it, you can use the git checkout command:

git checkout mynewbranch

You can also use the -b option to create a new branch and switch to it in a single command:

git checkout -b mynewbranch

To list all the branches in a repository, you can use the git branch command with no arguments:

git branch

This will list all the branches in the repository, with an asterisk next to the current branch:

* master
  mynewbranch

To delete a branch, you can use the git branch command with the -d option followed by the name of the branch you want to delete. For example, to delete the mynewbranch branch, you can run the following command:

git branch -d mynewbranch

The git branch command is a useful tool for managing the development of your project, and it is an important part of the Git workflow. It allows you to work on multiple features or bug fixes at the same time, and it makes it easy to switch between them.

10). Git Merge Command

The git merge command is used to merge the changes from one branch into another branch. It is typically used to combine the changes from a feature branch into the main branch (usually called master) of a repository.

To use git merge, you need to switch to the branch you want to merge the changes into, and then run the git merge command followed by the name of the branch you want to merge. For example, if you want to merge the changes from a branch called myfeature into the master branch, you can run the following commands:

git checkout master
git merge myfeature

This will merge the changes from myfeature into the master branch, and create a new commit that combines the changes from both branches.

If there are any conflicts between the changes in the two branches, Git will prompt you to resolve them. This means you will need to manually edit the conflicting files to decide which changes to keep and which to discard. Once you have resolved the conflicts, you can use git add to mark the files as resolved, and then run git commit to create a new merge commit.

The git merge command is an important part of the Git workflow, and it is used to combine the changes from multiple branches into a single branch. It is especially useful when working on a team, where multiple people may be working on different features or bug fixes in separate branches.

11). Git log command

The git log command is used to display the commit history of a Git repository. It shows a list of all the commits that have been made, along with the commit message, the author, and the date of each commit.

To use git log, you can simply run the git log command with no arguments:

git log

This will show a list of all the commits in the repository, in reverse chronological order (with the most recent commits at the top). The output will look something like this:

$ git log
commit abcdef0123456789
Author: John Doe <john@example.com>
Date:   Mon Jan 1 12:00:00 2020 -0500

    Add feature X

commit fedcba9876543210
Author: Jane Smith <jane@example.com>
Date:   Sun Dec 31 16:00:00 2019 -0500

    Fix bug Y

12). Git remote command

The git remote command is used to manage a list of remote repositories that are associated with a local repository. You can use this command to add, remove, and modify the remote repositories that your local repository is connected to.

Here are some common uses of the git remote command:

  • git remote add : add a new remote repository and give it the specified name
  • git remote remove : remove the named remote repository from the list of remotes
  • git remote set-url : change the URL of the named remote repository
  • git remote rename : rename an existing remote repository
  • git remote -v: show a list of all the remote repositories, along with their names and URLs

For example, to add a new remote repository named upstream that is located at https://github.com/upstream/repo, you would use the following command:

$ git remote add upstream https://github.com/upstream/repo

To see a list of all the remote repositories that are associated with your local repository, you can use the -v option:

$ git remote -v
origin  https://github.com/user/repo (fetch)
origin  https://github.com/user/repo (push)
upstream  https://github.com/upstream/repo (fetch)
upstream  https://github.com/upstream/repo (push)

For more information about the git remote command and the various options you can use with it, you can refer to the Git documentation.

Discover more from STRUGGLER KING.COM

Subscribe now to keep reading and get access to the full archive.

Continue Reading