10 Essential Git commands

10 Essential Git commands

Greetings, fellow developers! If you find yourself in the realm of programming or software development, you've likely come across Git. Most seasoned developers are already well-versed in Git and may not require additional articles and tutorials. However, if you're just starting, Git is an essential skill to acquire.

Whether you hold a degree in Computer Science, work as a Software Engineer, or are a self-taught programming enthusiast, Git is a skill you can't afford to overlook. If Git is still uncharted territory for you, it's high time you delved into it.

If you recognize the significance of Git and are on the hunt for the best resources to master it, you've landed in the right spot. Previously, I've shared the finest free Git courses and the top places to learn Git. In this article, I'll provide examples of fundamental Git commands that every programmer should be acquainted with.

For those who are new to Git, think of it as a source control tool that enables you to version and manage your code effectively. Git also boasts a code repository, known as GitHub, which serves as the home for numerous open-source frameworks that you likely use, such as Spring, Hibernate, JUnit, and more.

Over the past couple of years, Git has established itself as the de facto standard for version control systems in numerous companies. Many of these organizations have embarked on the transition, migrating their existing codebases from SVN or CVS to Git.

Git is essentially a software that you install on your local machine. Once installed, you can access it by typing "git" on the command line. Its primary purpose is to provide versioning for your source code, enabling you to track changes and manage them effectively.

To give you a brief overview of Git's utility, consider this scenario: in a collaborative team environment, both locally and remotely, there's a need for effective collaboration. It's entirely possible that two developers are simultaneously working on the same source file and might accidentally overwrite each other's changes or modify the exact same line. This is where Git steps in.

Git encourages collaboration by allowing everyone to work on their own local copy and commit their changes. When a feature is complete, you can push your changes to a remote repository on your own branch. After testing and verification, you can merge this branch back into the master or trunk branch, making it ready for release.

While this workflow might resemble what other source control tools like SVN, CVS, and Mercury do, Git has a distinct advantage. It operates as a distributed version control system, meaning you can commit changes to your local copy without immediately pushing them to a remote server. This sets Git apart from other source control systems.

Additionally, Git introduces concepts like remote repositories and pushing and pulling changes from these remote repositories. This workflow simplifies code review processes, as you can submit pull requests to merge your code into the master branch, which is typically the default branch for most repositories and is used for releases.

10 Examples of Git Commands for Java Developers

To work with Git, you need to know basic commands like git status, git add, git checkout, git push and many more and that’s what you will learn in this article.

Here, I am going to explain some of the most common Git commands you need for your day-to-day work like inspecting, removing, and tidying with Git.

Even though you can git directly from a command prompt like the DOS command line many programmers prefer to install feature-rich Git clients and there are many git clients for both Windows and Mac/Linux, which you can find on the Internet.

1. git status

This was the first git command I learned, even before the git add command, which is used to add a file in the Git tracking system. This command will tell you about both tracked and untracked files in your current working directory.

I often use this command to find the file which has changed and needs to be committed.

user@host /c/git/course (master)
$ git status
# On branch master
nothing to commit, working directory clean

Since it was a clean checkout and there was no change, git status says nothing to commit but now if you create a file say “readme.txt” and run the git status again, you will see the difference and it will highly that readme.txt is new file and untracked with red color output.

$ git status
# On branch master
# Untracked files:
# (use "git add ..." to include in what will be committed)
#
# readme.txt
nothing added to commit but untracked files present (use "git add" to track)

2. git add

This command adds a file into staging which means now git has started tracking that file. The file will not be committed until you call the git commit but git will track any change.

For example, the file we created in the last example, “readme.txt” is untracked so far. Let’s use the git add command to track that file.

$ git add readme.txt

Now the file is added into staging, if you run the git status again you will see a different output, also file name will be shown in green to indicate that it’s tracked but not committed.

$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD ..." to unstage)
#
# new file: readme.txt
#

3. git branch

This command is used to create a branch in GitHub. You can create a branch, amend a branch and delete a branch using this command.

For example:

$ git branch F1_BRANCH

will create a new branch from the trunk you are in. For example, if you are in master then F1_BRANCH will be branched from HEAD.

4. git checkout

You can use this git command to throw away your local changes and obtain a fresh copy from the repository.

$ git checkout HEAD

will discard all unstaged changes since HEAD, or the last commit. It’s best for local-only updos as it doesn’t affect the commit history from a remote branch that your team depends upon.

you can also use the git checkout command to download code from a specific branch. For example :

$ git checkout F1_BRANCH

will check code from F1_BRANCH and your working directory will be updated to match.

5. git log

This command is used to see all commit history. You can also use this command to see the history for a file as shown in the following example:

$ git log -p readme.txt

6. git diff

This is another useful git command to see all files which have been changed locally. The command will show exactly what has changed in all files as well.

$ git diff readme.txt

Tip: You can provide a file name to show changes for only one file.

7. git blame

This is another lesser-known git command which can be used to find out who has changed the file as shown below:

$ git blame readme.txt

Above command will show who has changed this file what has been added and when.

8. git reflog

Another git command which many programmers overlook. You can use this git command to show a log of changes to the local repositories’ HEAD.

$ git reflog

Tip: This command is good for finding lost work. Last four git commands are useful for inspecting things.

9. git reset

You can use this command to throw away any local commit you have made. For example:

$ git reset --hard HEAD

discards staged and unstaged changes since the most recent commit. you can change the HEAD to any commit hash to discard changes since that commit. HEAD is just a nice pointer for the most recent commit.

On the other hand — the hard option tells that both the staged and unstaged changes are discarded.

10. git revert

You can use it to revert a commit in the remote branch. Unlike git reset and git checkout which works on both commit and individual file levels, git revert only works at the commit level.

$ git revert last_commit

reverts or undos the effects of changes in last_commit. It’s worth noting that git revert makes a new commit when it reverts the changes into the repository.

You can use these three git reset, git checkout, and git revert commands to undo the effects of changes to your repository, though there is a slight difference between them.

Tip: If you are just working with your local commits that haven’t been merged into your teams’ remote branch like master then you can use any of these commands to undo the change but if it’s merged and you want to neutralize a commit then only git revert is the command to use.