9

10 Git commands Every Programmer Should Know

 9 months ago
source link: https://javarevisited.blogspot.com/2022/06/10-examples-of-git-commands-for-java.html#axzz8BJkNq0uN
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

10 Git commands Every Programmer Should Know

Hello guys, if you are a Programmer you probably have heard about Git, well most of you might have already been veteran into Git and don't need any more articles and tutorials, but if you are a beginner then Git is one of the most important skill to learn. If you are a Computer Science graduate, a Software Engineer, or even a Student trying to learn Programming on your own, Git is a must-know skill for you. If you already know the importance of Git and looking for best resources to learn Git then you have come to the right place. Earlier, I have shared best free Git courses and best places to learn Git and in this article, I am going to share examples of essential git commands which every programmer should know.
16693691941663808d6a52ef6.png
Loaded: 0.16%
If you don't know what a Git is then just remember that It's nothing but a source control tool which allows you to keep your code versioned and managed. It also has a code repository, known as Github which is home for many open source framework you use like Spring, Hibernate, JUnit etc. 
In last a couple of years, Git has become the defacto standard version control system for many companies and many of them moving their existing codebase from SVN or CVS to Git. This means, now there is no escape from Git, even for experienced developer who are used to of CVS and SVN, Git is a must-know. 
Git is nothing but a software you install on your machine. Once you install it, you can access it by typing "git" on command line. It provide versioning for your source code, so that you can track changes and manage them effectively. 
If you are thinking what exactly is Git and how it helps let me give you a brief overview. When you work within a team, both remote and local, you need a bit of collaboration. It's possible that two developers are working on same source file and may accidently overwrite each other changes or might be changing the exactly same line. Like any other source control tool Git helps you there.
It encourages collaboration and let everyone work on their own copy and commit there. Once a feature is complete, you can push your changes to remote repository on your own branch. Once testing is finished, you merge the branch back to master or trunk and release it. 
That's what other source control tools like SVN, CVS and Mercury does but Git is a little bit different as it's a distributed version control, which means you can commit on your local copy without pushing anything on remote server, and this is what separates git from other source control. 
It also gives birth to new concepts like remote repository and pushing and pulling changes from remote repository. This workflow also makes the code review a lot easier as you raise pull request to merge your code to master which is default branch for most repository and also used for releasing stuff. 
10 Git Commands Every Java Programmer Should Know

10 Examples of Git commands for Java Developers

In order to work with Git, you need to know basic commands lie 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 command prompt like DOS command line many programmer prefer to install feature rich Git clients and there are many git clients for both Windows and Mac/Linux, which you can find on Internet. 
Since I develop on Windows I like to use Git Bash, it provides Cygwin like command prompt and also has some useful features like it always shows which branch you are currently on. 
It also shows colored output which makes it easy to spot things and the best part of it, you can use most of the Linux commands like ls, cd, less etc from Git Bash command window.

1. git status

This was the first git command I learned, even before git add command, which is used to add a file in Git tracking system. This command will tell you about both tracked and untracked file 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 say 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 last example, "readme.txt" is untracked so far. 
Let's use 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 branch, amend branch and delete 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.

6. git checkout

You can use this git command to throwaway your local changes and obtain a fresh copy from repository. 
$ git checkout HEAD
will discard all unstaged changes since HEAD, or last commit. It's best for local-only undos as it doesn't affect the commit history from a remote branch that your team is depending upon. 
you can also use git checkout command to download code from a specific branch. For example :
$ git checkout F1_BRANCH 
will checkout code from F1_BRANCH and your working directory will be updated to match. 

3. git log

This command is used to see all commit history. You can also used this command to see history for a file as shown in following example:
$ git log -p readme.txt 


Tip: Please enter q to exit.

4. git diff

This is another useful git command to see all file 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. 

5. 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 and what has been added and when.

6. git reflog

Another git command which many programmer 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. In fact, last four git commands are useful for inspecting things. 

7. git reset

You can use this command to throwaway 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 most recent commit. 
On the other hand --hard option tells that both the staged and unstaged changes are discarded.

7. 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 level, git revert only work at the commit level. 
$ git revert last_commit
reverts or undo the effects of changes in last_commit. It's worth noting that git revert makes a new commit when it revert the changes into 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 own 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. 

8. git clean

You can use the git clean command to remove untracked files in your local directory. For example, when you run the program it may create some .log files in your working directory, you can get rid of that by using this command.
$ git clean -n 
Can delete untracked files in the local working directory. Though you should also take a note of some useful flags like: 
The -n flag is for a dry run where nothing is deleted.
The -f flag to actually remove the files.
and, you can use -d flag to remove untracked directories.
Tip: Worth noting is that by default files untracked by .gitignore will not be deleted, but you can change this behavior if you want to.

9. git commit

This command is used to commit your changes. It's worth noting that when you commit a change in git its only done in local working directory. The change will not go to remote branch until you use git push command. 
$ git commit -m
allows you commit a change with commit message. If you want to change the message, you can also use the
$ git commit --amend 
which allows you to edit the most recent commit message.? It can also add your staged changes to the most recent commit, if there is any. 
Tip: Only use this command if the commit has not been integrated into the remote master branch.
That's all about some of the most essential Git commands every Java Programmer, Developer, Software Engineer should know. If you have been using Git then there is a good chance that you already familiar with these commands but if you are seeing it first time then try to practice them on your own repository. This will help you to learn more and better. 
Now that you have good Intro to Git and Github you can checkout following resource to learn more:
Further Learning
Thanks for reading this Git command tutorial for beginner Java programmers, if you like this article and git examples then please share with your friends and colleagues. If you have any questions or feedback then please drop a note.

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK