Flutter Learning Roadmap

14 min

14 min

Ashutosh Rawat

Published on Dec 8, 2023

Best way to learn Git & GitHub Desktop in 5 Easy Steps: A Practical Guide for Programmers

Git Basics | GitHub | Repository | Pull Request
Git Basics | GitHub | Repository | Pull Request

Overview

Overview


Git Basic

Git is a free and open-source version control system used by developers worldwide. It allows you to track changes in your code over time, collaborate with other developers, and revert to previous versions if needed. Unlike older systems, Git is distributed, meaning each developer has a complete copy of the codebase locally. This makes it faster and more flexible, especially for teams working on large projects.

Originally, created by Linus Torvalds in 2005. Unlike older centralized version control systems such as SVN and CVS, Git is distributed: every developer has the full history of their code repository locally. This makes the initial clone of the repository slower, but subsequent operations such as commit, blame, diff, merge, and log dramatically faster.


How to use Git as a beginner?

To start using Git as a beginner, you need to follow these steps:

  1. Install Git: Download and install Git on your local machine. Git is available for Windows, macOS, and Linux.

  2. Configure Git: Set up your name and email address in the Git configuration. This information will be associated with your commits.

  3. Create a Repository: Initialize a new Git repository using the “git init” command. This creates a new directory where you can start tracking changes.

  4. Add Files: Add the files you want to track using the “git add” command. This stages the files for the next commit.

  5. Commit Changes: Commit your changes using the “git commit” command. This creates a snapshot of the current state of your code.

  6. Branching and Merging: Create branches to work on different features or bug fixes using the “git branch” command. Merge branches back into the main branch using the “git merge” command.

  7. Collaborate: Share your code with others by pushing your local changes to a remote repository using the “git push” command. Pull changes made by others using the “git pull” command.

What are the three states of Git workflow?

The three states of Git workflow are:

  1. Modified: When you make changes to a file, it becomes “modified” in the working directory.

  2. Staged: After staging the modified file using “git add”, it becomes “staged” in the staging area.

  3. Committed: When you commit the changes using “git commit”, the staged changes become “committed” in the repository.


Key Elements of Git

Git is a powerful version control system that helps developers manage their codebase effectively. The key elements of Git are:

  1. Repository: This is the central storage location for all the files and their history. It acts like a time machine, allowing you to revert to any previous version of your codebase.

  2. Commit: This is a snapshot of the codebase at a specific point in time, along with a descriptive message. Each commit is uniquely identified by a hash code, ensuring the integrity and traceability of your code.

  3. Branch: This is a parallel development line that allows you to work on new features or bug fixes without affecting the main codebase. You can create and switch between branches easily, allowing you to experiment and iterate without fear of breaking anything.

  4. Merge: It is the process of combining changes from one branch into another. Git provides several merging strategies to ensure a smooth and conflict-free integration of changes.

  5. Remote: This is a copy of the repository hosted on a remote server, allowing you to collaborate with other developers. You can push your local commits to the remote repository and pull updates made by others, keeping everyone in sync.

What are the four areas of Git?

The four areas of Git are:

  1. Working Directory: The directory where you make changes to your code.

  2. Staging Area: A temporary area where you stage changes before committing them.

  3. Local Repository: The local copy of the Git repository on your machine.

  4. Remote Repository: A repository hosted on a remote server, allowing collaboration with others.

Basics of Git Workflow:

  1. Clone Repository: Download a copy of the project’s codebase from a hosting service like Bitbucket.

  2. Make Changes: Edit the code locally and commit your changes to save them.

  3. Push Changes: Upload your local changes to the remote repository, making them available to others.

  4. Pull Changes: Download the latest changes from other developers to your local machine.

  5. Branching and Merging: Create separate branches for new features or bug fixes, then merge them back into the main codebase.

  6. Pull Requests: Propose changes to the main codebase for review and approval.

Six Functions of Git

