logo
Back to Main Website
Go back
Tomas Vivaldi

Mastering Git: Git Pull and Git Good!

April 4, 2023

Author: Tomas Vivaldi

Lets go over the basic GitHub commands to set you up to get started with Git

Git

Beginner

Getting Started with Git

As developers, we rely on version control systems to keep track of changes in our projects and collaborate effectively with our team. One of the most popular and powerful version control systems is Git. In this post, I'll share some essential Git commands and workflow tips to help you work more efficiently and since I myself keep forgetting some of the commands needed to manage you repository properly, I decided to make this post.

Before diving into the commands, let's take a moment to understand the basic structure of a Git repository. A Git repository consists of a series of commits, each representing a snapshot of your project at a specific point in time.

"Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency." - git-scm.com

Installing Git

To get started, you'll need to have Git installed on your system. Visit the official Git website for installation instructions for your specific operating system.

Essential Git Commands

Here are some fundamental Git commands that every developer should know.

1. git init

git init initializes a new Git repository in the current directory.

$ git init

2. git clone

git clone creates a copy of an existing Git repository on your local machine.

$ git clone https://github.com/username/repository.git

3. git add

git add stages changes to be committed. You can stage all changes in your working directory or specify individual files.

$ git add . # Stage all changes $ git add filename.txt # Stage specific file

4. git commit

git commit creates a new commit with your staged changes and an associated message describing the changes made.

$ git commit -m "Your commit message here"

5. git status

git status displays the current state of your working directory, including any staged, modified, or untracked files.

$ git status

6. git log

git log shows a log of all commits in the repository, with the most recent commit at the top.

$ git log

7. git diff

git diff shows the differences between your working directory and the latest commit.

$ git diff

8. git pull

git pull fetches changes from a remote repository and merges them into your local branch.

$ git pull

9. git push

git push pushes your local changes to a remote repository.

$ git push

10. git branch

git branch lists all branches in your repository and shows the currently active branch.

$ git branch

11. git checkout

git checkout switches between branches or specific commits.

$ git checkout branch_name # Switch to a branch

$ git checkout commit_hash # Switch to a specific commit

Workflow Tips

  1. Commit early and often: Frequent commits make it easier to track changes and roll back to previous versions if needed.
  2. Write meaningful commit messages: Descriptive commit messages help you and your team understand the purpose of each commit.
  3. Use branches: Branches allow you to work on new features or bug fixes without affecting the main codebase until you're ready to merge.
  4. Embrace collaboration: Take advantage of Git's powerful collaboration features, such as pull requests and code reviews, to ensure high-quality code.

With these essential Git commands and workflow tips, you'll be well-equipped to manage your projects. But that alone may not be enough, I believe that debugging is the bane of every programmer's existence. So to help out in that regard, I also compiled here some commands that might help in case of facing problems with the branches of a project.

Fixing Out-of-Sync Branches and Advanced Git Commands

Sometimes, you may encounter situations where your branches are out of sync or need to be restructured. In this section, we'll discuss how to fix these issues and explore some advanced Git commands that can help you manage your repositories more effectively.

Fixing Out-of-Sync Branches

When branches are out of sync, it usually means that new changes have been made in one branch but not the other. To sync branches, you can either merge or rebase, depending on the desired workflow.

Merging Branches

git merge is used to combine changes from one branch into another. When you merge a branch, Git creates a new commit that includes the changes from both branches.

$ git checkout master # Switch to the target branch (e.g., master)

$ git pull # Update the target branch with remote changes

$ git merge feature-branch # Merge the feature branch into the target branch

$ git push # Push the merged changes to the remote repository

Rebasing Branches

git rebase is an alternative to merging that helps maintain a linear commit history. When you rebase a branch, Git replays the commits from one branch onto another.

$ git checkout feature-branch # Switch to the feature branch

$ git pull --rebase origin master # Rebase the feature branch onto the latest version of the master branch

$ git push --force-with-lease # Push the rebased changes to the remote repository (use with caution)

Note that force-pushing can overwrite remote changes and should be used with caution. Always communicate with your team before force-pushing to avoid potential conflicts.

Advanced Git Commands

Here are some advanced Git commands that can help you navigate and manage your repositories more effectively.

1. git remote

git remote lists all remote repositories connected to your current repository.

$ git remote

To add a new remote repository, use the git remote add command:

$ git remote add remote_name https://github.com/username/repository.git

To remove a remote repository, use the git remote remove command:

$ git remote remove remote_name

2. git fetch

git fetch downloads changes from a remote repository but does not merge them. This is useful for reviewing changes before merging them into your local branch.

$ git fetch remote_name

3. git stash

git stash temporarily saves changes in your working directory that you do not want to commit yet. This allows you to switch branches without committing unfinished work.

$ git stash # Save changes to a new stash

$ git stash list # List all stashes

$ git stash apply # Apply the latest stash

$ git stash drop # Remove the latest stash

4. git cherry-pick

git cherry-pick applies specific commits from one branch to another. This is useful for incorporating specific changes without merging the entire branch.

$ git checkout target_branch # Switch to the target branch

$ git cherry-pick commit_hash # Apply a specific commit from another branch

5. git bisect

git bisect is a binary search tool that helps you find the commit that introduced a bug by iteratively testing a range of commits.

$ git bisect start # Start the bisect process

$ git bisect good # Mark the current commit as good (bug-free)

$ git bisect bad # Mark the current commit as bad (buggy)

$ git bisect reset # Reset the bisect process and return to the original branch

6. git reflog

`git reflog` shows a log of all references (branches, tags, etc.) that have been updated in your local repository. This is useful for finding lost commits or branches.

$ git reflog

7. git clean

git clean removes untracked files and directories from your working directory. This is useful for cleaning up your repository after building or running tests.

$ git clean -n # Show a list of untracked files and directories that would be removed

$ git clean -f # Remove untracked files

$ git clean -fd # Remove untracked files and directories

Navigating Branches, Merging, and Rebasing

In addition to the commands covered above, there are some best practices you can follow when navigating branches, merging, and rebasing.

  1. Always create a new branch for each feature or bugfix: This keeps your main branch clean and makes it easier to review and merge changes.
  2. Keep your branch names descriptive: Use clear and concise branch names that describe the feature or bugfix being worked on.
  3. Regularly fetch and merge or rebase with the main branch: This helps you stay up-to-date with the latest changes and reduces the likelihood of conflicts when it's time to merge your branch.
  4. Resolve conflicts promptly: If a merge or rebase results in conflicts, resolve them as soon as possible to maintain a clean commit history.
  5. Delete merged branches: Once a branch has been merged, it's a good practice to delete it to keep your repository clean and organized.

By following these best practices and using the advanced Git commands covered in this post, you'll be better equipped to manage your projects, collaborate with your team, and navigate complex repository structures.