50 Git Commands That You Must Know For Your Next Project
Posted by TotalDCGit is a distributed version control system that helps developers collaborate on projects of any scale. In this article, I will give you 50 Git commands that will help you in your future projects.
What Is Distributed Version Control System (GIT)?
A distributed version control system is a system that helps you keep track of changes you’ve made to files in your project.
Git has many different commands you can use. So I have written them down and thought it’d be nice to share them with the community. I hope you find them useful.
Check Your Git Configuration
The command below returns a list of information about your git configuration including user name and email:
git config -l
Setup Your Git Username
With the command below you can configure your user name:
git config --global user.name "Paul"
Setup your Git user email
This command lets you set up the user email address you’ll use in your commits.
git config --global user.email "email@email.com"
Cache Your Login Credentials In Git
You can store login credentials in the cache so you don’t have to type them in each time. Just use this command:
git config --global credential.helper cache
Initialize A Git Repository
Everything starts from here. The first step is to initialize a new Git repo locally in your project root. You can do so with the command below:
git init
Add A File To The Staging Area In Git
The command below will add a file to the staging area.
git add your_file_name
Add All Files Fn The Staging Area In Git
If you want to add all files in your project to the staging area, you can use a wildcard. and every file will be added for you.
git add .
Add only certain files to the staging area in Git
With the asterisk in the command below, you can add all files starting with ‘fil’ in the staging area.
git add fil*
Check the repository’s status in Git
This command will show the status of the current repository including staged, unstaged, and untracked files.
git status
Commit changes in the editor in Git
This command will open a text editor in the terminal where you can write a full commit message.
git commit
Commit changes with a message in Git
You can add a commit message without opening the editor. This command lets you only specify a short summary for your commit message.
git commit -m "your commit message here"
Commit changes and skip the staging area in Git
You can add and commit tracked files with a single command by using the -a and -m options.
git commit -a -m"your commit message here"
See your commit history in Git
This command shows the commit history for the current repository:
git log
See your commit history including changes in Git
git log -p
See a specific commit in Git
This command shows a specific commit. Replace commit-id with the id of the commit that you find in the commit log after the word commit.
git show commit-id
See log stats in Git
This command will cause the Git log to show some statistics about the changes in each commit, including line(s) changed and file names.
git log --stat
See changes made before committing them using diff in Git
You can pass a file as a parameter to only see changes on a specific file.
git diff shows only unstaged changes by default.
We can call diff with the –staged flag to see any staged changes.
git diff
git diff all_checks.py
git diff --staged
See changes using git add -p
This command opens a prompt and asks if you want to stage changes or not, and includes other options.
git add -p
Remove tracked files from the current working tree in Git
This command expects a commit message to explain why the file was deleted.
git rm filename
Rename files in Git
This command stages the changes, then it expects a commit message.
git mv oldfile newfile
Ignore files in Git
Create a .gitignore file and commit it.
Revert unstaged changes in Git
git checkout filename
Revert staged changes in Git
You can use the -p option flag to specify the changes you want to reset.
git reset HEAD filename
git reset HEAD -p
Amend the most recent commit in Git
git commit –amend allows you to modify and add changes to the most recent commit.
git commit --amend
Rollback the last commit in Git
git revert will create a new commit that is the opposite of everything in the given commit.
You can revert the latest commit by using the head alias like this:
git revert HEAD
Rollback an old commit in Git
You can revert an old commit using its commit id. This opens the editor so you can add a commit message.
git revert commit_id_here
Create a new branch in Git
By default, you have one branch, the main branch. With this command, you can create a new branch. Git won’t switch to it automatically – you will need to do it manually with the next command.
git branch new_branch_name
Switch to a new branch in Git
When you want to use a different or a newly created branch you can use this command:
git checkout your_branch_name
List branches in Git
You can view all created branches using the git branch command. It will show a list of all branches and mark the current branch with an asterisk and highlight it in green.
git branch
Create a branch in Git and switch to it immediately
In a single command, you can create and switch to a new branch right away.
git checkout -b branch_name
Delete a branch in Git
When you are done working with a branch and have merged it, you can delete it using the command below:
git branch -d branch_name
Merge two branches in Git
To merge the history of the branch you are currently in with the branch_name, you will need to use the command below:
git merge branch_name
Show the commit log as a graph in Git
You can use –graph to get the commit log to show as a graph. Also,
–oneline will limit commit messages to a single line.
git log --graph --oneline
Show the commit log as graph of all branches in Git
Does the same as the command above, but for all branches.
git log --graph --oneline --all
Abort a conflicting merge in Git
If you want to throw a merge away and start over, you can run the following command:
git merge --abort
Add a remote repository in Git
This command adds a remote repository to your local repository (just replace https://repo_here with your remote repo URL).
git add remote https://repo_here
See remote URLs in Git
You can see all remote repositories for your local repository with this command:
git remote -v
Get more info about a remote repo in Git
Just replace origin with the name of the remote obtained by
running the git remote -v command.
git remote show origin
Push changes to a remote repo in Git
When all your work is ready to be saved on a remote repository, you can push all changes using the command below:
git push
Pull changes from a remote repo in Git
If other team members are working on your repository, you can retrieve the latest changes made to the remote repository with the command below:
git pull
Check remote branches that Git is tracking
This command shows the name of all remote branches that Git is tracking for the current repository:
git branch -r
Fetch remote repo changes in Git
This command will download the changes from a remote repo but will not perform a merge on your local branch (as git pull does that instead).
git fetch
Check the current commits log of a remote repo in Git
Commit after commit, Git builds up a log. You can find out the remote repository log by using this command:
git log origin/main
Merge a remote repo with your local repo in Git
If the remote repository has changes you want to merge with your local, then this command will do that for you:
git merge origin/main
Get the contents of remote branches in Git without automatically merging
This lets you update the remote without merging any content into the
local branches. You can call git merge or git checkout to do the merge.
git remote update
Push a new branch to a remote repo in git
If you want to push a branch to a remote repository you can use the command below. Just remember to add -u to create the branch upstream:
git push -u origin branch_name
Remove a remote branch in Git
If you no longer need a remote branch you can remove it using the command below:
git push --delete origin branch_name
Git rebase
You can transfer completed work from one branch to another using git rebase.
git rebase branch_name
Force Push Request in Git
This command will force a push request. This is usually fine for pull request branches because nobody else should have cloned them. But this isn’t something that you want to do with public repos.
git push -f