GitHub is a great platform that hosts code for a variety of projects. Even, the Linux foundation has a mirror of their repository on this platform. GitHub provides an impressive “Help” section where you can browse through tutorials and articles to get started. However, if you are not comfortable with the command line, it may seem daunting. There are various GUIs around working with Git. This means that the command line is not as important for basic workflow. However, you still have to understand what you can do. This is where this guide comes into place. It aims to help you learn the basics of Git and GitHub without coding expertise. But, it is assumed that you know some form of coding such as CSS and HTML. You might even know some PHP.

What is Git

Git is a version control system that is widely used for software development and other version control tasks. It allows users to save changes to files and coordinate changes among multiple people. Git also allows users to revert files back to previous states, view change history, and collaborate on projects. It is an open-source tool that is freely available to anyone.

How can I use Git Command

how can I use git

To use git, you will first need to install it on your computer. Once git is installed, you can use it to track changes to files in a project, collaborate with others on a project, and manage different versions of a project. Here are some basic steps for using git:

  1. Create a new directory for your project, or navigate to an existing directory that you want to track with git.
  2. Initialize a new git repository in your project directory by running the command git init.
  3. Use the git add command to add files to the repository. This tells git which files to track and save changes for.
  4. Use the git commit command to save changes to the repository. This creates a new “commit” with a message describing the changes that were made.
  5. Use the git log command to view the commit history for the repository. This will show you all of the previous commits, along with their messages and other information.
  6. Use the git branch command to create and switch between different branches of your project. This allows you to work on multiple versions of your project simultaneously.
  7. Use the git merge command to combine changes from different branches. This allows you to integrate changes from different branches into a single branch.

These are just some of the basic commands for using git. There are many other features and advanced commands that you can use to work with git more effectively. To learn more, you can read the git documentation or take a tutorial on using git.

The GitHub Workflow

Here is what you need to know about the workflow when working on projects on GitHub.

  • Fork the project that you are interested in on GitHub.
  • Clone the Fork to the computer.
  • Make a topic branch for the work in a local clone.
  • Commit changes to the local repository.
  • Make the changes to the GitHub fork.
  • Send the pull request back for the original project in case your contribution is useful for the community.

The above are some of the basics that you must understand. Each of the terms will be explained to help you get started.

Centralized Vs Decentralized Version Control

As Git is a distributed version control system unlike subversion which is centralized, the code repository (repo) lives on the server and special permission (commit rights) are needed to add code. Everything exists in one place. A distributed system would provide you with your own repository when you clone a project. It means that you can start adding code to the repo when offline as the repo lives on the computer. However, it would require you to ensure everything is in sync through the development as the repo would be separate.

Fork and Clone

When you use GitHub, you need to fork which requires you to click the fork button to get your personal copy of the original project in your account. You will need to clone it to work on your computer which would result in two repos. One would be remote on GitHub, whereas, the local one would exist on your computer. It is known as forking on GitHub as each repo allows you to take the development in any direction. It is up to you decide whether to contribute to the original project or not. Since you do not want to do that, this post will help you pursue the other route.

Remotes

Other repos are known as remotes by Git. When you clone a repo, it would save the parent repo under “Origin” name. It would point to the personal fork on GitHub. It is up to you to add as many remotes as you want and give them any name you desire. In fact, you might want to add a remote for everyone who has forked the original project to keep a track of their work. It is recommended to add the original repo as the remote of your project to keep up with the development. The name convention for such a remote would be “Upstream”. It would mean that there would be two remotes configure in the local repository.

“upstream” would point to the original GitHub repository, whereas, “origin” would point to the fork one on GitHub.

Make a Topic Branch

The thing about Git repos is that they are organized with branches and tags. Each branch allows you to keep lines of development separate. The term “Master” is used to refer to the default branch in Git. Branches that are made from Master are known as topic branches. For example, when you work on a website and you already have an idea but do not want to break anything, branching would be convenient. You can easily create a branch for the idea and do the testing and work as intended. Only when everything works out, it is the time to merge the branch into “master” branch.

Tagging

Next, you need to know about tags. They are pointers for a certain commit. They make it easier to reference a certain commit rather than having to memorize hash numbers. Version numbering is one of the ways tags can be used. GitHub generates zip archive of the entire repository at your tag commit.

Commit the Work and Checkout Your Files

Once you have made some changes that need to be saved, you commit them to the repo. Commits can be described as logical chunks of changes that are saved in a sequence and form a history which you can review later. It is possible for changes to several files to go to the same commit. A timestamp is made for each commit along with an author name and their email. Hash is automatically generated which Git keeps track of. A great thing about Git is that it is smart and only requires you to enter the first six to eight digits to find the relevant Hash.

Altering History

Now, you need to keep in mind that commits can be manipulated, split apart, altered, or combined later. Even though the hash might be individual for each commit, if the commits are changed, the corresponding hash would also change which would alter the history that Git tracks. One of the reasons why you should use local topic branches is because they allow you to manipulate commits without you having to stress about messing up anyone else’s history.

