Complete GIT Guide – Basic Operations

Learn how to use the basic commands on GIT to create a repository, update and push content and keep track of changes.

Joshua González

Estimated reading time: 14 minute(s)
September 17, 2024

git init – Initialize a repository

The git init command is the first step to start using GIT in a project. Its function is to convert an existing directory into a GIT repository, or to create a new empty repository.

What does git init do?

  • Creates a hidden folder called .git inside the current directory. This folder is the heart of the repository, where all change history, branches, configuration and other project metadata will be stored.
  • Initialize the repository with a main branch called master (or main in newer versions of GIT).
  • Prepare the repository so you can start adding files, making commits, and performing other version control operations.

When to use git init?

  • New Project: When you are starting a new project and want to start using GIT from the beginning for version control.
  • Existing Project: When you have an existing project that is not under version control and you want to start using GIT to manage it.

How to use git init:

  • Open a terminal or command line: Navigate to the root directory of your project.
  • Execute the command: Type git init and press Enter.
  • Check the creation of the repository: You should see a message similar to “Initialized empty Git repository in /path/to/your/project/.git/”. You can also check that the .git folder has been created inside your project.

Example:

Considerations:

  • Empty Repository: Initially, the repository will be empty, as you have not yet added any files.
  • Existing Files: If the directory already contains files, GIT will not automatically add them to the repository. You will have to use git add to add them to the Staging Area and then commit them to include them in the version history.
  • gitignore: It is recommended that you create a .gitignore file to specify which files or folders you want to exclude from version control (e.g. temporary files, environment-specific configuration files, etc.).

git clone – Clone an Existing Repository

The git clone command allows you to create a local copy of a GIT repository that is located elsewhere, either on a remote server or in another location on your machine. This is useful when you want to start working on an existing project or collaborate with other developers.

What does git clone do?

  • Downloads the entire commit history, branches, files and configuration from the original repository.
  • Creates a new folder on your local machine with the same name as the original repository (you can specify a different name if you wish).
  • Set up a remote connection called origin that points to the original repository, allowing you to interact with it easily (do push, pull, etc.).
  • It automatically places you in the master (or main) branch of the cloned repository, ready to start working.

When to use git clone?

  • Collaboration: When you want to join an existing project and start working on it.
  • Backup: When you want to create a local copy of a remote repository as a security measure.
  • Development in Different Environments: When you want to work on a project on different machines or environments.

How to use git clone:

  • Open a terminal or command line: Navigate to the directory where you want to create the local copy of the repository.
  • Run the command: Type git clone <URL-of-repository> and press Enter. Replace <URL-of-repository> with the address of the repository you want to clone (for example, the URL of a repository on GitHub, GitLab or Bitbucket).
  • Wait for the cloning to complete: GIT will download all files and history from the repository. The time it takes will depend on the size of the repository and the speed of your internet connection.

Example:

Additional Options:

  • git clone -b <branch-name> <URL-of-repository>: Clones the repository and automatically switches to the specified branch.
  • git clone --depth 1 <URL-of-repository>: Clones only the latest commit of the repository, which can be useful if you only need the latest version and want to save disk space.

git add – Add changes to the Staging Area

The git add command is central to the GIT workflow, as it allows you to select the changes you want to include in the next commit. It acts as a bridge between the Working Tree (where you make changes) and the Staging Area (where you prepare the changes for commit).

What does git add do?

  • It takes the changes made to the files specified in the Working Tree and “copies” them to the Staging Area.
  • It prepares those changes to be included in the next commit.
  • It does not modify the commit history or affect other branches.

When to use git add?

  • After modifying files_**: Every time you make changes to your files and want to include them in a commit, you should use git add to add them to the Staging Area.
  • Granular control of commits_**: If you have made several changes but only want to include some of them in the next commit, you can use git add to select specifically which changes to add.
  • Before making a commit_**: You should always use git add before making a commit to make sure the changes you want to include are in the Staging Area.

How to use git add:

  • git add <file>: Add a specific file to the Staging Area.
  • git add .: Adds all modified and new files in the current directory and its subdirectories to the Staging Area.
  • git add -p: Allows you to review the changes to each file interactively and select which parts to add to the Staging Area.

Example:

Considerations:

  • New files: New files must also be added to the Staging Area with git add before they can be included in a commit.
  • Deleted files: If you have deleted files, you must also use git add to record that deletion in the Staging Area.
  • Partial changes: With git add -p you can select which parts of a modified file you want to add to the Staging Area, which gives you more precise control over your commits.

