Basics to Advance Git commands is listed here, practicing them will make working with GIT easier and faster.
To view all Git configuration settings, use the following command:
git config --list
This will display all the configuration settings, including local, global, and system configurations.
To check a specific configuration setting, use:
git config user.name
This command will output the currently configured username.
You can use the --show-origin
or --show-scope
options to see where each setting is defined:
git config --list --show-origin
git config --list --show-scope
These commands will show the names and values of Git config properties along with their file locations.
To configure your Git username, use:
git config --global user.name "Your Name"
This sets the username globally for all repositories used by the current user.
To configure your Git email, use:
git config --global user.email "your.email@example.com"
This sets the email address globally for all repositories used by the current user.
To store login credentials in the cache, use:
git config --global credential.helper cache
This command sets up the credential helper to cache your login credentials for a limited time.
To start a new Git repository, use:
git init
This command initializes a new Git repository in the current directory.
Add a File to the Staging Area
To add a specific file to the staging area, use:
git add filename_here
Replace filename_here
with the name of the file you want to add.
To add all files in the current directory to the staging area, use:
git add .
This command stages all files in the project directory.
To add files matching a pattern, use:
git add fil*
This command adds all files starting with fil
to the staging area.
To commit changes with a message, use:
git commit -m "Your commit message here"
This command commits the staged changes with a short commit message.
To add and commit tracked files in one step, use:
git commit -a -m "Your commit message here"
This command adds and commits tracked files without needing to stage them separately.
To see the status of the current repository, including staged, unstaged, and untracked files, use:
git status
This command provides an overview of the repository’s current state.
To view the commit history, use:
git log
This command displays a list of commits made to the repository.
To see the commit history including the changes made in each commit, use:
git log -p
This command shows the commit history along with the diff of each commit.
To view details of a specific commit, use:
git show commit-id
Replace commit-id
with the ID of the commit you want to view.
To see commit history with statistics about the changes in each commit, use:
git log --stat
This command displays the commit history along with line and file changes.
To see unstaged changes, use:
git diff
This command shows the differences between the working directory and the staging area.
To see staged changes, use:
git diff --staged
This command shows the differences between the staging area and the last commit.
To interactively stage changes, use:
git add -p
This command opens a prompt to selectively stage changes.
To remove a tracked file from the working tree, use:
git rm filename
This command removes the file and stages the removal.
To rename a file and stage the change, use:
git mv oldfile newfile
This command renames the file and stages the change.
To ignore files, create a .gitignore
file and commit it:
echo "filename" > .gitignore
git add .gitignore
git commit -m "Add .gitignore"
This process tells Git to ignore the specified files.
To revert changes to a file in the working directory, use:
git checkout filename
This command reverts the file to its state in the last commit.
To unstage changes, use:
git reset HEAD filename
This command unstages the file but keeps the changes in the working directory.
To modify the most recent commit, use:
git commit --amend
This command allows you to modify and add changes to the most recent commit.
To revert the last commit, use:
git revert HEAD
This command creates a new commit that is the opposite of the last commit.
To create a new branch, use:
git branch branch_name
This command creates a new branch but does not switch to it.
To switch to a branch, use:
git checkout branch_name
This command switches to the specified branch.
To create and switch to a new branch in one step, use:
git checkout -b branch_name
This command creates a new branch and switches to it immediately.
To list all branches, use:
git branch
This command displays all branches and highlights the current branch.
To delete a branch, use:
git branch -d branch_name
This command deletes the specified branch if it has been fully merged into the current branch.
To merge the history of one branch into another, use:
git merge branch_name
This command merges the specified branch into the current branch.
To view the commit log as a graph, use:
git log --graph --oneline
This command displays the commit history in a graphical format.
To view the commit log as a graph for all branches, use:
git log --graph --oneline --all
This command displays the commit history for all branches in a graphical format.
To abort a merge and return to the state before the merge, use:
git merge --abort
This command throws away the current merge and resets the repository to its state before the merge attempt.
To add a remote repository, use:
git remote add origin https://repo_here.git
Replace https://repo_here.git
with the URL of your remote repository.
To see the URLs of remote repositories, use:
git remote -v
This command displays the URLs of all remote repositories associated with the local repository.
To get more information about a remote repository, use:
git remote show origin
Replace origin
with the name of the remote repository you want to query.
To push changes to a remote repository, use:
git push
This command pushes the local commits to the remote repository.
To fetch and merge changes from a remote repository, use:
git pull
This command retrieves the latest changes from the remote repository and merges them into the local branch.
To download changes from a remote repository without merging them, use:
git fetch
This command updates the remote tracking branches but does not merge the changes into the local branch.
To list the name of all remote branches that Git is tracking, use:
git branch -r
This command displays the names of all remote branches.
To merge changes from a remote repository into the local branch, use:
git merge origin/main
Replace origin/main
with the name of the remote branch you want to merge.
To update the remote tracking branches without merging any content into the local branches, use:
git remote update
This command updates the remote tracking branches but does not perform a merge.
To push a new branch to a remote repository, use:
git push -u origin branch_name
This command creates the branch on the remote repository and sets the local branch to track the remote branch.
To remove a branch from a remote repository, use:
git push --delete origin branch_name_here
This command deletes the specified branch on the remote repository.
To transfer completed work from one branch to another, use:
git rebase branch_name_here
This command rebases the current branch onto the specified branch.
To run an interactive rebase, use:
git rebase -i master
This command opens the editor with a list of commits to interactively rebase.
To force a push request, use:
git push -f
Note: This command is generally not recommended for public repositories as it can overwrite changes made by others.
.git/config
within the repository’s directory.git config --local core.editor vim
.~/.gitconfig
on Unix-like systems, C:\Users\USERNAME\.gitconfig
on Windows.git config --global user.name "Your Name"
./etc/gitconfig
on Unix-like systems, C:\ProgramData\Git\config
on Windows.sudo git config --system core.autocrlf false
.You can edit configuration files directly using the --edit
option:
git config --edit
git config --local --edit
git config --global --edit
git config --system --edit
This opens the configuration file in your default editor.
back -> Git basics Commands