Intro to Git
GitHub is a web-based platform for version control and collaborative software development. It provides a platform for developers to host and manage their code repositories, collaborate with others, and track changes to their code base.
In this article you will learn about the basics of Git, from function to operation.
Key Principles
Git works as a version control, imagine you’re working on a project and you need to collaborate with others, or even you have a production code running and meanwhile the local version already have some updates but can’t be published yet, that’s where git can help, because it tracks commits and you can move from commits and do changes.
In terms of repositories you have local and remote, and the way of interacting is you can pull a repo if you want to receive from remote to local, you can push a repo if you want to send from local to remote.
Another detail important to know is in your local environment, there are 3 stages:
- Working
- Staging
- Commited
The working environment is the base in which you operate, you develop your code. Once you finished coding you send it to staging by running the “git add” command, and to send to the commited stage you run “git commit”. You will see this more in detail later on, but this is the key principles of dealing with Git.
Installing Git
In order to install git you can take two approaches, via homebrew or apt install, let’s see:
Note: be sure to run “sudo apt update” previously
brew install git
sudo apt install git -y
To check if it was install successfully you can run:
git --version
Another package you should install is git man pages, so you can learn more about each command, before running and doing a unwanted mistake:
sudo apt-get install git-man
Basic Commands
Now the first thing to have git working is to initialize it for that you run:
git init
This will create a folder .git which from now on will track your changes in that folder, thus it’s good to have a specific folder to work and initialize git.
Once you add a file, for example:
touch story.txt
echo "<insert something here>" >> story.txt
And then run:
git status
Here you can see information about the current branch you’re at, if there were commits done and which are the untracked files. Once you add they will will in staging, but if you modified the modification will be in untracked mode, with the information saying that the file already exists but there are modifications not staged yet.
To send a file to staging you run:
git add <file>
To commit you run:
git commit -m "<insert a message that is clear of the changes made>"
On thing to have in mind is that the commits must be atomic, you must not change to multiple things at once and commit, every commit must focus on a specific change or task performed.
If you want to return a file to untracked you can run:
git restore --staged <file>
Also there are files that must be ignored, like compilation files and other alike, for that you can use the .gitignore file, you just have to create a file with that name and insert the files names or folders names that you don’t want to be tracked.
One last command is:
git log
Git log enables you to see information about the commits, get the commit hash that you can use for cherry picking and squashing, the author, date, and commit message.
Conclusion
From here you know how to setup git use the basics, in the following articles about git you can learn more about branches and operations over commits and branches.