Git is a software used as a distributed version control system (VCS). It is utilized to track changes in software projects, manage different versions, and collaborate effectively. Git operates swiftly, flexibly, and reliably.
GitHub is a cloud-based platform built upon Git. It allows you to host, share, and collaborate on projects.
đź’ˇBefore diving into Git usage, let's take a look at some essential terminal commands.
-
ls
: It lists the files and folders in the current directory. -
ls -la
: It lists the files and folders in the current directory in detail, showing permissions, owners, and other attributes of the files. -
cd [directory]
: It changes the directory to the specified one. -
cd ..
: It allows the user to move up one directory level from the current directory, effectively navigating to the parent directory. -
mkdir [folder name]
: It creates a new folder. -
rmdir
: It is used to delete the specified folder (directory). However, the folder must be empty. If there are files or subfolders inside the folder, thermdir
command will not work. If files or subfolders need to be deleted as well, therm -rf
command should be used. -
touch [file name]
: It creates a new file. -
rm [file name]
: It deletes the specified file. -
cp [source] [destination]
: It copies the source file to the destination location. -
mv [source] [destination]
: It moves the source file to the destination location. -
cat [file name]
: It prints the content of the file to the terminal. -
grep [word] [file name]
: It finds the lines containing the specified word. -
chmod [permissions] [file name]
: It changes the permissions of the file. -
pwd
: It shows the current working directory. You can use this command to find out in which directory you are located. -
echo 'message' > [file name]
: Writes a "message" containing text or a string of characters to the specified file. -
nano [file name]
: Used to open and edit a file.
👉 Now that we have covered the basic terminal commands, we can move on to Git commands.
Command | Description |
---|---|
git config |
It is used to configure or view Git configuration settings. |
git init |
It is used to initialize an existing directory as an empty Git repository. |
git clone |
It is used to create a local copy from a remote Git repository. |
git add |
It prepares the changes in the working directory to be tracked by Git. |
git commit |
It saves the changes staged in the working directory to a local Git repository. |
git diff |
It shows the differences between the working directory and the index, or between the index and the last commit. |
git reset |
It is used to undo changes made in Git or to edit the index. |
git revert |
Instead of reverting the changes of the previous commit, it allows creating a new commit to undo those changes. |
git restore |
It is used to revert the contents of the working directory or specific files to their previous commit state or to change them. |
git status |
It shows which files have been modified in the working directory, which ones are staged in the index, and which ones are awaiting commit. |
git rm |
It is used to remove a file or directory from the Git repository. |
git log |
It is used to view the commit history in a Git repository. |
git show |
It is used to display the changes and commit details of a specific commit detail. |
git tag |
It is used to add changes in the working directory to the index for Git to track. |
git branch |
It is used to list the existing branches in the current Git repository or to create a new branch. |
git checkout |
It is used to switch between branches in a Git repository, navigate to a specific commit, or create a new branch. |
git switch |
It is a command used in Git version 2.23 and later. This command is used to switch between branches in Git repositories. |
git merge |
It is used to merge changes from different branches. |
git remote |
It is used to manage remote Git repositories and interact with them. |
git push |
It is used to push local commits to a remote Git repository. |
git fetch |
It is used to fetch new information from a remote Git repository to the local repository, but it does not make any changes to the current working directory. |
git pull |
It is used to pull the latest information from a remote Git repository and apply these updates to your local working directory and current branch. |
git stash |
It is used to temporarily store changes you are working on but haven't committed yet, and to clean your working directory. |
đź‘€ Now let's look at the commands in more detail.
git help
This command typically opens the help documentation related to Git commands. Additionally, you can use git help -a
to
display an alphabetical list of all Git commands. This provides a quick overview of all Git commands.
git config --global user.name "your name"
It is used to configure Git settings. This command is used to set the username and is typically defined as a global setting, meaning the username applies to all Git projects on the system.
This setting is important for specifying which user made the changes, especially during commit operations.
git config --global user.email "your-email@example.com"
It is used to configure Git settings. This command is used to set the user's email address and is typically defined as a global setting, meaning the email address applies to all Git projects on the system.
This setting is important for specifying which user made the changes, especially during commit operations.
git config --global core.editor "code -w"
This is used to set the user-defined text editor for Git. In this example, it specifies the use of Visual Studio Code (code
), and the-w
option ensures that Git waits for the editor to close before proceeding. This means you can continue with commit messages or other editing tasks without waiting for Visual Studio Code to close.
git config --list(-l)
Used to list Git configuration settings. This command displays the configuration settings and values used by Git.
For example, you can use this command to view the username, email address, color preferences, and other settings defined in the Git configuration.
The output is typically in key=value
format and includes the configured settings for Git.
git init [project name]
-
Now, let's initialize this directory as a Git repository using the
git init
command: -
This process turns the current directory into an empty Git repository. You can now track files in this directory, commit changes, and
use Git's version control features. If you use
git init my_project
, a folder namedmy_project
will be created, and the Git repository will be initialized inside that folder. -
When the
git init
command is executed, Git initializes the current directory as a Git repository and adds a subdirectory named.git
. This subdirectory contains all the information and configuration settings for the Git repository. Therefore, running thegit init
command creates a Git repository and generates the.git
directory that holds all the related information. -
However, if you want to undo this process and delete the Git repository, simply deleting the
.git
directory is enough. However, this action is irreversible, and you will lose all history, commit information, branch structures, and other related data. Therefore, you should proceed with caution when deleting the directory. - For example, after creating a Git repository, you can follow the steps below to delete the repository (use with caution):
-
This command completely deletes the
.git
directory in the current directory. -
When the
-r (recursive)
and-f (force)
options are included, it deletes the specified directory, along with all files and subdirectories within it, without prompting for confirmation.
Used to initialize an existing directory as a Git repository. If [project name] is specified, a folder with this name is created, and the Git repository is initialized within this folder.
mkdir my_project
cd my_project
git init
rm -rf .git
git clone <project url>
- As an example, to clone a GitHub repo:
-
This command downloads the specified GitHub repository and creates a folder named
repository-name
in the current directory, copying the contents into it. This allows you to use the entire project on your local machine and make changes to it.
Used to copy a project from a remote Git repository to a local machine. This command downloads the specified Git repository in
its entirety and creates a local copy. The <project url>
represents the URL of the Git repository to be cloned.
git clone https://github.com/user/repo-path.git
git status
- Create a new directory and switch to this directory:
-
Check the directory status using the
git status
command: - The output will be like this:
-
This output indicates that the directory is not yet a Git repository. Therefore, when the
git status
command is run, Git reports that no repository has been initialized in the directory and returns an error. - Now, let's create the Git repository:
- Check the directory status using the git status command again:
- The output will be like this:
- This output indicates that the Git repository has been created successfully, but no commits have been made yet and there are no files being tracked.
Displays the status of files in the working directory and index of a Git repository. This command is used to see which files have
been modified, which are staged in the index, and which are waiting to be committed. Here are some example usages of the git status
command:
mkdir my_project
cd my_project
git status
fatal: Not a git repository (or any of the parent directories): .git
git init
git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
git add [file]
Thegit add
command is used to add changes in the working directory to the staging area for Git to track. Here are some examples of using thegit add
command:
# Create a new file
echo "This is an example file" > file.txt
# Add the file to the stage
git add file.txt
file.txt
was created in the working directory and added to the staging area using the git add
command
# Create new files
echo "hello world 1" > file1.txt
echo "hello world 2" > file2.txt
# Add all files to stage
git add file1.txt file2.txt
# Add all changes in the working directory to the staging area
git add .
.
(dot) represents all changes in the working directory. The git add .
command stages all files.
# Only add files with .txt extension to stage
git add *.txt
.txt
extension.
# Unstage a file from the staging area
git reset file.txt
file.txt
that we previously added to the staging area.
git diff [file]
Thegit diff [file]
command is used to show changes in a Git repository. This command is useful for comparing differences between commits, branches, or file versions. Here are the basic usages and examples of thegit diff
command:
git diff
git diff --cached
# git diff commit_id1 commit_id2
git diff abc def
abc
and def
commits
# git diff file_name
git diff app.js
# git diff commit_id
git dif abc
abc
commit and the current status.
# git diff other_branch_name
git dif feature-branch
git checkout -- [file]
Thegit checkout
command is used to switch between branches, view commits, create new branches, and revert files in the working directory within a Git repository. However, starting from Git 2.23, thegit switch
andgit restore
commands have taken over some of the responsibilities ofgit checkout
. Here are the basic uses of thegit checkout
command:
# git checkout branch_name
git checkout main
main
branch.
# git checkout -b new_branch_name
git checkout -b feature-xyz
feature-xyz
and automatically switches to this branch.
# git checkout -- file_name
git checkout -- index.html
index.html
file back to its last commit state.
# git checkout commit_id
git checkout abc123
abc123
commit id.
# git checkout branch_name -- file_name
git checkout main -- index.html
index.html
file of the main
branch into a specific commit state.
git switch
Thegit switch
command, introduced in Git version 2.23, is designed for switching between branches. This command allows you to move from the current branch to another branch. It replaces thegit checkout
command for branch switching, providing a safer and more explicit tool. Here are the basic usages and examples of thegit switch
command:
# git switch branch_name
git switch feature-branch
feature-branch
.
# git switch -c new_branch_name
git switch -c new-feature
new-feature
and switches to this branch.
# git switch --track remote_repo_name/remote_branch_name
git switch --track origin/main
# git switch -c new_branch_name --discard-changes
git switch -c new-feature --discard-changes
new-feature
, but does not save changes to the existing branch.
git commit
Thegit commit
command is used to permanently save changes in your working directory. Here are the basic uses and some examples of thegit commit
command:
# git commit -m "Commit Description"
git commit -m "Update homepage design"
git commit
command allows you to permanently save changes that are staged (in the Staging Area).
When used with the -m
parameter, you can add a commit message. The message is important for describing the changes you have made.
git commit -a -m "Update all files"
-a
(all) parameter. This commits all changes
to tracked files without needing to explicitly stage them.
However, be cautious when using this method, as it does not include untracked new files in the commit.
git commit --amend -m "fix: Commit Description"
--amend
parameter.
This command updates your most recent commit.
git rm [file]
The git rm
command removes a file or directory from version control in a repository. The removed file or directory will
no longer be tracked, and this change will take effect in the next commit. However, the file or directory is not physically deleted; it is only untracked.
# git rm file_name
git rm myFile.txt
myfile.txt
file, and the change will take effect in the next commit.
# git rm -f file_name
git rm -f myFile.txt
myfile.txt
file and physically deletes it. The -f
option forces the
deletion, even if the file has been modified.
git stash
Thegit stash
command is used to temporarily save changes in the current branch. This is useful when you want to save your work without committing it, allowing you to switch branches or work on something else without losing progress. Here are the basics ofgit stash
usage and examples:
git stash
git stash
saves all changes in the working directory to a temporary storage location called a stash.
This allows you to return your current branch to a clean state while keeping your changes safe for later use.
git stash list
# git stash apply stash_index_number
git stash apply 0
apply
command applies the stash but does not delete it.
If you want to apply and delete the stash simultaneously, you can use git stash pop
.
# git stash pop stash_index_number
git stash pop 0
# git stash show stash_index_number
git stash show 0
git stash clear
git branch
Thegit branch
command is used to list branches, create new branches, switch between branches, and delete branches in a Git repository. Here are the basic usages and some examples of thegit branch
command:
git branch
# git branch new_branch_name
git branch feature-xyz
feature-xyz
but does not automatically switch to it. You continue working on the current branch.
# git checkout target_branch_name
git checkout feature-xyz
# Alternatively, in Git 2.23 and later
# The following command can also be used:
# git switch target_branch_name
git switch feature-xyz
git checkout
and git switch
commands allow you to leave the current branch and switch to another branch.
# git checkout -b new_branch_name
git checkout -b feature-abc
feature-abc
and automatically switches to that branch.
# git branch -d branch_name_to_delete
git branch -d feature-xyz
feature-xyz
. However, if there are unmerged changes in this branch, the deletion
will not proceed. You can forcefully delete the branch using git branch -D
, but you should be cautious in this case.
git merge
git merge
command is used to combine different branches. It is typically used when you want to add changes made on a feature branch to themaster
branch or merge changes from different branches. Here is the basic usage of thegit merge
command along with examples:
git checkout master # switch to the branch to be merged
git merge feature-xyz # merge feature-xyz branch into master branch
# or use with switch command
git switch master
git merge feature-xyz
feature-xyz
branch into the current branch.
If the changes on a branch were made after the latest commit on the target branch (the branch to be merged), Git performs a 'Fast Forward' merge. In this case, no separate commit is created.
git checkout master
git merge feature-xyz
master
branch into the feature-xyz
branch. If a Fast Forward merge occurs, you will see that the master
branch now points to the same commit as the feature-xyz
branch's latest commit.
If there are changes made between the branch being merged and the target branch, and Fast Forward merge is not possible, Git will create a new commit to complete the merge.
git checkout master
git merge --no-ff feature-xyz
--no-ff
parameter forces a non-fast-forward merge, creating a new commit even if a fast-forward merge is possible.
If there are conflicting changes during the merge process, Git will not be able to complete the merge automatically. In this case, manual intervention may be required.
git checkout master
git merge feature-xyz
# git merge commit_id
git merge abc123
git log
git log
command is used to view the commit history of a Git repository. This command provides a list containing details such as commit IDs, authors, dates, and commit messages. Here are the basic usages and some examples of thegit log
command:
git log
git log --oneline
git log --oneline --graph
# git log file_name
git log index.html
index.html
file.
git log --until=2025-01-01
# git log --author="author_name"
git log --author="mces58"
git show
git show
command provides detailed information about a specific commit, branch, or tag in Git. This command displays the changes made in a commit, the commit message, and details about modified files. Below are the basic uses and examples of thegit show
command:
# git show commit_id
git show abc123
abc123
, including the changes, commit message, and other details.
git show
# git show branch_name
git show main
# git show v1.0.0
# git show commit_id file_name
git show abc123 index.html
index.html
file in a specific commit.
git show --color commit_id
git show -v commit_id
git tag
Thegit tag
command is used to add a tag to a specific commit or manage existing tags in a Git repository. Tags typically represent the name, version number, or description of a specific release or significant point in the repository's history. Here are the basic uses of thegit tag
command with examples:
# git tag tag_name
git tag v1.0.0
v1.0.0
to the current HEAD commit.
# git tag tag_name commit_id
git tag v1.0.0 abc123
v1.0.0
to a specific commit (here represented as abc123
).
# git tag -a tag_name -m "Tag Description"
git tag -a v1.0.0 -m "Stable version"
v1.0.0
with a description.
git tag
# git show tag_name
git show v1.0.0
v1.0.0
.
# git tag -d tag_name
git tag -d v1.0.0
v1.0.0
locally.
# git push remote_repo tag_name
git push origin v1.0.0
# git push remote_repo --tags
git git push origin --tags
git reset [<path>...]
Thegit reset
command is used to modify the commit history of a branch and revert files in the working directory. This command should be used carefully, as it changes history and is generally not recommended on shared branches. Here are the basic uses and examples of thegit reset
command:
# git reset commit_id
git reset abc123
abc123
commit ID. By default, git reset
performs a 'soft reset,' meaning it
reverts commits without modifying files in the working directory.
# git reset --hard commit_id
git reset --hard abc123
# git reset --soft commit_id
git reset --soft abc123
# git reset --mixed commit_id
git reset abc123
git reset --hard HEAD^
git revert
Thegit revert
command allows you to undo changes from one or more commits by creating a new commit. This method undoes changes without altering the commit history, providing a reversible mechanism for reverting changes. Here are the basic uses and examples of thegit revert
command:
# git revert commit_id
git revert abc123
abc123
commit and creates a new commit.
# git revert commit_id1 commit_id2
git revert abc123 def456
abc123
and def456
and creates separate commits for each revert operation.
While performing a revert operation, you can add a description to the new commit using the -m
or --mainline
option. This specifies
which parent branch to revert when dealing with a merge commit
# git revert -m 1 commit_id
git revert -m 1 abc123
git restore
Thegit restore
command is used to restore files or changes in the working directory or revert them to a specific commit state. Introduced in Git version 2.23, it serves as a replacement for thegit reset
andgit checkout
commands. Here are the basic usages and examples of thegit restore
command:
# git restore file_name
git restore index.html
index.html
file from the working directory.
# git restore --source=commit_id --staged --worktree file_name
git restore --source=abc123 --staged --worktree index.html
git restore --source=commit_id --staged --worktree --source=commit_id .
# git restore --source=HEAD --worktree file_name
git restore --source=HEAD --worktree index.html
git restore --source=commit_id --staged --worktree --source=commit_id file_name
git restore --source=commit_id --staged --worktree --source=commit_id .
git remote
Thegit remote
command is used to manage remote repositories, which are often centralized locations where the project is stored or shared. This command allows you to view, add, remove, and modify remote repositories. Below are the basic uses and examples of thegit remote
command:
git remote
git remote -v
# git remote add remote_repo_name remote_repo_url
git remote add origin https://github.com/username/project.git
# git remote rename old_name new_name
git remote rename upstream origin
# git remote remove remote_repo_name
git remote remove origin
# git remote show remote_repo_name
git remote show origin
# git remote set-url remote_repo_name new_url
git remote set-url origin https://new-address.com/username/project.git