Git commands for developer contains the details from basic to some advance level, as we discussed multiple times about GIT, here we have added few more points with some use cases.
A version control system, or VCS, tracks the history of changes as people and teams collaborate on projects together. As developers make changes to the project, any earlier version of the project can be recovered at any time.
Developers can review project history to find out:
- Which changes were made?
- Who made the changes?
- When were the changes made?
- Why were changes needed?
VCSs give each contributor a unified and consistent view of a project, surfacing work that’s already in progress. Seeing a transparent history of changes, who made them, and how they contribute to the development of a project helps team members stay aligned while working independently. In a distributed version control system, every developer has a full copy of the project and project history. Unlike once popular centralized version control systems, DVCSs don’t need a constant connection to a central repository.
Git is the most popular distributed version control system. Git is commonly used for both open source and commercial software development, with significant benefits for individuals, teams, and businesses. Git lets developers see the entire timeline of their changes, decisions, and progression of any project in one place. From the moment they access the history of a project, the developer has all the context they need to understand it and start contributing.
Developers work in every time zone. With a DVCS like Git, collaboration can happen any time while maintaining source code integrity. Using branches, developers can safely propose changes to production code.
Businesses using Git can break down communication barriers between teams and keep them focused on doing their best work. Plus, Git makes it possible to align experts across a business to collaborate on major projects. In this article, I will list the most essential commands which you should know as a developer and become a master in handling your Git repositories. This crafted for both beginners and experienced developers to get benefit from this article, Let’s start
1. Set Your Username and Email
The username and email address are required to link commits with your name. Its no need to be same as your GITHUB or GITLAB or BITBUCKET repositories, you can set or update your username by using any name with git config command. The new name will automatically reflect in any future commits that you push to GitHub from the command line. Same for email address you can set as following,
# git config --global user.name "foxutech"# git config --global user.email "iam@foxutech.com"
2. Cache Your Credentials
As we are little lazy typing the credentials always, even GIT helps us to be more. You can just cache your login credentials by using the config parameter and –global flag. It helps to avoid re-typing the username and password every time you perform a commit, one another way this option helps to save our time also.
# git config --global credential.helper cache
3. Initialize a Repository
You can create an empty Git repository or reinitialize an existing one by using the init parameter. It will create a hidden directory called .git upon initialization. This directory contains all the objects and references that Git uses and creates as a part of your project’s history.
# git init
4. Add a file to Staging Area
You can add a file to the staging area by using the add parameter and the name of the file. Just replace the myfirst.html with your filename. Also you can also add all files and directories to the staging area by providing the wildcard . instead of the file name.
# git add myfirst.html# git add .
5. Check a Repository Status
You can view the status of the current repository by using the status keyword, which includes the staged, unstaged, and untracked files.
# git status
6. Commit Changes with a Single Line Message or Through an Editor
You can add a single line message while making a commit in your repository by using the commit with -m flag. Please find the sample reference below.
# git commit -m "Your short summary about the commit"
You can also open a text editor in the terminal to write a complete commit message. This message can contain multi-line text to allow you to explain the changes made to a repository in a detailed way.
# git commit
7. View Commit History with Changes
Sometime we need check what was changes are done and commit details, for that GIT will help to track with log parameter and you can add some additional flags, like below -p which will provide detailed changes
# git log -p
8. View a Particular Commit
You can see the detailed changes of a specific commit by using the show parameter and providing the ID or hash of the commit. This hash is unique for each commit made in your repository.
# git show 9c13a4e3add9d788ae35d269a50cbbd8c7cc89a1
You can also provide a short hash to get the same result.
# git show 9c13a4e
9. View Changes Before Committing
You can view the list of changes made to your repository by using the diff parameter. It will only show the unstaged changes by default.
# git diff
If you want to view the staged changes, you can add the –staged flag.
# git diff --staged
You can also provide a filename as a parameter to only view the changes of a specific file.
# git diff myfirst.html
10. Remove Tracked Files from The Current Working Tree
You can remove files from the current working tree by using the rm parameter. This action will also remove the files from the index.
# git rm dirname/myfirst.html
You can also provide file globs (e.g., *.js) to remove all matching files.
# git rm dirname/*.html
11. Rename Files
You can rename a file or directory by using the mv parameter. This parameter requires a <source> and a <destination>. The source must exist and can be a file, or directory, and the destination must be an existing directory.
# git mv dir1/myfirst.html dir2
Executing the above command will move the source(s) into the target directory. The index will get updated, but you must commit the changes.
12. Revert Unstaged and Staged Changes
You can restore the unstaged working tree files by using the checkout parameter. You need to provide a file path to update it. If the file path is not set, then git checkout will update the HEAD to set the specified branch as the current branch.
# git checkout myfirst.html
To restore a staged working tree file, you can use the reset parameter. You need to provide a file path to remove it from the staging area. This will not remove any changes or modifications done to the files, instead, the file will be considered as an unstaged file.
# git reset HEAD myfirst.html
If you want to unstage all staged files, then do not provide the file path.
# git reset HEAD
13. Amend the Most Recent Commit
You can make changes to the most recent commit by using the commit parameter with the –amend flag. For instance, you just committed some files, and you remember that you have made a mistake in your commit message. In such a scenario, you can execute this command to edit the previous commit’s message without modifying its snapshot.
Continue Reading on GIT Commands for Developer — FoxuTech
Follow us on: