Git is possibly the most used version control system in use today. I’ve used tools other than Git, such as GitHub, GitLab, Bitbucket, and SVN. However, here I will share those Git commands that I use the most and what you may encounter.
You can use these Git commands in conjunction with the terminal, where you may input specific commands to accomplish tasks. If you like graphical user interfaces, this may not be for you. However, if you want to learn useful Git commands that you can use every day like Hackerman and improve your IT skills, you may keep reading.
1. git init
Git init command is used to create an empty Git repository. Essentially, it is to establish an open Git repository in the desired location. A particular folder, .git, will be there as soon as you run this command.
After that, you may begin using Git commands in the directory.
$ Git init
2. git clone
Git clone command is a powerful command, and you can use it to clone or download source code from a remote repository (such as Github, for example) into your directory.
It essentially copies the most recent version of a project from the remote repository and stores it on your PC.
I usually use the link to clone because it is more familiar to me.
Run $ git clone [your repo link] to clone the project repository into the main branch.
3. git branch
Git branch will provide a list of all the Git branches currently available on your local machine. It is a famous Git command. You can use this command to create a new git branch as well.
Branches are crucial for cooperation and keep the main branch from getting messed up by incomplete or untested code.
The git branch command may be used to create, list, and delete branches.
Use $ git branch [your-branch-name] to establish a new branch (locally).
4. git checkout
Git checkout is also one of the most commonly used commands, mainly when working with many branches.
You can use the Git checkout (Like the swiss knife command) when switching to a different branch. Also, you can use it to check out files and commits.
Run $ git checkout [another-branch-name] to move to another branch.
As an example:
$ git checkout my_branch
There are specific criteria that you may use to switch to the other branch:
Before switching, the modifications in your current branch must be committed or stored.
The branch you wish to visit should be available in your area.
Another use would be to update a file in your branch and revert to the old version. Run the command $ git checkout [path].
5. git status
Git status command provides us with all of the information we require about the current branch.
Details such as:
- Check to see if the current branch is up to date.
- Whether there is anything to do with commit, push or pull.
- Whether there are files that are in the staging area or are untracked.
$ git status
The procedure usually requires that the files that have changed (made, updated, or deleted) and in the staging area first, and then you may commit it by adding all of the files to the stage. You may push the files to the remote repository after committing.
6. git fetch
You can run the $ Git fetch to retrieve project updates from your remote repository.
Git fetches updates for your accessible branches, which you can see both locally and remotely. You may use $ Git fetch –all to get all the updates for all remote branches.
7. git pull
This command is more often used than the Git fetch command.
Like Git fetch and git merge, this command is used to get updates from the remote repository.
Git pull implies that after retrieving the updates, the changes are immediately applied to your local files.
$ git pull
Because conflicts are possible, I recommend always performing a git pull before pushing any modifications.
As a result, Git pull is usually preferable to first pull the changes to your local computer and resolve any issues. Then you can raise it to ensure a smooth merging procedure later on. Git pull command also prevents any problems from arising for your colleagues.
8. git add
When changes are made, whether they be creation, deletion, or modification, they are made locally and are not yet reflected remotely.
Typically, once the changes are complete, you will need to stage the files so that you can execute the git commit command to have your altered files committed to the changes — or stored in a sense.
Then you execute a git push to push the changes to the remote repository, where they will be mirrored remotely as well.
As a result, the first step is to place your files on the stage.
How? By using the command $ Git add.
This command will place all of your files on the stage. You can also perform the following to add a single file, do $ Git add [file_name].
9. git commit
Git Commit is most likely the most commonly used command.
It would help if you used this every time you wish to save your modifications.
When you need to push your changes, you must first execute this command.
You would use this command even if you did not plan to push your modifications.
After resolving a problem or completing a job, you could commit it first to verify that the changes are locked locally by creating a checkpoint. It’s almost like a staple.
There are two approaches to this:
Adding a brief message to a commit: $ Git commit -m “commit message.”
Adding a lengthier message to the commit: $ git commit.
10. git push
This command is executed after you have committed your modifications to transmit your changes to the remote server:
$ git push
Git may appear daunting at first, especially if you’ve just begun your adventure and see this extensive list of command possibilities. There are many advanced Git commands, However, as you become more involved in projects that utilize Git often, you will gradually begin to see how you use some of the commands daily.
And when you begin to list them, you will discover how many commands you have used yourself. You’ll eventually know these like the back of your hand.
Don’t stubbornly push your modifications up without first fetching changes from the remote repository. This might pose problems not just for your project but also for your team members.
A clear commitment message will benefit your initiatives in the long run, and I wish you the best of luck on your exciting trip!
That concludes today’s list. If you know of any more popular Git commands, there are many resources available like opensource.com, stack overflow, community blog, and many learning resources. Please share them in the comments area below! We’d all want to learn something together.