git commit – Create a commit

The git commit command is the one that finally records the changes you have prepared in the Staging Area, creating a new commit in your repository history. Each commit is a snapshot of your project at a given point in time, allowing you to revert to previous versions if necessary and keep detailed track of the evolution of your code.

What does git commit do?

  • It takes all the changes that are in the Staging Area and packages them into a new commit.
  • It assigns a unique identifier (SHA-1) to the commit to ensure its integrity and traceability.
  • Records the author, date and a descriptive message of the commit.
  • Updates the HEAD pointer to point to the newly created commit.

When to use git commit?

  • After adding changes to the Staging Area: Once you have used git add to prepare the changes you want to include in the commit, you must use git commit to create the commit.
  • To save a checkpoint: When you have completed a task or feature and want to save a snapshot of your project in that state.
  • To document your work: The commit message allows you to describe the changes you made, making it easier to understand the project history and collaborate with other developers.

How to use git commit:

  • git commit -m “Descriptive message”: Creates a new commit with the changes that are in the Staging Area and the specified message.
  • git commit: Opens a text editor where you can write a more detailed message for the commit.

Example:

Good Practices for Commit Messages:

  • Brevity and clarity: The message should be concise but descriptive, summarizing the changes made in a few words.
  • Present tense verb: Use the present indicative to describe the action taken (e.g., “Add ‘, ’Correct ‘, ’Improve ”).
  • Emphasis on change: The message should focus on what has been changed, not how it was done.
  • References to tasks or issues_**: If you are working with a task or issue tracking system, you can include references in the commit message for easy traceability.

git status – View the status of files

The git status command is an essential tool to keep you informed about the current status of your repository. It gives you an overview of the changes you have made to your files, telling you which ones are modified, which ones are ready to be included in a commit, and which ones have not yet been tracked by GIT.

What does git status do?

  • Compares the contents of your Working Tree (the files in your working directory) with the contents of the Staging Area (the changes ready for the next commit) and the last commit in your current branch.
  • It shows you a list of the files that have been modified, added or deleted since the last commit.
  • Indicates which files are in the Staging Area, ready to be included in the next commit.
  • Informs you about new files that have not yet been added to the Staging Area (untracked files).
  • It provides hints about the next commands you might run, such as git add or git commit.

When to use git status?

  • Before making a commit: To make sure that you are including all the desired changes in the commit and that you are not including unwanted changes.
  • After making changes: To see an overview of the changes you have made and decide what to do with them (add them to the Staging Area, discard them, etc.).
  • To keep you informed: To have a clear idea of the current status of your repository and the changes that are pending to be checked in.

How to use git status:

  • Open a terminal or command line: Navigate to the root directory of your repository.
  • Run the command: Type git status and press Enter.

Example output:

Interpretation of the output:

  • On branch main: Indicates the current branch you are on.
  • Your branch is up to date with 'origin/main': Indicates that your local branch is synchronized with the main branch of the remote origin repository.
  • Changes to be committed: Shows the files that are in the Staging Area, ready to be included in the next commit.
  • Changes not staged for commit: Shows files that have been modified but not yet added to the Staging Area.
  • Untracked files: Shows new files that GIT is not tracking yet.

git log – View commit history

The git log command is your window into your project’s past. It allows you to explore the commit history, showing you a detailed list of all the changes that have been made over time, who made them and when. It’s an invaluable tool for understanding the evolution of your code, tracking bugs, collaborating with other developers and much more.

What does git log do?

  • Displays a list of commits in reverse chronological order (from most recent to oldest) on the current branch.
  • For each commit, it shows:
  • The unique identifier (SHA-1).
  • The author and his email address.
  • The date and time of the commit.
  • The commit message.
  • Optionally, it can show more details, such as the changes made to each file.

When to use git log?

  • Review the history: To see what changes have been made to the project, who made them and when.
  • Track bugs: To identify when a bug was introduced and who made it, making it easier to fix it.
  • Understanding the evolution of the code: To see how the code has evolved over time and understand the design decisions made.
  • Collaboration: To see other developers’ commits and understand their contributions to the project.

How to use git log:

  • Open a terminal or command line: Navigate to the root directory of your repository.
  • Execute the command: Type git log and press Enter.