Using Checkout

Checkout allows you to see how the files of the project looked at a given point. The hash-tags of a commit can be used as a reference. It is normally used for tags or branch names. You should be able to checkout one branch and proceed with the other. Head is a special type of tag that tracks which commit is checked out currently in the active directory. This means that you can easily checkout how files looked 12 commits from now and revert back to the last version. It can be described as redo or undo on steroids.

Edit a Commit

To edit a commit in git, you can use the git rebase command. This command allows you to change the commit history of a branch by modifying, adding, or removing commits. Here is an example of how to use git rebase to edit a commit:

  • First, use the git log command to view the commit history for the branch that you want to edit. This will show you a list of all of the previous commits on the branch, along with their SHA-1 hashes.
  • Find the commit that you want to edit, and make a note of its SHA-1 hash.
  • Use the git rebase -i command, followed by the SHA-1 hash of the commit that you want to edit. This will open an editor window showing the commits on the branch, starting from the commit that you specified.
  • In the editor window, change the command at the beginning of the line for the commit that you want to edit from pick to edit. This will tell git rebase that you want to edit this commit.
  • Save and close the editor window. git rebase will now apply all of the commits on the branch up to the commit that you specified, but it will stop before applying the commit that you want to edit.
  • At this point, you can use the git commit --amend command to modify the commit that you want to edit. This will open an editor window where you can change the commit message and make other changes to the commit.
  • Save and close the editor window. git rebase will now continue applying the remaining commits on the branch, incorporating your changes to the commit that you edited.

Keep in mind that using git rebase to edit commits can be a powerful but dangerous operation. It allows you to modify the commit history of a branch, which can be useful for cleaning up your commits and making them more readable. However, it can also cause problems if other people have based their work on the original commit history. In general, it is best to use git rebase with caution and only when you are confident in what you are doing.

UI for Git

There are many different ways to use git with a user interface (UI), depending on your specific needs and preferences. Here are some options for using git with a UI:

  • Git command-line interface: You can use git directly from the command line, which provides a text-based interface for running git commands. This is the most basic way to use git, and it is included with the git installation.
  • Git graphical user interface (GUI) tools: There are many different git GUI tools that you can use to manage your git repositories. These tools provide a graphical interface for running git commands, and often include additional features and tools for visualizing and working with git repositories. Some popular git GUI tools include GitKraken, GitUp, and Git Extensions.
  • Integrated development environment (IDE) support: Many IDEs, such as Visual Studio and Eclipse, include built-in support for git. This allows you to use git directly from within the IDE, without needing to use a separate GUI tool.
  • Hosting services: There are many hosting services, such as GitHub and GitLab, that provide web-based interfaces for working with git repositories. These services often include additional features and tools for collaboration, such as pull requests, code review, and issue tracking.

Overall, the best way to use git with a UI will depend on your specific needs and preferences. You can try out different options and see which one works best for you.

What is the Origin of Git

Git was created by Linus Torvalds in 2005 for use in the development of the Linux operating system kernel. Torvalds wrote the initial version of git in response to the limitations of existing version control systems, which he found to be inadequate for managing the complex and rapidly-changing codebase of the Linux kernel. Torvalds wanted a system that was fast, flexible, and capable of handling large projects with many contributors. This led him to create git, which has since become one of the most widely used version control systems in the world.

Other available Version Control Systems

There are many other version control systems besides git, each with their own unique features and characteristics. Some of the most popular version control systems include:

  • Subversion (SVN): A centralized version control system that was designed to be a successor to CVS.
  • Apache Concurrent Versions System (CVS): A centralized version control system that was once popular for open-source projects.
  • Mercurial: A distributed version control system that is similar to git in many ways.
  • Perforce: A commercial version control system that is popular in the gaming industry.
  • Visual SourceSafe (VSS): A proprietary version control system from Microsoft.

These are just a few examples of version control systems. There are many others, each with their own strengths and weaknesses. In general, git is considered to be one of the most powerful and flexible version control systems available, and is a popular choice for many software development projects.

More Resources

There are many resources available for learning more about git, including documentation, tutorials, and online courses. Here are some places where you can get more information about git:

  • Git documentation: The official git documentation is a comprehensive resource that covers all aspects of using git, including installation, basic commands, advanced features, and troubleshooting. You can find the git documentation at https://git-scm.com/doc.
  • Git tutorials: There are many tutorials available online that can help you learn git. These tutorials range from basic introductions to more advanced topics, and often include examples and hands-on exercises. You can find git tutorials on websites like GitHub, GitLab, and the Atlassian Git tutorials.
  • Online courses: There are many online courses available that can help you learn git. These courses often include video lectures, quizzes, and other resources to help you understand git and how to use it effectively. You can find git courses on websites like Udemy, Coursera, and LinkedIn Learning.

Conclusion

When you continue developing a project, you get to keep up with upstream and ensure that everything is in sync. Your new workflow must pull the latest changes, push your master branch, create a local topic branch, commit changes, push the topic branch, send a pull request, and get the request accepted.