Saturday, April 27, 2024
HomeDevOpsGIT Commands for Developer

GIT Commands for Developer

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.

# git commit --amend -m "update my previous commit"

It is also possible to make changes to the previously committed files. For instance, you have updated some files in multiple folders that you want to commit in a single snapshot, but then you forgot to add one folder to commit. Fixing this sort of error is just a matter of staging the other files or folders and committing with the –amend and –no-edit flags.

# git add dir1
# git commit
# git commit --amend --no-edit

The –no-edit flag will allow you to make the correction to your commit without altering its commit message. The final commit will then replace the incomplete one, and it will look like we have committed the changes to all updated files and folders in one snapshot.

Alert!!! Don’t amend public commits.
Correcting a local commit with amend is excellent, and you can push it to a shared repository. But you should avoid amending commits that are already public. Remember that the amended commits are completely new commits, and the previous commit will not be available on your current branch. It has the same consequence as resetting a public snapshot.

14. Rollback Last Commit

You can roll back the last commit by using the revert parameter. This will create a new commit, an inverse of the previous commit, and add it to the current branch history.

# git revert HEAD

Revert v/s Reset

The git revert command only undoes a single commit. It does not move back to the previous state of a project by removing all succeeding commits, which is done when git reset is used.

Reverting has two major benefits over resetting. First, it doesn’t alter the project history, which makes it a safe operation for commits. Second, it can target a specific commit at any point in the history, whereas git reset can only work backwards from the current commit. For instance, if you want to undo an old commit with git reset, you will have to remove all the commits that occurred after the target commit, then re-commit all the subsequent commits. Thus, the git revert is a much better and safer way to undo the changes.

15. Rollback a Particular Commit

You can roll back to a particular commit by using revert parameter and the commit ID. It will create a new commit, a copy of the provided commit ID, and add it to the current branch history.

# git revert 9c13a4e

16. List All Branches

You can view the list of all branches by using the branch parameter. It will display all branches and mark the current branch with an asterisk (*) sign and highlight it.

# git branch

You can also list all remote branches by using the -a flag.

# git branch -a

You can create a new branch by using the branch parameter and the name of the branch.

# git branch MyNewBranch

But Git won’t switch to it automatically. If you want Git to auto-switch, you must pass the -b flag and the checkout parameter.

# git checkout -b MyNewBranch

17. Delete a Branch

You can delete a branch by using the branch parameter, -d flag and the name of the branch. If you’ve completed working on a branch and have merged it into the main branch, you can delete the branch without losing any history. However, if the branch hasn’t been merged, the delete command will output an error message. This safeguards you from losing access to your files.

# git branch -d MyOldBranch

If you want to forcibly delete a branch, you can use the capital -D flag. This will delete the branch despite its status and without any warning.

# git branch -D MyOldBranch

The above commands will only delete a local copy of the branch. The branch may exist in the remote repository. If you want to delete a remote branch, then execute the following command.

# git push origin --delete MyOldBranch

18. Merge Two Branches

You can merge two branches by using the merge parameter and the name of the branch. This will combine the specified branch into the main branch.

# git merge MyOldBranch

If you need a merge commit, you can execute git merge with the –no-ff flag.

# git merge --no-ff MyOldBranch

The above command will merge the specified branch into the main branch and generate a merge commit. This is essential to document all merges that occur in your repository.

19. Show Commit Log as Graph for Current or All Branches

You can view the commit log as a graph for the current branch by using the log parameter and –graph –oneline –decorate flags. The –graph option will draw an ASCII graph, which represents the branch structure of the commit history. When it used in association with the –oneline and –decorate flags, it makes it easier to identify which commit belongs to which branch.

# git log --graph --oneline --decorate

If you want to see the commit log for all branches, you can use the –all flag.

# git log --all --graph --oneline --decorate

20. Abort a Conflicting Merge

You can abort a conflicting merge by using the merge parameter and the –abort flag. It allows you to exit from the merge process and return to the state after which the merge began.

# git merge --abort

You can also use the reset parameter to during a merge conflict to reset the conflicted files to a stable state.

# git reset

21. Add a Remote Repository

You can add a remote repository by using the remote add parameter, <shortname> and the <url> of the remote repository.

# git remote add foxapp https://github.com/foxutech..

22. View Remote URLs

You can view the remote URLs by using the remote parameter and -v flag. This will list the remote connections you have to other repositories.

# git remote -v

The above command is an interface to manage a list of remote entries that are stored in the repository’s .git/config file.

You can get detailed information about a remote repository by using the remote show parameter and the name of the remote like origin.

# git remote show origin

The above command will output a list of branches that are associated with the remote and the endpoints that are connected to fetch and push files.

23. Manage Changes to a Remote Repository

You can push changes to a remote repository by using the push parameter, name of the repository, and the name of the branch.

# git push origin main

The above command will help you to upload the local changes to a central repository so that other team members can view the changes you have made.

You can pull changes from a remote repository by using the pull parameter. This will fetch the specified remote’s copy of the current branch and instantly merge it into the local copy.

# git pull

You can also view the details of the files that have been downloaded, by using the –verbose flag.

# git pull --verbose

You can merge a remote repository with your local repository by using the merge parameter and the name of the remote.

# git merge origin

You can push a new branch to a remote repository by using the push parameter, -u flag, the name of the remote, and the name of the branch.

# git push -u origin MyNewBranch

You can remove a remote branch by using the push parameter, –delete flag, the name of the remote, and the name of the branch.

# git push --delete origin MyOldBranch

24. Use Rebase

You can use this feature by using the rebase parameter and the name of the branch. Rebasing is a process to combine or move a sequence of commits to a new base commit.

# git rebase myBranch

The above command will change the base of your branch from one commit to another, which will make it appear as if you have created your branch from a different commit. Git achieves this by creating new commits and applying them to the specified base. It’s very necessary to understand that even though the branch looks the same, it’s comprised of entirely new commits.

RELATED ARTICLES
- Advertisment -

Most Popular

Recent Comments