What is GitHub
- GitHub is a web-based platform that provides version control and collaboration tools for software development. It allows developers to host and share their code, track changes made to the code over time, and collaborate with other developers on software projects.
- GitHub is built on top of the Git version control system, which is used to track changes made to the code.
- Developers can create and manage their own repositories, which are essentially collections of files and folders that make up a software project. They can also fork existing repositories, which allows them to make a copy of someone else's code and use it as a starting point for their own project.
- GitHub also provides a variety of other tools, such as issue tracking, pull requests, and code review, which help developers collaborate on code and maintain a high level of code quality. Additionally, it has an open-source community, where developers can contribute to open-source projects and reuse code.
Here is a step-by-step guide on how to use GitHub
- Create an account: Go to GitHub's website (https://github.com/) and sign up for a new account.
- Install Git: In order to use GitHub, you need to have Git installed on your computer. You can download it from the official website (https://git-scm.com/)
- Create a new repository: Click on the "New repository" button on your GitHub dashboard, to create a new repository for your project. Give it a name and a short description.
- Clone the repository: Copy the repository's URL and use Git to clone it to your local machine. You can do this by running the command "git clone [repository URL]" in your terminal.
- Make changes to the files: Now that you have the repository on your local machine, you can make changes to the files using your preferred text editor.
- Commit the changes: Use Git to commit the changes you made. This will create a new version of the files that you can push to the repository. To commit the changes, use the command "git commit -am "Commit message""
- Push the changes: Use Git to push the changes to the repository. This will upload the new version of the files to GitHub. Use the command "git push"
- Create a pull request: If you're collaborating with other people on the project, you can use pull requests to propose changes and have them reviewed before they are merged.
- Collaborate: you can collaborate with other people by forking their repository, making changes, and then creating a pull request for them to review and merge.
Here is a list of some common Git commands that are used with GitHub
- git init: Initializes a new Git repository.
- git clone [repository URL]: Creates a copy of a remote repository on your local machine.
- git status: Shows the status of the files in the repository, including which files have been modified and which are staged for commit.
- git add [file name]: Adds a file to the staging area, preparing it for the next commit.
- git commit -m "[commit message]": Creates a new commit with the changes that have been made. The commit message is a short description of the changes that have been made.
- git push: Sends the committed changes to the remote repository.
- git pull: Fetches the latest changes from the remote repository and merges them into the local repository.
- git branch: Shows the branches in the repository and indicates the current branch.
- git merge [branch name]: Merges the specified branch into the current branch.
- git log: Shows a log of all the commits in the repository.
- git diff: Shows the differences between the current state of the files and the last commit.
- git stash: Temporarily saves changes that have been made but are not ready to be committed.
- git fetch: Retrieve the latest versions of the remote branches but doesn't merge them.
Understand commands
git init
- git init is a command used to initialize a new Git repository. It creates an empty repository on the local machine, allowing you to start tracking changes to files and directories.
- When you run git init, a new subdirectory called .git is created in the current working directory. This directory contains all of the information that Git needs to keep track of the repository, including the repository's history, configuration files, and metadata.
- The git init command can be run on an existing directory that you want to turn into a Git repository, or it can be run on a new directory that you want to create as a repository. Once you've run the command, you can start adding, committing and tracking changes to files and directories.
- It's worth noting that git init only creates the repository on the local machine and doesn't create a remote repository on a service like GitHub. To share the repository with others, you'll need to use other Git commands like git remote add or git push to connect to a remote repository and push your commits.
git clone
- git clone is a command used to create a copy of a remote repository on your local machine. It allows you to download the entire repository, including its history and all of its files, and create a local copy that you can work on.
- When you run the git clone, Git creates a new directory with the same name as the repository and then copies all of the files and history into that directory. The local copy of the repository is linked to the remote repository, allowing you to easily synchronize changes between the two.
- The basic syntax for the git clone command is git clone [repository URL]. The repository URL is the URL of the remote repository that you want to clone. For example, if you want to clone a repository located at https://github.com/username/repo.git, you would run the command git clone https://github.com/username/repo.git.
- Once the repository is cloned, you can start making changes to the files, committing them and pushing them to the remote repository. You can also pull the changes made by other people and merge them with your local repository.
git status
- git status is a command used to check the current status of a Git repository. It shows the list of files that have been modified, added, or deleted, and which are ready to be committed.
- When you run git status, it will show the list of files that are currently in the working directory, including the ones that have been modified but not yet staged for commit, and the ones that have been added to the staging area but not yet committed. It also shows the current branch, and if there is any difference between the local and remote repository.
- The output of the git status command is divided into two sections: the "Changes not staged for commit" section, which shows the files that have been modified but not yet added to the staging area, and the "Changes to be committed" section, which shows the files that have been added to the staging area and are ready to be committed.
- The git status command is very useful when you are working on a repository and want to see which files have been modified and which are ready to be committed. It allows you to keep track of your work and make sure that you are only committing the changes that you intend to.
git add
- git add is a command used to add files to the staging area in a Git repository. The staging area is a temporary holding area where changes made to files are prepared for a commit.
- The basic syntax for the git add command is git add [file name]. This command stages the specified file for commit, meaning that the changes made to that file will be included in the next commit.
- You can also use git add . to stage all the changes in the current directory, or git add -A to stage all changes including file deletions.
- The git add command is an important step in the Git workflow, as it allows you to carefully review and select the changes that you want to include in the next commit. This way, you can make sure that you are only committing changes that are related to the current task, and not including any unnecessary or unrelated changes.
git commit -m
- git commit is a command used to create a new commit in a Git repository. A commit is a snapshot of the repository at a specific point in time, and it includes all the changes that have been made since the last commit.
- The -m option is used to add a commit message, which is a short description of the changes that are being committed. The commit message should be written in the present tense and should be a clear and concise description of the changes that were made.
- The basic syntax for the git commit command is git commit -m "[commit message]". When you run this command, Git takes the changes that have been staged with git add and creates a new commit with those changes. This new commit is added to the repository's history, and it serves as a reference point for future commits.
- The git commit command is an important step in the Git workflow, as it allows you to save the changes that have been made in the repository permanently. It also allows you to organize your changes into logical units, and make it easy to revert or review the changes.
git push
- git push is a command used to send commits from a local repository to a remote repository. It allows you to share your changes with others and collaborate on a project.
- The basic syntax for the git push command is git push [remote name] [branch name]. The remote name is the name of the remote repository you want to push to (usually origin) and the branch name is the name of the branch you want to push.
- When you run git push, Git will send all of the commits that have been made in the local repository to the remote repository. This includes all commits made since the last time you pushed, and it will update the remote repository with the latest version of the code.
- It's important to note that git push only sends the commits, not the entire repository, this way it's faster and more efficient. Before pushing, it's recommended to pull the changes made by other people and merge them with your local repository.
git pull
- git pull is a command used to fetch and merge changes from a remote repository to a local repository. It allows you to synchronize your local repository with the remote repository, and incorporate changes made by other people into your own work.
- The basic syntax for the git pull command is git pull [remote name] [branch name]. The remote name is the name of the remote repository you want to pull from (usually origin) and the branch name is the name of the branch you want to pull.
- When you run git pull, Git will fetch the latest changes from the remote repository and merge them with your local repository. This includes all commits made since the last time you pulled, and it will update the local repository with the latest version of the code.
- It's important to note that git pull fetches the changes and merge them, this can lead to conflicts if there are changes in both the local and remote repository that can't be automatically resolved. In that case, you'll need to resolve the conflicts manually before committing the changes.
- git pull is a convenient command as it allows you to quickly and easily synchronize your local repository with the remote repository, it's also a best practice to pull the changes before pushing your own changes to the remote repository, in order to avoid conflicts.
git branch
- git branch is a command used to list, create, or delete branches in a Git repository. A branch is a separate line of development within a repository, allowing multiple people to work on the same codebase simultaneously without interfering with each other's work.
- When you run git branch without any arguments, it will list all the branches in the repository, including the current branch, which is indicated by an asterisk (*) next to its name.
- The basic syntax for creating a new branch is git branch [branch name]. This will create a new branch with the specified name and it will be an exact copy of the current branch.
- To switch to another existing branch use git checkout [branch name] this command will change the current branch and all the files in the working directory to the state of the specified branch.
- You can also delete a branch using the command git branch -d [branch name], this command will delete the specified branch but it will only work if the branch has been fully merged and it's not the current branch.
- Branches are useful for isolating different lines of development, allowing multiple people to work on the same codebase simultaneously without interfering with each other's work. They are also useful for creating new features, testing, and bug fixing without affecting the main branch. It's important to keep in mind that once a branch is deleted it cannot be recovered.
git merge
- git merge is a command used to merge changes from one branch into another branch in a Git repository. It allows you to bring the changes from one branch into another, and incorporate the work of multiple people into a single branch.
- The basic syntax for the git merge command is git merge [branch name]. When you run this command, Git will take the changes from the specified branch and apply them to the current branch. If there are any conflicts between the two branches, Git will prompt you to resolve them manually before the merge can be completed.
- It's important to note that git merge only works when you are on the branch that you want to merge the changes into. So you need to use git checkout command to switch to the branch first.
- Merging is a key aspect of Git's branching model, it's used to integrate changes made in different branches, this way it's possible to keep working on a feature, bug fix or any other task in a separate branch, and then merge the changes back into the main branch when they are ready. It's also important to keep in mind that merging can lead to conflicts, so it's best to pull the changes and resolve any conflicts before merging.
git log
- git log is a command used to display the commit history of a Git repository. It shows a list of all the commits that have been made in the repository, along with details such as the author, date, and commit message.
- When you run git log, it will display a list of commits, with the most recent commit at the top. Each commit is displayed with the following details:
- The commit hash (a long string of letters and numbers that uniquely identifies the commit)
- The author's name and email address
- The date the commit was made
- The commit message
3. The git log command is a powerful tool that allows you to see the history of the repository and understand how it has evolved over time. It's useful for reviewing the changes that have been made, finding when a specific change was made, identifying who made a change, and more.
4. There are also several options that you can use with the git log command to customize its output and filter the commits that are displayed. For example, you can use the -p option to display the changes made in each commit, or use --author=[author name] option to filter the commits by author.
git diff
- git diff is a command used to show the differences between the current state of files in the working directory and the last commit. It shows the lines that have been added, modified, or deleted in the files, allowing you to review the changes before committing them.
- When you run git diff, it will display the differences in a "unified diff" format, which shows the changes made to each file, line by line. The lines that have been added will be prefixed with a "+" symbol, the lines that have been removed will be prefixed with a "-" symbol, and the lines that have been modified will be prefixed with both "+" and "-".
- You can also use git diff to compare two specific commits, branches or even with a remote repository. For example, git diff [commit hash] will show the differences between the current state and the specified commit, git diff [branch name] will show the differences between the current branch and the specified branch and git diff [remote name]/[branch name] will show the differences between the current branch and the specified remote branch.
- The git diff command is a powerful tool that allows you to review the changes before committing them. It's also useful for identifying what has been changed and understanding the context of the changes. It's recommended to use it before committing or pushing changes, to make sure that you are only committing the intended changes.
git stash
- git stash is a command used to temporarily save changes that you have made to your working directory, but that you are not ready to commit yet. It allows you to switch to a different branch or task without losing your changes, and then reapply them later.
- When you run git stash, it will save the changes that you have made in the working directory and return the repository to the state of the last commit. You can then switch to another branch, work on a different task, or do anything else you need to do without losing your changes.
- To reapply the saved changes, you can use the command git stash apply to apply the most recent stash or git stash apply [stash name] to apply a specific stash.
- You can also use git stash list to see the list of all the stashes that you have created, and git stash drop [stash name] to delete a specific stash.
- The git stash command is a useful tool when you need to switch to a different branch or task, but you don't want to commit the changes you've made. It allows you to save the changes temporarily, and reapply them later when you are ready to continue working on them. It's also useful when you are working on a feature or a bug fix, and you need to switch to another task, but you don't want to create a new branch.
git fetch
- git fetch is a command used to download commits, files, and references from a remote repository to a local repository. It allows you to retrieve the latest changes from a remote repository without merging them with your local repository.
- When you run git fetch, it will download all the new commits, branches, and tags from the remote repository and store them in your local repository. However, it will not change any of the files in your working directory or update your local branches. This means that after a fetch, you will still be in the same branch and your working directory will not reflect the changes that were just downloaded.
- The basic syntax for the git fetch command is git fetch [remote name]. The remote name is the name of the remote repository you want to fetch from (usually origin).
- You can use git fetch to check for new changes in a remote repository without modifying your local repository, and it's also useful when you want to review the changes before merging them with your local repository.
- It's important to note that git fetch only downloads the changes, it doesn't merge them into your local repository. To bring the changes into your local repository, you'll need to use git merge or git pull.