Nowadays, a developer’s regular working day would be incomplete without using Git. Yes, Git has become an integral part of our daily development process, and it has shown to be beneficial when working with several individuals.
Git’s popularity has grown because of its speed, agility, and flexibility. It uses systematic versioning to help you maintain track of your files. Git makes it simple to revert to previous versions of code or build new branches on which to experiment with the current codebase. The following are the most useful Git commands for day-to-day tasks.
1. Git version
If you are a beginner and recently started your journey with git commands, then the first necessary step is to check whether you have the latest version or not by typing.
2. Git config
The git config tool is very useful if you are new to the git commands as this command creates your identification, including your name and email address. you can use this command by following the general syntax mentioned below.
$ git config --global user.name "User name" $ git config --global user.email "User email"
$ git config --global user.name "Its Linux Foss" $ git config --global user.email [email protected]
3. Git init
After creating your identification, the next step is to create a git repository where you can store and manage your source code, and you can do that by typing.
$git init <repository name>
4. Git status
Git is continually looking for changes in your project’s working directory. This covers modifications such as adding a new file for tracking, deleting a file, altering file permissions, changing the name or content of a file, and so on. Using the git status command, you can check what changes Git sees at any given time:
5. Git clone
This command will duplicate a repository that already exists and for that, it uses the git init command for the required information.
$git clone <Project URL>
6. Git commit
This command is used to record the files that you have added or modified with the git add command. A Git commit is a collection of file changes saved in Git as a single item.
You should give a clear and simple commit message as part of this procedure so that other developers may quickly grasp its purpose:
$git commit “Your git message”
7. Git add
The Git add command will populate your repository with all new or changed code files. This command provides a variety of options for adding files and directories.
$git add project_name
8. Git rm
You may need to remove files from your codebase from time to time, in which case you may use the Git rm command.
If you have files that you don’t want to include in your commit then you can type
$git rm <Your_file_name>
9. Git branch
In most cases, your Git repository has many branches. To put it another way, a branch is a separate line of code development. You can properly manage your branches with the Git branch command. The Git branch has a lot of various settings and configurations. A Git branch may be thought of as a series of commits or a path of development. A branch name refers to a particular commit ID that is linked to its parent commit ID, forming a development history chain.
$git branch <branch name>
Or if you want to delete a branch you can type
$git branch -d <branch_name>
10. Git Annotate
For each line in a file, you might wish to examine the commit information. The Git Annotate command aids in the viewing of file lines that include committing information.
$git annotate <File_Name>
11. Git fetch
As the name suggests, it is used to fetch or download all the information to the changes to your local repository so you may inspect them before applying them.
12. Git log
You can see all prior commits with the help of the Git log, with the most recent commit appearing first.
By default, it will display all commits from the currently checked-out branch, but you may compel it to display all commits from all branches by selecting all choices.
$git log - -all
13. Git shortlog
You can use this command to view the summary as it groups authors with their commits, this tool is useful for seeing who worked on what.
14. Git merge
This command will merge numerous commit sequences into a single history. The git merge command seeks a common base commit between two commit points, generally the branch tips. It will build a commit sequence after it has found a common base commit.
15. Git pull
New data is retrieved from a distant repository and applied to the local repository using git pull. This will synchronize the modifications from the remote repository to the local branches.
$git pull <remote> <Branch_Name>
16. Git push
After you’ve committed your changes, you’ll generally want to push them to a remote repository so that your coworkers can access them. So, running this command will push the current branch’s new modifications to its remote.
$git push <remote> <Branch_Name>
17. Git stash
This Git command saves your edited files in a temporary location. With the Git command below, you may work in the stash. In basic terms, stashing stores your modifications on a temporary shelf that you don’t need right now but could require later. You’ve been working on your feature branch for a while, but there’s a new urgent problem that has to be fixed. You were in the process of implementing the modifications and didn’t want to commit them.
18. Git show
Unlike the Git log, the command git show displays information about a given commit. Git show is quite useful for inspecting a given commit. The show is a useful command for displaying information about various objects, such as commits or tags. You can use the command to see the version of a file from two commits ago.
$git show <your_commit_hash>
19. Git rebase
Git rebase works similarly to git merge. With one exception, it merges two branches into a single branch. When you want to merge multiple private branches into a single branch, you should use the Git rebase command. It’ll also make the commit history more linear.
$git rebase <Rebase_Branch>
20. Git bisect
The Git bisect command aids in the detection of faulty commits by monitoring and tracking down the problem that causes it.
$git bisect start
21. Git tag
Tags are useful in Git and may be used to manage the release. If you’re releasing anything to the public, it’s much more crucial. Tags in Git allow you to distinguish between different versions of your code.
$git tag <tagname>
22. Git diff
The git diff tool is useful for comparing files and displaying the discrepancies between the staging area’s files and the current version. It accepts data sources as inputs and produces differences as outputs. Files commit, and branches are all examples of data sources.
$git diff source branch <target branch>
23. Git mv
To change the name of a git file. It will take two arguments: the name of the source and target files.
$git mv <Old_File_Name> <New_File_Name>
24. Git checkout
To swap between branches, use this Git command. By modifying your working directory to reflect the tip of the checked-out branch, the git checkout command lets you hop (switch) between various branches.
$git checkout <Branch_Name>
25. Git remote
In Git, a remote repository is a shared repository where all team members may exchange changes.
Additionally, the git remote command assists you in managing connections to such remote repositories.
You may always use the git remote command to see which remote server you’ve established. You may view the URL that git has saved by using the -v option.
$git remote add <Name> <url>
26. Git reset
You may use the git reset command to RESET your current head to a certain state. You can change the state of individual files or a whole branch.
27. Git clean
This command is used to delete all those files which have not been tracked yet. You must use the Git reset command if you wish to work with tracked files. The untracked files will be removed from the working directory. Untracked files are ones that haven’t been uploaded to the staging area but are in your repository directory.
28. Git help
There are numerous commands in Git, and you may use git help from the terminal at any time for further information on any command.
$git help <git_command>
29. Git Gui
It gives you access to the git repository and allows you to make changes to it. You can use git gui to create new commits, branches, local merges, and fetch and push. Git Gui, on the other hand, does not display project history.
30. Git work tree
In most cases, a single git repository has only one work tree. Multiple working directories can relate to a single git repository using Git worktree. It comes in handy for a variety of reasons, including executing two separate branches of your code.
$git worktree add
Git has become an integral part of our daily development process, and it has been shown to be beneficial when working with several individuals. Its popularity has grown because of its speed, agility, and flexibility and you can get profit from Git whether you’re a freelance web developer or a software designer for enterprise-level apps. So, you can not only improve your productivity but can also save your time if you should learn these most commonly used git commands.