Example output:

Useful git log options:

  • git log --oneline: Displays a compact version of each commit, with only the identifier and message on a single line.
  • git log -p: Shows the detailed changes (diffs) made to each file in each commit.
  • git log --author=“<author-name>”: Displays only commits made by a specific author.
  • git log --since=“<date>” or git log --until=“<date>”: Displays commits made within a specific date range.
  • git log --grep=“<pattern>”: Displays commits whose messages contain a specific search pattern.

git push – Send changes to remote repository

The git push command is your communication path to the outside world. It allows you to push commits you have created in your local repository to a remote repository, such as GitHub, GitLab or Bitbucket. This is essential for collaborating with other developers, sharing your work and keeping a backup of your code on an external server.

What does git push do?

  • Uploads commits from your current local branch to the specified remote repository.
  • Updates the corresponding remote branch to reflect the changes you have made locally.
  • Allows other developers to view and download your changes.

When to use git push?

  • Collaboration: When you want to share your changes with other developers who are working on the same project.
  • Backup: When you want to make sure your changes are backed up on a remote server in case of problems with your local machine.
  • Deployment: When you want to deploy your code to a production or test server.

How to use git push:

  • Make sure you have a remote connection set up: If you haven’t already done so, use git remote add origin to add a remote connection named origin pointing to the remote repository where you want to push your changes.
  • Make commits to your local branch: Make the necessary changes to your files, add them to the Staging Area with git add and create commits with git commit.
  • Run the command: Type git push origin <branch-name> and press Enter. Replace <branch-name> with the name of the local branch you want to push to the remote repository. If this is the first time you are pushing to that branch, you can use git push -u origin <branch-name> to set it as the default tracking branch for future pushes.

Example:

Considerations:

  • Conflicts: If other developers have pushed to the same remote branch while you were working locally, conflicts may arise. In that case, GIT will ask you to resolve the conflicts before you can complete the push.
  • Permissions: Make sure you have the necessary permissions to push to the remote repository. If you are working on a collaborative project, you may need to request write access from the repository owner.
  • Protected branches: Some remote repositories may have protected branches, such as master or main, that require code review or approval before you can push directly to them.

git pull – Pull changes from a remote repository

The git pull command is your way of keeping your local repository in sync with a remote repository. It allows you to download changes that other developers have made to the remote repository and integrate them into your current local branch. It is an essential operation for collaborating on projects and making sure you are working with the latest version of the code.

What does git pull do?

  • It performs two operations in sequence:
    1. git fetch: Fetches changes from the remote repository without integrating them into your local branch yet.
    2. git merge: Merge the downloaded changes with your current local branch.

When to use git pull?

  • Collaboration: Before you start working on your local branch, to make sure you have the latest changes made by other developers.
  • Updating: To keep your local repository up to date with changes from the remote repository.
  • Change Integration: To incorporate changes from a remote branch into your local branch.

How to use git pull:

  • Make sure you have a remote connection set up: If you haven’t already, use git remote add origin <URL-of-repository> to add a remote connection named origin pointing to the remote repository you want to pull changes from.
  • Run the command: Type git pull origin <branch-name> and press Enter. Replace <branch-name> with the name of the remote branch you want to pull the changes from. If you are on the main branch and want to download the changes from the main branch of the remote repository, you can simply type git pull.

Example:

Considerations:

  • Conflicts: If your local changes conflict with changes downloaded from the remote repository, GIT will ask you to resolve the conflicts before you can complete the pull.
  • Tracking branches: If you have set up a tracking branch (with git push -u origin <branch-name>), you can simply type git pull to download and merge the changes from the corresponding remote branch.
  • Merge: By default, git pull uses git merge to merge the changes. If you prefer to use git rebase, you can use git pull --rebase.

Find the full guide here.

Read the first chapter of this guide here.

Share this post: LinkedIn LinkedIn Logo Facebook Facebook Logo

About Aviada

With Aviada’s expertise in staff augmentation, hiring professionals in Mexico has never been easier.
By providing us with a job description, you can harness the benefits of Employer Of Record Services and trust Aviada to locate the ideal candidate to meet your needs, seamlessly taking care of the entire process.

Aviada ranked #1 in Best Place to Code 2021

Best Place to Code® is a program to identify and award companies that make efforts to offer the best work conditions to IT Professionals. This award is operated by Software Guru.
BPTC Logo