Git is one of the most popular software source code control version system (CVS).

This post explains the main aspects of Git.

You may want to read about alternative source code CVSs on this post.

Explaining what is Git

Git is free and open source (FOSS) control version system (CVS). As its code is publicly available under a copyleft GPL-2.0-only license, it can be implemented by anyone.

It was developed initially by Finnish developer Linus Torvalds, the same person that created the Linux kernel.

Git can be downloaded and installed on a personal computer or server.

Official website

Many popular CVS cloud services, like GitHub, are based on Git. The concepts of Git and these cloud services are very similar, so if you want to use any of these services it is important that you are familiar with Git.

You can read more about CVS cloud services, including those that are Git-based like GitHub and GitLab, on this post.

Git Concepts

The source code of a software project is stored on a code repository, also known as repository or repo.

A branch is a version of a project. There is usually a main branch on all projects. When a developer wants to make changes on a project, it usually uses its own branch.

A commit is a snapshot of the project at a particular point. One of the interesting functionalities of code repositories is version history, and storing different commits enables it by keeping different snapshots of the same project.

A pull request is a proposal to merge the changes from or a branch into another branch or from a a clone repository to the original repository.

Pull requests can be accepted or rejected.

Then, as a last step, changes are merged.

Git Files

gitignore is a file that specifies all files that should be ignored by Git. It is used to ignore, for example, system folders or files that are used locally by the OS of a personal computer and should not be pushed to a common repository.

You can find more information about gitignore on this external link.

Git Basic Commands

This section show some basic commands.

Git Help

git tag lists the available tags.

Commands to initialize a Git Project

git init creates a new Git repository within the current directory.

git clone import a copy of an existing project on the internet to your local machine. It creates a directory with the project name within the current directory.

The syntax would be:

git clone https://example.com

Commands for Making Changes

git status: Check the current changes.

git add : Stage the changes on files so they are taken into account when committing.

git add . or git add -A: Adds all files that have been changed at once.

git commit -m “Commit message”: Seal the deal on your changes with a commit and a handy message to remind you what you did.

Commands to operate with branches

git branch: Curious about your branches? List them all out with this simple command.

git checkout -b new_branch_name: Grow your project with a new branch using this command.

git checkout existing_branch_name: Jump over to another branch to keep working seamlessly.

git merge : Done with changes on your side branch? Bring them back to the main branch with a merge.

Commands for working with Remotes

git push origin : Send your latest commits up to the cloud with this push command.

git pull remote_project remote_branch: fetch and merge the changes from a remote branch to your current branch.

git fetch:

git remote -v: Check your connections with this command that lists remote repositories linked to your local repo.

Understanding the Differences between Commands

git fetch vs git pull: Both bring data from remote repositories, but git fetch is like previewing, while git pull is like downloading and updating your files in one go.

git pull = git fetch + git merge

git merge vs git rebase: Both integrate changes from one branch to another, but git merge makes a new commit for it, while git rebase keeps your history neat and tidy.

git reset vs git revert: Need to backtrack? Use git reset to discard changes or git revert to undo while keeping your commit history intact.

How to create a Pull Request

A pull request is a request that a user does to suggest the original author of a project to modify it.

In the examples, it is common to call “upstream” the remote project and “origin” the local forked project.

An overview about how to create a pull request:

  1. Fork the Project
  2. Clone the forked project
  3. Link your local project to the remote project
  4. Create a branch
  5. Add changes to file/s
  6. Commit the changes
  7. Push your changes
  8. Create pull request
  9. Project owner evaluates the changes
  10. Pull changes

1. Fork the project

If the fork is in GitHub, login, visit the upstream project and click on dropdown list “Forks” and select “Create fork”.

It asks which is the branch you want to fork. It is usually the “master” or “main” branch.

Confirm and wait some seconds

2. Clone the forked project

In your local computer, open a terminal, go to a folder and type:

git clone https://forked.project/url.git

Substitute https://forked.project/url.git with the forked project URL.

It will create a new folder with the same name as the project. It contains all the files from the cloned project.

3. Link your local project to the remote project

Enter the new local project folder created from the terminal.

Link your local project to the remote project by typing:

git remote add upstream https://original.project/ur

Substitute https://original.project/url with the original project URL.

The command sets the name “upstream” to refer to the original project. This is a common convention, but you could name it however you prefer. The command also links the local project to the original project.

4. Create a branch

It is not recommended to make changes on the main branch, so we are switching to a different one.

To switch to a different branch type:

git checkout -b your-branch-name

5. Add changes to file/s

Perform changes on files.

If you want to modify the files using VS Code you can enter into the project folder:

code .

Remember to save the changes once you are finished.

Once changes are done, you must add them by typing:

git add .

6. Commit changes

Commit changes by typing:

git commit -m "Changes summary"

You can change “Changes summary” to an actual summary of the changes you have applied.

7. Push your changes

Push the changes to your cloned repository by typing:

git push origin your-branch-name

Substitute your-branch-name with your branch name.

You may need to provide access to your account, and you may use the classic tokens on this external link.

8. Create pull request

Create the pull request.

If you are using GitHub, go to the forked repository at GitHub and request the pull request.

9. Project owner evaluates the changes

The project owner will evaluate the changes, and accept them or reject them.

They may ask you to add some changes before resubmitting.

10. Pull changes

In case some changes are done in the original project after its acceptance, the current project must be updated:

git pull upstream main

You might also be interested in…

External references

Leave a Reply

Your email address will not be published. Required fields are marked *