If you're a developer who uses GitHub as your version control system,
understanding Git and its command-line interface is crucial for smooth
collaboration and efficient workflow. Whether you're new to Git or already
familiar with it, being aware of essential command lines will help you
interact seamlessly with GitHub through a terminal. Here is a list of key
commands that every developer should know, starting from basic to advance
Basic Git Commands
When we mention basics we are talking about the most-know commands for every
developer who wants to use GitHub as their version control system. These are:
1. Git init
The git init command is used to initialize a new Git
repository. It is particularly useful when you are working on a project
locally that is not yet on GitHub or any other online repository. By running
"git init", you can create a new repository and subsequently set it up to be
uploaded online. This command marks the beginning of version control for your
project and prepares it for Git operations such as committing changes and
pushing them to remote repositories.
How to use it
on your terminal or your command prompt type
git init <repository name>
After this command, it will help you to create your project. The repository
name is the name you would like to call your project
2. Git config of a username and email address
If you are new to GitHub or using a new computer for collaboration, it is
crucial to configure the name and email address that Git will associate with
your commits. This ensures that your commits are correctly attributed and
reflected on your GitHub activity graph. To have your commits displayed on
your GitHub graph, it is necessary to use an email address that is connected
to your GitHub account when making commits. This way, the commits will be
linked to your GitHub profile and appear in the appropriate repository's
commit history and contribution statistics.
Usage
On your terminal, before you start any project, run the following commands:
Notice the dot after the "add". The dot (.) punctuation tells Git that you
want to add all your files. Additionally, the position of the dot (.) matters
significantly. It should not be immediately adjacent to the "add" command;
instead, it should be separated by a space.
git config user.name <your name>
git config user.email <email>
When writing your name and the email address make sure is inside a double
quote(" ")
3. Git Branch
A git branch in a version control system, such as Git, represents a separate
and distinct version of the main repository. It allows developers to work
on different features or changes without directly modifying the main
codebase. Each branch serves as an isolated environment where
modifications can be made independently, ensuring that changes on one
branch do not affect others until they are intentionally merged.
Creating branches is a common practice in collaborative development
workflows. It enables multiple team members to work on different tasks
simultaneously, experiment with new features, or fix issues without
interfering with each other's work. Branches provide a structured approach
to managing code changes and facilitate effective collaboration and
version control within a software development project.
To create a new branch on a command line, type the following code
git -b <branch name>
To switch between branches use:
git -b checkout <branch name>
To know all the branches related to a repository, use
git branch
4. Git Clone
Git clone is used to replicate or copy remote
files from a repository to your local machine. It enables you to obtain a
complete copy of an online project, allowing you to work with it locally.
This is particularly useful when collaborating with a team and you need to
have the project available on your local machine. By utilizing
git clone, you can easily copy the project from the online repository
and have it ready for local use.
git clone <paste the project link here>
If for instance, I want to clone a project name
I will do it like this on my terminal or command prompt
Sometimes if the branch is not found but there does exist a tracking
branch in exactly one remote, you would have to track it remotely by using
this command
https://github.com/Peter-say/SEO-manager.git
git clone https://github.com/Peter-say/SEO-manager.git
5. Git Checkout
Git checkout is used to check the current branch
you're working on and to switch between branches in Git. If you have
multiple branches in a project, you must know the branch you are working
on because that's where your working directory/tree will reside. It is
also used to update the files in the working directory to match the
version stored in that branch and tells Git to record all new commits on
that branch.
Usage
git checkout [ the branch name ]
git checkout -b <branch> --track
[<remote>/<branch>]
6. Git Add .
The git add . the command is used to stage
new files in your current directory in Git. When you add a file, you are
instructing Git to include it in the index, also known as the staging
area. The index serves as a temporary holding area for files that you want
to include in the next commit.
By using git add ., you are telling Git to
include all new files in the current directory and its subdirectories.
This command allows you to efficiently add multiple files at once.
However, it's important to note that the changes are not permanently saved
until you explicitly tell Git to do so by using the commit command.
Without the git add command, the changes
you made to your files will not be saved locally.
Furthermore, using git add . helps you to keep
track of your files for temporary usage. For example, if your computer
unexpectedly shuts down while you are coding, the files you have
instructed Git to keep track of will still be available when your computer
finally boots up. This feature provides a level of protection and ensures
that your work is not lost due to unforeseen circumstances.
How to use it
git add .
7. Git Commit
The git commits -m command is used to save/stage
changes before pushing them to the online repository. It modifies the files
in your index based on your recent changes and saves them to your local
repository. The "-m" flag is used to provide a descriptive message. Clear
committed messages make it easy for yourself and others to understand the
changes made and their timing.
The git commit -m is usually used when you want to
push your local repository online. Below is how to make use of it.
git commit -m <"your message">
Also, make sure your message is presented in the double quote(" ") not the
single quote (' ') to avoid a git error response
8. Git Push
Git push is used to upload committed files from your local repository to
the online repository. It allows you to save your files online for easy
access by other project collaborators. Before pushing, ensure that you
have committed your work using the
commit -m command.
git push
To push a new branch that hasn't been pushed before, use the command
git push -u origin <branch name>
This establishes an upstream reference, linking your local repository to
the online one. The -u option sets the upstream
automatically, enabling future Git operations like pull and push without
specifying additional arguments.
9. Git pull
The git pull command is used to fetch a copy of
files from the online repository to your local repository. It functions
similarly to git clone, but there is a key
difference: git clone is used to obtain a
complete copy of an entire project, whereas git pull
is used to retrieve files that other team members have worked on.
If you are collaborating with a team on a project and wish to view their
contributions in your local repository,
git pull is the appropriate command. To ensure
you are up-to-date with the latest codebase or files from your team, it is
recommended to use git pull regularly.
git pull
To clarify, when you and your collaborators are working on the same branch,
you can use git pull to fetch and merge their
changes into your local branch. However, if you want to pull changes from a
different branch that you are not currently on, you should use the following
command: git pull origin <branch name>. This
command fetches the latest changes from the specified branch on the remote
repository (`origin`) and merges them into your current local branch.
10. Git Status
This command allows you to check the status of your working tree. It
provides you with valuable information regarding the state of all the files
in your repository. For example, it helps you identify which files are
currently present in the online repositories and which ones are in the
staging area (index). It also shows you any modifications or changes made to
the files that are yet to be committed.
Git status helps identify tracked and untracked
files, providing a comprehensive repository overview. It enables informed
decision-making and appropriate actions based on file statuses.
git status
Thanks for reading hope to see you at the next one
