GIT:Basic commands

abijithgabriel

ABIJITH

Posted on April 2, 2023

GIT:Basic commands

What is git?

Git is a popular and widely used version control system (VCS) for managing software development projects. It was created by Linus Torvalds in 2005 and has since become one of the most popular version control systems in use.

Git allows multiple developers to work on the same project at the same time without interfering with each other's work. It tracks changes made to a project's source code and allows developers to collaborate on those changes by merging them into a shared codebase. This makes it easier to manage complex projects with multiple contributors.

Git is also a distributed version control system, meaning that each developer has a complete copy of the project's source code and history on their local machine. This allows developers to work offline and then synchronize their changes with the rest of the team when they have an internet connection.

The Command Line

There are a lot of different ways to use Git. There are the original command-line tools, and there are many graphical user interfaces of varying capabilities.We will be using Git on the command line. For one, the command line is the only place you can run all Git commands — most of the GUIs implement only a partial subset of Git functionality for simplicity.
If you know how to run the command-line version, you can probably also figure out how to run the GUI version, while the opposite is not necessarily true. Also, while your choice of graphical client is a matter of personal taste, all users will have the command-line tools installed and available.

  • git init: initializes a brand new Git repository and begins tracking an existing directory. It adds a hidden subfolder within the existing directory that houses the internal data structure required for version control.

  • git clone: creates a local copy of a project that already exists remotely. The clone includes all the project's files, history, and branches.

  • git add: stages a change. Git tracks changes to a developer's codebase, but it's necessary to stage and take a snapshot of the changes to include them in the project's history. This command performs staging, the first part of that two-step process. Any changes that are staged will become a part of the next snapshot and a part of the project's history. Staging and committing separately gives developers complete control over the history of their project without changing how they code and work.

  • git commit: saves the snapshot to the project history and completes the change-tracking process. In short, a commit functions like taking a photo. Anything that's been staged with
    git add will become a part of the snapshot with git commit.

  • git status: shows the status of changes as untracked, modified,
    or staged.

  • git branch: shows the branches being worked on locally.

  • git merge: merges lines of development together. This command is typically used to combine changes made on two distinct branches. For example, a developer would merge when they want to combine changes from a feature branch into the main branch for deployment.

  • git pull: updates the local line of development with updates from its remote counterpart. Developers use this command if a teammate has made commits to a branch on a remote, and they would like to reflect those changes in their local environment.

  • git push: updates the remote repository with any commits made
    locally to a branch.

Start a new repository and publish it to GitHub
First, you will need to create a new repository on GitHub.Do not initialize the repository with a README, .gitignore or License file. This empty repository will await your code.

# create a new directory, and initialize it with git-specific functions
git init my-repo

# change into the `my-repo` directory
cd my-repo

# create the first file in the project
touch README.md

# git isn't aware of the file, stage it
git add README.md

# take a snapshot of the staging area
git commit -m "add README to initial commit"

# provide the path for the repository you created on github
git remote add origin https://github.com/YOUR-USERNAME/YOUR-REPOSITORY-NAME.git

# push changes to github
git push --set-upstream origin main
Enter fullscreen mode Exit fullscreen mode

Contribute to an existing repository

# download a repository on GitHub to our machine
# Replace `owner/repo` with the owner and name of the repository to clone
git clone https://github.com/owner/repo.git

# change into the `repo` directory
cd repo

# create a new branch to store any new changes
git branch my-branch

# switch to that branch (line of development)
git checkout my-branch

# make changes, for example, edit `file1.md` and `file2.md` using the text editor

# stage the changed files
git add file1.md file2.md

# take a snapshot of the staging area (anything that's been added)
git commit -m "my snapshot"

# push changes to github
git push --set-upstream origin my-branch
Enter fullscreen mode Exit fullscreen mode

Contribute to an existing branch on GitHub
This example assumes that you already have a project called repo on the machine and that a new branch has been pushed to GitHub since the last time changes were made locally.

# change into the `repo` directory
cd repo

# update all remote tracking branches, and the currently checked out branch
git pull

# change into the existing branch called `feature-a`
git checkout feature-a

# make changes, for example, edit `file1.md` using the text editor

# stage the changed file
git add file1.md

# take a snapshot of the staging area
git commit -m "edit file1"

# push changes to github
git push
Enter fullscreen mode Exit fullscreen mode

The number of commands in git 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 accordingly.
For more git commands

References:
git
https://docs.github.com/en
https://docs.github.com/en/get-started/using-git/about-git

💖 💪 🙅 🚩
abijithgabriel
ABIJITH

Posted on April 2, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

GIT:Basic commands
git GIT:Basic commands

April 2, 2023