Git is a widely-used, open-source version control system that enables developers to effectively track and manage the changes made to their source code. It allows them to see who made specific changes, when they were made, and why, making collaboration and version management much easier. Mastery of key Git commands is essential for developers to fully utilize its capabilities while understanding a large number of commands is not always necessary in day-to-day use.
In this blog, you will discover the essential Git commands that will elevate your development skills and workflow.
1) git init
Usage: git init
The "git init" command initiates a new Git repository, making it the initial step in creating a project on GitHub. To do this, navigate to the directory that holds your project files and execute the command. This will add a hidden ".git" subdirectory to the directory, marking it as a version-controlled repository.
You can specify a name for the repository while running the git init command to identify it easily.
2) git clone
Usage: git clone [url]
The git clone command creates a local copy of the source code from a remote repository, allowing you to work on the code locally. This will automatically download the repository's code to your machine, and it will be ready for use.
The git clone command also adds the remote repository as a source, so that you can both retrieve updates from it and also submit your own changes if you have the necessary permissions.
3) git branch
Usage: git branch [branch-name]
It creates a new branch locally.
Usage: git branch -a
It lists all the local and remote branches.
Usage: git branch -a or git branch --list
It views all branches and sees which branch you’re currently working on.
Usage: git branch -d [branch-name]
It deletes a branch.
Usage: git branch -m [branch-name] [new-branch-name]
It renames a branch.
4) git checkout
Usage: git checkout [branch-name]
It switches to an existing branch
Usage: git checkout -b [new-branch-name]
It creates and switches to a new branch.
5) git add
Usage: git add [file-name1] [file-name2]
It adds specific files.
Usage: git add --all
It adds all new, modified, and deleted files.
Usage: git add -u
It adds modified and deleted files.
6) git commit
Usage: git commit -a
The git commit command saves any newly added files that were specified using the git add command, as well as any changes made to previously tracked files, to the local repository.
Please note that after running the git commit command, your default text editor will open to allow you to write a message describing the changes made in the commit.
Usage: git commit -m “commit-msg”
It commits files with a message.
Usage: git commit -am “commit-msg”
This can replace the two previous commands with the above single command.
Usage: git commit --amend -m “commit-msg”
Modify the last commit with the latest changes as a new commit.
7) git push
Usage: git push or git push [remote] [branch-name]
The git push command is used to upload committed changes from your local repository to a remote one, making them available for other users. It also creates a named branch in the remote repository if one with the same name does not exist, allowing you to keep your local and remote copies in sync.
In order to make your new branch visible in a remote repository, you will need to push it using the git push command along with the specific branch name.
git push --set-upstream [remote] [branch-name] or git push -u [remote] [branch-name]
8) git pull
Usage: git pull or git pull [remote] or git pull [remote] [branch-name]
"git pull" is a command used in Git to update the local version of a repository with changes that have been made to its remote counterpart. When you run "git pull," it fetches the latest changes that have been pushed to the remote repository by your teammates. This includes any new commits, branches, or file modifications. These changes are then merged into your local repository, allowing you to have the most up-to-date version of the codebase. It is important to note that "git pull" is a combination of "git fetch" and "git merge", It fetches the changes from remote and merges them with your local branch. It is a best practice to pull changes frequently so that you can stay in sync with your teammates and avoid conflicts when committing your own changes.
9) git merge
Usage: git merge [branch-name]
The git merge command combines changes from a specified branch with the parent branch. Typically, the parent branch is either the development or master branch, depending on your workflow. It creates a new commit automatically when there are no conflicting changes. To run this command, ensure that you are currently on the branch that you wish to merge with the parent branch. For example, to merge changes from a feature branch to the development branch.
Switch to the branch that you want to merge everything in:
git checkout [branch]
It's worth noting that before merging, it is important to ensure that your local parent branch (e.g., development or master) is up to date with the remote repository, as your team members may have already made merged their own changes while you were working on your branch.
Command to fetch the latest updates:
git pull or git fetch
After pulling updates from the remote repository and ensuring there are no conflicts, you can safely merge your changes into the parent branch.
Merge your branch:
git merge [branch-name]
10) git status
Usage: git status
"git status" is a command used in Git to check the current status of the local repository. When you run "git status," it will display the current branch that you are on, as well as any files that have been modified, added, or deleted. It will also show you any changes that have been made to the local repository that have not yet been committed. Additionally, it will indicate if there are any untracked files in the repository. This command is useful for quickly identifying which files have been changed and what the next steps should be for committing and pushing those changes to the remote repository.
These are some of the useful git commands used by every developer. Other than these commands, there are many more. Using these git commands in your day-to-day coding life will enhance your productivity.