Git provides six key functions that enable efficient and collaborative development:

  1. Version Control: Git tracks every change made to your files, allowing you to revert to any previous version, see the history of changes, and compare different versions.

  2. Collaboration: Git allows multiple developers to work on the same codebase simultaneously by providing branching and merging features. This ensures everyone can contribute without interfering with each other's work.

  3. Branching: Creating separate branches allows for parallel development of new features or bug fixes without affecting the main codebase. This enables experimentation and risk-free development.

  4. Merging: Combining changes from different branches allows you to integrate new features and bug fixes into the main codebase seamlessly. Git provides various merging strategies to handle potential conflicts effectively.

  5. Staging Changes: This allows you to selectively choose which changes to include in the next commit. This provides a safety net and controls over what gets recorded as part of your project history.

  6. Remote Repositories: Hosting your repository on a remote server allows you to collaborate with others easily. You can push your local changes to the remote repository and pull updates made by others, keeping everyone synchronized.

How to install Git

Install Git on Mac OS X

There are several ways to install Git on a Mac. If you’ve installed XCode (or its Command Line Tools), Git may already be installed. To find out, open a terminal and enter git --version.

$ git --version
  git version 2.7.0 (Apple Git-66)

Apple maintains and ships its fork of Git, but it tends to lag behind mainstream Git by several major versions. You may want to install a newer version of Git using one of the methods below:

Git for Mac Installer
The easiest way to install Git on a Mac is via the stand-alone installer:

  1. Download the latest Git for Mac installer.

  2. Follow the prompts to install Git.

  3. Open a terminal and verify the installation was successful by typing git --version:

$ git --version
  git version 2.9.2

4. Configure your Git username and email using the following commands, replacing Emma’s name with your own. These details will be associated with any commits that you create:

$ git config --global user.name "Hello Blup"
$ git config --global user.email "hello@blup.in"

5. (Optional) To make Git remember your username and password when working with HTTPS repositories, configure the git-credential-osxkeychain helper.


Install Git on Windows

Git for Windows stand-alone installer

  1. Download the latest Git for Windows installer.

  2. When you’ve successfully started the installer, you should see the Git Setup wizard screen. Follow the Next and Finish prompts to complete the installation. The default options are pretty sensible for most users.

  3. Open a Command Prompt (or Git Bash if during installation you elected not to use Git from the Windows Command Prompt).

  4. Run the following commands to configure your Git username and email using the following commands, replacing Blup's name with your own. These details will be associated with any commits that you create:

$ git config --global user.name "Hello Blup"
$ git config --global user.email "hello@blup.in"

5. Optional: Install the Git credential helper on Windows
Bitbucket supports pushing and pulling over HTTP to your remote Git repositories on Bitbucket. Every time you interact with the remote repository, you must supply a username/password combination. You can store these credentials, instead of supplying the combination every time, with the Git Credential Manager for Windows.


Learn Git & GitHub Desktop in 5 Easy Steps: A Practical Guide for Programmers


Step 1: Create a Repository Locally and on GitHub

Adding a repository from your local computer to GitHub Desktop
1. In the menu bar, select File, then click Add Local Repository.

  1. In the "Add Local Repository" window, click Choose..., then use the Finder window to navigate to the local repository you want to add.

  1. When you have chosen the local repository, in the "Add Local Repository" window, click Add Repository.

Tip: You can add a Git repository from your local computer to GitHub Desktop by dragging the folder onto the GitHub Desktop window. If you drag multiple Git folders into GitHub Desktop at the same time, each folder will be added as a separate Git repository.

Adding an existing project to GitHub using GitHub Desktop

You can add an existing Git repository to GitHub using GitHub Desktop.

  1. Using the command line, remove any git remotes currently configured for the repository.

# Show existing remotes 
$ git remote -v 
> origin git@git-server/octocat/hello-world.git (fetch) 
> origin git@git-server/octocat/hello-world.git (push) 
# Remove existing remotes 
$ git remote remove origin

