One of the most useful programming tools is version control. It helps you to keep track of what you’ve done and when, reverse any changes you don’t like, and cooperate with others at scale.
In this post, we’ll teach you how to use Git, a modern version control tool popular among data scientists and software engineers, to get more done in less time and with less stress.
What is version control?
A version control system is a tool that keeps track of changes to a project’s files and folders. There are a variety of version control systems available; this article concentrates on Git. It has the following advantages:
- Nothing you save to Git is ever deleted, so you can always go back and examine which versions of your programs produced which outcomes.
- It’s harder (but not impossible) to mistakenly replace work using Git since it warns you when your work clashes with someone else’s.
- Git can synchronize work done by several individuals on multiple machines, allowing it to expand with your team.
Where does Git store information?
Each of your Git projects is made up of two parts: files and folders that you create and modify directly, and supplementary information about the project’s history that Git stores. A repository is a result of combining these two elements.
All of Git’s additional information is saved in a directory called
.git in the repository’s root directory. You should never alter or remove anything in the
.git file since Git wants this information to be written up in a very specific way.
How can I check the state of a repository?
When using Git, you’ll want to check the status of your repository regularly. Run the command
git status to get a list of all the files that have been edited since the last time changes were stored.
How can I tell what I have changed?
Git features a staging area where it keeps files that haven’t been saved yet but have modifications you wish to save. Putting files in the staging area is like putting items in a box, and committing those changes is like mailing that box: you can add more things to the box or take things out as frequently as you like, but once it’s in the mail, you can’t make any more changes.
git status displays which files are in this staging region, as well as which files have modifications that haven’t been committed yet. You may use
git diff filename to compare the current state of the file to the last time it was saved.
git diff without any filenames will display all changes in your repository, whereas
git diff directory will display changes to files in a specific directory.
What is in a diff?
A diff is a formatted comparison of two sets of files.
What’s the first step in saving changes?
There are two steps to committing changes to a Git repository:
- Add one or more files to the staging area.
- Commit everything in the staging area.
git add filename to add a file to the staging area.
How can I tell what’s going to be committed?
You may use
git diff -r HEAD to compare the condition of your files with those in the staging area.
HEAD is a shorthand for “the most current commit,” while the
-r flag implies “compare to a specific revision.”
You may use
git diff -r HEAD path/to/file to limit the results to a particular file or directory, where the path to the file is relative to where you are.
Interlude: how can I edit a file?
There are a surprising number of text editors available on Unix. In this post, we’ll tell you about a very basic one called
Nano. If you type
nano filename, it will open that file for editing (or create it if it doesn’t exist). The arrow keys may then be used to navigate through the file, while the backspace key can be used to erase characters, and so on. Control-key combinations can also be used to do the following tasks:
- Ctrl-K: delete a line.
- Ctrl-U: un-delete a line.
- Ctrl-O: save the file (‘O’ stands for ‘output’).
- Ctrl-X: exit the editor.
How do I commit changes?
git commit is used to store changes in the staging area. It always saves everything in the staging area as a single unit.
Git requires you to add a log message when you commit changes. This serves the same goal as a program comment: it informs the next person inspecting the repository why you made a change.
Git opens a text editor by default, allowing you to add this message. To keep things simple, use the command line option
-m "some message in quotes" to input a single-line message like this:
git commit -m "This is a message."
You may use the —amend flag to alter a commit message if you enter it incorrectly.
git commit --amend - m "new message"
How can I view a repository’s history?
To examine the project’s history log, run the command git log. The most recent log entries are displayed first, and they look like this:
Author: Projects Engine <[email protected]>
Date: Wed Sep 20 13:42:26 2017 +0000
Added year to report title.
The commit line shows a hash, which is a unique identifier for the commit.
How can I view a specific file’s history?
Because inspecting only the changes to certain files or directories in a project’s log might be overwhelming, it’s frequently beneficial to check only the changes to specific files or directories. This may be accomplished using the command
git log path, where the path is the path to a specified file or directory.