In this post, You’ll learn what are the git commands that can be used in everyday life. You’ll see the top git commands with examples. You can not imagine a developer’s life without using version control tools such as git, bitbucket, or any tool.Because this makes like simple and easy to maintain the programming files and collaborate with your teammates.
Most of you do not use git commands either you use GUI plugins in IDE eclipse or Intellij tools
But, if you know all these handy command and you will get confidence in dealing with the conflicts resolving manually from the terminal.
Git Commands: Let us see what are the commands that are mostly used from the terminal or command prompt such as git bash in windows. At least the following should be known by all developers and most used in the software industry. We’ll discuss one by one from scratch.
1. git config:
This is the most important one but this is used only once when you join the new company or get a new laptop in the office. This is only a one-time job but if you want to change at any time such as name, email address using the “git config” command as below.
Names, email values can be set at the global, system, or user level. Example Commands to set the user name and email address. General syntax is followed as:
git config –global <property-name> <value>
git config –global user.name “Uday Chauhan”git config –global user.email “uday.chauhan@example.com"
To see all the user configurations have been set in your machine use the
git config –-list | grep ‘user’
2. git init:
“git init” is to convert a project into a git project. To see the current folder is under git or not using git status command or if the “.git” folder is present in the root directory it is git project.
If not a git project, just run the “git init” command at the root folder to convert into a git project.
$ git init ### Initialized empty Git repository in C:/project/.git/ $ ls -a ./ ../ .git/
3. git clone
If you want to clone any repo from GitHub or bitbucket or remote location then use `git clone “repo location”`
git clone https://github.com/ucguy4u/commands
4. git help:
If you don’t know much about any command then. use “git help <command-name>” it pulls syntax and all its options.
git help <command-name>
$ git help init
5. git status:
To see the list of files are modified and added to the stage in the current directory. And also show the current branch name.
$ git status
6. git add:
‘git add’ command to add new files or existing modified files to the git stage. This works from current directory. After ‘git add’ command execution, files will be moved from untracked files to ”changes to be committed”
git add <file-1-name>
$ git add file1.txt
Even you can add multiple files by delimiting the file names with space or use ‘*’ to add all files to the stage.
$ git add <file-1-name> <file-2-name>
for
$ git add *
7. git commit:
‘git commit’ command commit the files in stage area (Changes to be committed section) and creates a new commit id for this commit. But these files are now not available on the git. At this stage, all files are on in your local machine as now.
git commit -m <commit-name>
$ git commit -m "Adding git commands files"
8. git push:
After commit, use the ‘git push‘ command to move your changes to a remote repo that means from where repo is created.
git push
git push origin <branch-name> -> pushing the changes to a specific branch
git push -all –> pushing all branch changes to remote git.
$ git push
9. git pull
If you have other changes those are committed by your teammate and to get the latest changes to use ‘git pull‘ command.
git pull -> pulls from the current branch
git pull <repo-url> -> pull change from the given repo
$ git pull
10. git branch:
To see all branches under this repo, use ‘git branch’
$ git branch
* master
develop
To see, all the remote branches using ‘git branch -r‘
git branch -r -> to see remote branches
git branch <new-branch> release -> to create a new branch from release branch.
11. git checkout:
If you have modified the file in local repo and you want to replace all the changes with the remote file, use ‘git checkout <file-name>‘
$ git checkout file1.txt
12.git stash:
If you modified files in your local and do not want to commit immediately then use ‘git stash‘. Now, modified files will be shown in the ‘git status’ command. Nothing added to commit but untracked files present (use “git add” to track)
Added again some files to git stash. ‘save’ is optional and by defaults it does save.
$ git stash save
To see all the commits in the git stash use ‘git stash list’
To get the latest changes from stash to stage, use ;git stash pop‘.
Now see the list of all commits in the stash with the command ‘git stash list’
13. git branch:
‘git branch‘ command list all branches available in this repo.
This shows all branches untill your last ‘git pull‘. After, it pulls if any new branches created on the remote branch, and those can not be seen from the local branch.
To see only remote branches use “git branch -r” and use “git branch -a” to all remote and local branches.
14.git merge:
If you want to merge another branch to the current branch then use the ‘git merge‘ command. Making the changes to develop branch and merging develop branch changes to the master branch. Merging develop branch commits into the master branch.
git merge develop -> merges the changes of develop branch into the current branch.
15.git log:
To see all the commits version history in any branch or current branch, use ‘git log’ command.
By default, the ‘git log‘ command shows all the commits for the current branch but it provides different flags to do specific set of operations.
To see the file commits for a specific file then use ‘
git log –follow <file-name>‘
To see the commits for file1.txt
$ git log --follow file1.txt
To see all the commits for file2.txt
$ git log --follow file2.txt
16. git diff:
To see the difference among the modified files with remote files using ‘git diff‘ command.
Now, add the modified file to git.
Now, git diff does not show the differences but still, you can see the diff wit flag ‘–staged‘.
git diff –staged
Another interesting way to see the differences between the two branches.
$ git diff master develop
17.git reset:
To remove the file from stage and file should be in the local system. Look at the in-depth article on “git reset and git rm” commands to unstage a file.
Syntax: git reset <file-name>
$ git reset file1.txt
To unstage and remove all the files from a specific commit. But, all the files from this commit will remain as modified in the local machine.
git reset <commit-id>
$ git reset 6873c7a6c1135b3a977c9d14404e0fd652f566a8
Unstaged changes after reset:
M pom.xml
To discard all the changes from a commit and goes back to the specific commit.
After resetting to the first commit, we are not seeing the file2.txt in the local file system. Because, if you use a ‘hard‘ flag then it removes all history of the files.
You must be careful when running git commands with a ‘hard‘ flag.
18. git rm:
This command deletes the file from your current local working directory and from the stage as well. Once you do commit and push then file will be removed from git repo permanently.
If you do not use any flag then ‘git rm’ command just remove the file.
‘git rm’ command does not work with the staged file and works on only committed files.
Created now a new file3.txt and added to git. Then tried to remove using file3.txt file with ‘git rm file3.txt‘. This result in error saying use‘–cached’ or ‘-f’ flag.
“–cached” flag is used to remove the file from staging and reserves the changes.
git rm -f file3.txt — Just deleted the file from local machine.
19. git tag:
This command is used to give tags to the specified commit.
git tag <commit-d>
‘git tag’ command to see all the tags for a branch.
20. git show:
“git show” command shows the files metadata sections of contents changed in the commit. By default, it shows the result for the latest commit from the ‘git log‘ command.
Highlighted in yellow color is the contents modified.
To see only file names for a commit.
use flag ‘–name-only’ in ‘git show’ command.
git show –name-only <commit-id>
We’ve seen most used git commands in the software developer life every day from git bash or terminal.
Checkout to last checked out branch
Let us say you have created a fix-payment-bill-issue
branch from the master
branch to add a new feature or to fix a bug and want to switch to the last branch you were in, use the following command. This will be useful when you don’t remember the branch name that you have checked out last time.
$ git checkout -
Check whether the current branch is merged with the targeted branch
This will be useful to know whether your changes were merged with the targeted branch.
$ git branch --merged <targeted-branch>
Copy the files from another branch without switching the branch
Copy a file from another branch without switching branch, make sure the file is committed in another branch.
$ git checkout <target-branch> -- <complete/path/to/file.js>
Search for a particular text in the commit message
Search git log with a particular text string, it lists all the commits matching the string
$ git log -S<search-string>
Add the modified and uncommitted files to the previous commit
Add the modified and uncommitted files to the previous commit without changing the commit message, new files should be added to the staging.
$ git commit -a --amend -C HEAD
Remove the branches that are deleted from remote in the local copy
Remove the local branches no longer on remote
$ git remote prune origin
$ git fetch -p
Skip the hooks from executing on commit
If you have configured hooks to check for linting
or pre-checking
for some required steps before committing the changes. But you want to commit your changes without these checks, since there are lots of issues in the initial code, to avoid the hooks from being blocked from committing the changes, do this instead and commit it, use --no-verify
instead of -n
if you prefer a more verbose way to explain what it is.
$ git commit -n
If you know, all these commands then you can use git commands wisely.
Please leave a comment, if you know other tips and tricks that you are using every day.
Happy coding.
Github: git_commands