Add the repository to GitHub Desktop.

  1. In the repository bar, click Publish Repository.

  2. In the "Publish Repository" window, in the "Name" field, type the desired name of the repository or use the default current local repository name.

  3. Optionally, add a description for the repository.

  4. Optionally, to publish a public repository, deselect Keep this code private.

  5. Select the "Organization" dropdown menu, then either click the organization where you want to publish the repository, or, to publish the repository to your personal account, click None.

  6. Click Publish Repository.

Step 2: Pushing Local Changes to the Repository

Pushing Changes to GitHub from GitHub Desktop

As you work on your project locally, Git tracks your changes and allows you to commit them to your local repository. These committed changes are not yet accessible to others unless you push them to a remote repository, such as GitHub.

Pushing changes to GitHub essentially sends the committed changes in your local repository to the remote repository on GitHub. This allows other collaborators to access your changes, pull them to their local machines, and work on them further.

Pushing Local Changes to the Repository

  1. To push your local changes to the remote repository, in the repository bar, click Push origin.

  2. If there are commits on the remote branch that you don't have on your local branch, GitHub Desktop prompts you to fetch new commits from the remote. In the "New Commits on Remote" window, click Fetch.

  3. Optionally, click Preview Pull Request to open a preview dialog where you can review your changes and begin to create a pull request. For more information, see "Creating an issue or pull request from GitHub Desktop."


Step 3: Pulling and Cloning Projects from the Repository

  • Pulling: In GitHub Desktop, click "Pull" to download the latest changes from the remote repository to your local machine.

  • Cloning: Go to GitHub.com and find the repository you want to clone. Click "Clone" and choose a local directory to clone it into.

Pulling to your local branch from the remote

You can sync your local branch with the remote repository by pulling any commits that have been added to the branch on GitHub since the last time you synced. If you make commits from another device or if multiple people contribute to a project, you will need to sync your local branch to keep the branch updated.

In GitHub Desktop, use the Current Branch drop-down, and select the local branch you want to update.

  1. To check for commits on the remote branch, click Fetch origin


  2. To pull any commits from the remote branch, click Pull origin or Pull origin with rebase.



  3. Resolve any merge conflicts in your preferred way, using a text editor, the command line, or another tool.

Cloning a repository from GitHub to GitHub Desktop

You can use GitHub to clone remote repositories to GitHub Desktop.

Tip: You also can use GitHub Desktop to clone repositories that exist on GitHub. For more information, see "Cloning and forking repositories from GitHub Desktop."

  1. Sign in to GitHub.com and GitHub Desktop before you start to clone.

  2. On GitHub.com, navigate to the main page of the repository.

  3. Above the list of files, click <> Code.

  4. To clone and open the repository with GitHub Desktop, click Open with GitHub Desktop.

  5. Click Choose... and navigate to a local directory where you want to clone the repository.

    Note: If the repository is configured to use LFS, you will be prompted to initialize Git LFS.

  6. Click Clone.


Step 4: Branching: Managing branches in GitHub Desktop

You can use branches to safely experiment with changes to your project. Branches isolate your development work from other branches in the repository. For example, you could use a branch to develop a new feature or fix a bug.

You always create a branch from an existing branch. Typically, you might create a branch from the default branch of your repository. You can then work on this new branch in isolation from changes that other people are making to the repository.

Tip: The first new branch you create will be based on the default branch. If you have more than one branch, you can choose to base the new branch on the currently checked-out branch or the default branch.

  • At the top of the app, click Current Branch and then in the list of branches, click the branch that you want to base your new branch on.

  • Click New Branch.

  • In the "Create a Branch" window, under "Name", type the name of the new branch.

  • Under "Create branch based on...", select a base branch for your new branch.

  • Click Create Branch.

Creating a branch from the previous commit

  • In the left sidebar, click History.


  • Right-click on the commit you would like to create a new branch from and select Create Branch from Commit.


  • In the "Create a Branch" window, under "Name", type the name of the new branch.

  • Click Create Branch.


