I’ll show you the bare minimum of Git commands you’ll need to get started being productive in this post. And these commands will help you to start using source control on your computer in the Git repository.
Let me begin by addressing a few issues:
Let’s say you’re writing a lecture, and you make some changes, save them, exit the text editor, and then return to the file. You want to see all of the modifications you made last time. Or maybe you want to erase some changes you made the night before (writing a lecture after coming from a party is a noble act, but it’s not very clever!).
You produce multiple versions of the same file while writing this seminar and have no idea what’s changed in each one; you’d like to know what was changed in this current version compared to, say, a version from a few days ago. You’re working on a collective project when someone accidentally deletes your edits.
These issues are usually solved by VCS (Version Control Systems). And, first and foremost, you do not need to utilize VCS to develop code (thus the seminar example). It may use for any text-based project. Git is one of the VCS options. Subversion is another prominent VCS that is still used to some extent today.
What is Version Control System
You use version control to keep track of different versions of your papers. True, it appears to be highly popular among engineers— still, its use to monitor versions of files by other professionals like designers and other individuals.
One of the important things that set Git apart from other VCSs is the distributed component. Rather than having a single repository for the whole version history of the program, as is the case with once-popular VCS such as CVS or Subversion, Git makes every developer’s working copy into a repository that can hold the entire history modifications.
We’ll need to make some fast settings now that we’ve installed Git on our machine. There are many choices to play with, but we’ll focus on the most crucial ones: our username and email address.
Run the following commands in a terminal:
$ git config –global user. Name “Name”
$ git config –global user.email “firstname.lastname@example.org”
With this configuration change, every action has a name and address stamped on it. Users will always know who did what, and everything will be visible in a more excellent way.
2.Create a Repo
As previously stated, Git keeps its files and history in your project as a folder. We need to open a terminal, browse our project directory, and run the Git init command to create a new repository.
Git init command will enable this folder and create a hidden .git directory to hold the repository history and configuration.
Create a folder named git exercise on your Desktop, then open a new terminal and type the following:
git init $
Something along the lines of: should be easily visible from the command line.
In the folder /home/user/Desktop/git exercise/.git/, created an empty Git repository.
The empty directory indicates that our repository is ready to use, but it remains empty.
Now you can create and save a simple text file named hello.txt in your folder.
3.What is Git status
Another must-know command is git status, which gives information about the current condition of the repository.
Git status tells if everything is up to date, what’s new, and what’s changed. Running git status on our freshly established repository should tell you about the situation of files.
$ git status
Hello.txt is untracked, according to the message returned. The message indicates that the file is new, and Git is unsure whether or not it should keep track of changes to it or disregard it. We must stage the new file to recognize it. Let’s jump to the following git command.
4.Staging your files
A “staging area” is a notion in Git. You may think of it as a blank canvas to paint the modifications you want to make. It starts empty, but you may use the Git add command to add files (or even single lines and portions of files) and then use Git commit to commit everything (make a snapshot).
We have one file in our example, so let’s add it:
$ Git add hello.txt
And in case there are multiple files, you can use the following git command.
$ Git add -A
Now, if you recheck the status, you will get a different message.
Our file is now ready for commit. The Git command status message also tells us what has changed in the staging area’s files – in this example, it’s a new file, but depending on what has occurred to a file since the last Git add, it might be updated or removed.
5. git commit
In simple words, commit is a snapshot of your repository at a specific moment in time. It’s like a picture that we can look back on to see how things were at the time.
To make a new commit, we must first add at least one modification to the staging area (which we did with git add) and then perform the following commands:
$ Git commit -m “Initial commit.”
Git commit will generate a new commit, including all of the modifications made in the staging area (adding hello.txt). The -m “Initial commit” parameter provides a user-written description of the changes made in that commit. Committing frequently and always writing relevant commit messages is considered excellent practice.
When working on a new feature, it’s a good idea to work on a branch, which is a clone of the original project. Branches have their histories and keep their modifications separate until you wish to join them again.
There are many advantages of using branches like
- It will not break your existing code in the main branch if something goes wrong in your new branch.
- Several aspects can be created safely at the same time by separate persons.
- Developers may work on their branch without worrying about their codebase altering as a result of the work of others.
6. Create new branches
Every repository’s default branch is main.
Use the git branch <branch_name> command to create new branches:
$ git branch your_feature_branch
8. Switch your branch
When we run the git branch now, we’ll notice that we have two options:
$ git branch
The current branch, denoted by an asterisk, in our case, it’s main.
However, we need to move to the other branch to work on our new features.
You can switch using the git checkout command, which takes only one parameter: the branch name.
$ git checkout your_feature_branch
It’s time to conclude our basic git tutorial!
In this article, I did my best to provide you with the most necessary information, presented most shortly and succinctly possible. Git is a sophisticated tool with a lot of extra features and gimmicks. I hope this tutorial will provide you with an idea about Git and how easy it is to learn Git.