Git Simplified
We are back after a quite long hiatus, and this time will be posting about Git. After all this is a github web page, so what better topic to resume (and hopefully continue) regular posts proadcast.
I’ll be embedding some gists here about simple and regular tasks a beginner will need to make regularly when working with git.
Setting up Git
You probably have git already available on your system.
To check if you got it installed and which version you use the command git --version
:
If you get a command not found
error you need to install git. It will depend on what system you are running. You can find the different install methods on the official git website at https://git-scm.com/book/en/v2/Getting-Started-Installing-Git. There you will see how to install it on macOS, Windows, and Linux systems. For the sake of completeness I’ll show here how to install on Debian based Linux distributions:
Installing using apt command (Ubuntu, Raspberry OS, etc)
# First update your package list
sudo apt update
# Then install git
sudo apt install git-all
Note: If you later want to upgrade to a new version you can just use the previous install command, it will upgrade git-all
if it’s already installed.
Basic Workflow
When working on small or single developer projects, the git workflow is usually very simple. Let’s see the common tasks you will perform.
Initializing the repository
To initialize the git repository you must use the following command:
# Creates repository with the default initial branch name
git init
Do some work
Let’s run some commands that will create new files in the repository:
# Create a README.md file in markdown format
echo "# RANDOM HEX HASH GENERATOR" > README.md
# Create a script
echo "xxd -u -l 16 -p /dev/urandom > HASHFILE" > hash32.sh
chmod 744 hash32.sh
# Run the script that will create a file called HASHFILE
./hash32.sh
# Check repository status
git status
Stage files to be committed:
These new files are not currently part of the repository. We need to stage them so that git knows we want to add them to the repository:
# Stage README.md
git add README.md
# Stage hash32.sh
git add hash32.sh
# Check repository status
git status
Commit files to the repository
We added two files to the stage area. They are still not part of the repository. We need to issue a commit command along with a description of the committed changes:
# Commit staged files
git commit-m "Add README.md file and hash32.sh script"
# Check repository status
git status
Ignoring files
After performing the commit we see there’s still a file that’s not part of the repository. That file was generated by the script, and we don’t really want it to be part of the repository. We can tell git to ignore the file so it won’t be showed when checking the repository status:
# Create .gitignore and add a line with filename to be ignored
echo "HASHFILE" > .gitignore
# Check repository status
git status
# Add & Commit the .gitignore file so it becomes part of the repository
git add .gitignore
git commit -m "Add .gitignore file"
# Check that working tree is now clean
git status
Checking file differences
Let’s add more information to the readme file. We will check the file differences between committed files and unstaged files using the git diff
command:
# Add a line to README.md
echo "\nRun the hash32.sh script to generate a hash of 32 HEX digits and store it in HASHFILE file." >> README.md
# Check differences
git diff
# Add file to the stage area
git add README.md
# Commit staged file(s)
git commit -m "Modify README.md file"
# Check working tree is clean
git status
Checking commit history
We have done several commits, each one with a description about the changes being committed. We can see a detailed log or resumed version by using the git log
command:
# Basic but relevant info (won't show the filenames)
git log
# Same as before but shows filenames
git log --name-only
# Simplified list (one line per commit)
git log --oneline
# Custom simplified list
git log --pretty='format:%C(auto)%h %ad: %s' --date=short
# Custom simplified list with filenames
git log --pretty='format:%C(auto)%h %ad: %s' --date=short --name-only
Closing comments
This summarizes the basic workflow for local repositories for a single person working on a basic project.
Some more advanced topics will be covered in later articles:
-
Working with remote repositories
-
Using Branches and Branching Methodologies
-
Clonning and Forking Repositories
-
Git Submodules
-
Undo commited changes
-
Checking previous versions of commited files