Step 5: Merging and Pull Requests

  • Merging combines changes from one branch into another.

  • In GitHub Desktop, click "Pull Request" to create a pull request for your changes.

  • This notifies other developers about your changes and allows them to review and approve them.

  • Once approved, you can merge your changes into the main branch.

  1. Merging another branch into your project branch

To add changes from one branch to another branch, you can merge the branches. To apply changes to your branch from another branch in the same repository, you can merge the other branch into your branch on GitHub Desktop. To request that changes from your branch are merged into another branch, in the same repository or in another repository in the network, you can create a pull request on GitHub Desktop.

  1. In GitHub Desktop, click Current Branch.


  2. Click Choose a branch to merge into BRANCH.


  3. Click the branch you want to merge into the current branch, then click Merge BRANCH into BRANCH.


    Note:
    If there are merge conflicts, GitHub Desktop will warn you above the Merge BRANCH into BRANCH button. You will not be able to merge the branches until you have resolved all conflicts.

  4. To push your local changes to the remote repository, in the repository bar, click Push origin.


Viewing a pull request in GitHub Desktop

Pull requests let you propose changes to projects, provide feedback and reviews, and merge changes into projects.


When someone creates a pull request, they make changes on a "head branch" and suggest these changes to a "base branch," such as main. In GitHub Desktop, you can open (or "check out") the head branch of a pull request to view the changes a contributor is suggesting. For example, you can see a history of the commits that the contributor has made, and see which files the commits modified, added, or deleted.

Opening a pull request Branch in GitHub Desktop

  • In GitHub Desktop, click Current Branch.

  • At the top of the drop-down menu, click Pull Requests.


  • In the list of pull requests, click the pull request you want to view.

    Optionally, to refresh the list of pull requests, click .


Apart from this, we have written other in-detail articles on Flutter and mobile app development on Blup's official: https://www.blup.in/blogs

Conclusion:

Git and Github empower developers with a robust toolset for managing code versions, fostering collaboration, and streamlining the development process. By embracing Git’s functionalities and leveraging Github’s platform, you can enhance your workflow, improve project management, and ultimately contribute to successful software development.

Remember, continuous learning and exploration are key to mastering the intricacies of Git and Github. Utilize online resources, tutorials, and communities to deepen your understanding and unlock the full potential of these valuable tools. Happy coding!

Step 1: Create a Repository Locally and on GitHub

Adding a repository from your local computer to GitHub Desktop
1. In the menu bar, select File, then click Add Local Repository.

  1. In the "Add Local Repository" window, click Choose..., then use the Finder window to navigate to the local repository you want to add.

  1. When you have chosen the local repository, in the "Add Local Repository" window, click Add Repository.

Tip: You can add a Git repository from your local computer to GitHub Desktop by dragging the folder onto the GitHub Desktop window. If you drag multiple Git folders into GitHub Desktop at the same time, each folder will be added as a separate Git repository.

Adding an existing project to GitHub using GitHub Desktop

You can add an existing Git repository to GitHub using GitHub Desktop.

  1. Using the command line, remove any git remotes currently configured for the repository.

# Show existing remotes 
$ git remote -v 
> origin git@git-server/octocat/hello-world.git (fetch) 
> origin git@git-server/octocat/hello-world.git (push) 
# Remove existing remotes 
$ git remote remove origin

Add the repository to GitHub Desktop.

  1. In the repository bar, click Publish Repository.

  2. In the "Publish Repository" window, in the "Name" field, type the desired name of the repository or use the default current local repository name.

  3. Optionally, add a description for the repository.

  4. Optionally, to publish a public repository, deselect Keep this code private.

  5. Select the "Organization" dropdown menu, then either click the organization where you want to publish the repository, or, to publish the repository to your personal account, click None.

  6. Click Publish Repository.

Top Blogs

Follow us on

Follow us on