Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 13, 2021 11:16 am GMT

Top 10 Git Commands Every Developer Should Know

Hello Everyone ,

Git is an important part of daily programming (especially if you're working with a team) and is widely used in the software industry.

Since there are many various commands you can use, mastering Git takes time. But some commands are used more frequently (some daily). So in this post, I will share and explain the top 10 Git commands that every developer should know.

1. Git Init

The git init command is used to initialize a blank repository. It creates a .git folder in the current working directory

$ git init

The above command will create an empty .git repository. Suppose we want to make a git repository on our desktop. To do so, open Git Bash on the desktop and run the above command. Consider the below output:
git init

The above command will create a new subdirectory named .git that holds all necessary repository files. The .git subdirectory can be understood as a Git repository skeleton. Consider the below image:
git init 1

An empty repository .git is added to my existing project. If we want to start version-controlling for existing files, we have to track these files with git add command, followed by a commit.

2. Git Commit

The git commit command is used to save the changes to the local repository. This command will help you keep a record of all the changes made. We also need to write a short message to explain what we have developed or changed in the source code.

$ git commit

The above command will prompt a default editor and ask for a commit message. We have made a change to newfile1.txt and want it to commit it. It can be done as follows:

Consider the below output:

git commit

As we run the command, it will prompt a default text editor and ask for a commit message. The text editor will look like as follows:

git commit 2

Press the Esc key and after that 'I' for insert mode. Type a commit message whatever you want. Press Esc after that ':wq' to save and exit from the editor. Hence, we have successfully made a commit.

We can check the commit by git log command. Consider the below output:

git commit 3

We can see in the above output that log option is displaying commit-id, author detail, date and time, and the commit message.

3. Git Status

The git status command is used to display the state of the current repository & staging area.

We can gather information these information using git status command:

i) Whether the current branch is up to date

ii) Whether there is anything to commit, push or pull

iii)Whether there are files staged, unstaged or untracked

iv) Whether there are files created, modified, or deleted

$ git status

Consider the below output:

git status

In the above output, the list of all untracked files is displayed by the git status command. We can track all the untracked files by Git Add command.

4. Git Add

When we create, modify or delete a file, these changes will happen in our local and won't be included in the next commit (unless we change the configurations).

We need to use the git add command to include the changes of a file(s) into our next commit.

To add a single file:

$ git add

Consider the below output:

git add 1

From the above output, we can see newfile.txt has been added to our repository. Now, we have to commit it to share on Git.

To add everything at once:

$ git add -A

or

$ git add .

git add 2

In the above output, all the files are displaying as untracked files by Git. To track all of these files at once, run the below command:

$ git add -A

The above command will add all the files available in the repository. Consider the below scenario:

We can either create four new files, or we can copy it, and then we add all these files at once. Consider the below output:

git add 3

In the above output, all the files have been added. The status of all files is displaying as staged.

5. Git Merge

When you've completed development in your branch and everything works fine, the final step is merging the branch with the parent branch (dev or master). This is done with the git merge command.

The git merge command is used to integrate different branches into a single branch. It's important to remember that you first need to be on the specific branch that you want to merge with your feature branch.

$ git merge

git merge

6. Git Push

After committing your changes, the next thing you want to do is send your changes to the remote server. Git push uploads your commits to the remote repository.

$ git push -u origin

git push 1

The file abc.jpg is successfully pushed to the origin master. We can track it in a remote location. I have pushed these changes to my GitHub account. I can track it there in my repository. Consider the below image:

git pull 2

In the above output, the pushed file abc.jpg is uploaded on my GitHub account's master branch repository.

7. Git Pull

The git pull command is used to get updates from the remote repo. This command is a combination of git fetch and git merge which means that, when we use git pull, it gets the updates from remote repository (git fetch) and immediately applies the latest changes in your local (git merge).

$ git pull

git pull

8. Git Clone

Git clone is a command for downloading existing source code from a remote repository (like Github, for example). In other words, Git clone basically makes an identical copy of the latest version of a project in a repository and saves it to your computer.

$ git clone https://name-of-the-repository-link

git clone

9. Git branch

Branches are highly important in the git world. By using branches, several developers are able to work in parallel on the same project simultaneously. We can use the git branch command for creating, listing and deleting branches

$ git branch

git branch

This command will create branch B1 locally in the Git directory.

10. Git Checkout

This is also one of the most used Git commands. To work in a branch, first you need to switch to it. We use git checkout mostly for switching from one branch to another. We can also use it for checking out files and commits.

$ git checkout

git checkout

As you can see in the given output that master branch has switched to TestBranch.

So these are my 10 most-used git commands that I come across in my daily programming. I hope you find it useful. If yes then let me know in the comments.

Also if you got any question feel free to ping me on Twitter

You can now extend your support by buying me a Coffee.

Buy Me A Coffee

Thanks for Reading


Original Link: https://dev.to/muthuannamalai12/top-10-git-commands-every-developer-should-know-422l

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To