A comprehensive Git and GitHub crash course
What is Git?
Git is a widely-used version control system that allows software developers to track changes to their source code over time. It provides tools for collaboration, managing different versions of code, and easily reverting to previous versions if necessary.
What is Github?
GitHub is a web-based platform that provides developers with a centralized location to store and manage their Git repositories, collaborate with others on code, and host and review code. It also offers project management tools and features such as issue tracking, wikis, and code reviews.
Git Installation
To install Git on your computer, you can follow these general steps:
Note that the exact steps for installing Git may vary slightly depending on your operating system and the version of Git you are installing. The Git website provides detailed installation instructions for each operating system, which you can follow to ensure that Git is installed correctly.
Git Setup
Run these commands and your git will be ready to use the useful commands.
git config --global user.name "John Doe" # your name
git config --global user.email [email protected] # your email
Essential Git commands
Git init
git init?is a command used to initialize a new Git repository in a directory. It creates an empty repository with the necessary files and folders to track changes, enabling version control for the project.
$ git init
Initialized empty Git repository in /path/to/your/directory/.git/
Git add
git add?is a command used in Git to add changes made to files to the staging area, preparing them to be committed. It allows you to select specific files or directories to track for changes and include in the next commit.
git add . # to add all files
git add filename # add a specific file
Git status
Git status is a command used in Git version control system to display the current state of the repository. It shows information about any modified, added, or deleted files, as well as untracked files. For example:
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Git commit
A git commit is a snapshot of your code changes that you save in the version control system. It represents a milestone in the project’s history and includes a unique identifier, a message describing the changes, and references to the modified files.
$ git commit -m "Add feature X to the project"
Git push
git push?is a command used in Git to upload the local changes of a branch to a remote repository. It updates the remote repository with the latest commits, allowing collaboration and synchronization between team members.
领英推荐
$ git push origin master
Git clone
git clone?is a command used to create a local copy of a remote Git repository. It downloads all the files and history from the remote repository and sets up a local copy on your machine, allowing you to work with the code and contribute changes.
git clone https://github.com/example/repository.git
Git branch
A Git branch is a pointer to a specific commit in a repository’s version history. It allows for the creation and isolation of independent lines of development. An example would be creating a new branch called “feature-branch” from the “master” branch using the command:
Create a branch
$ git branch branchname
Check all branches
$ git branch
Switch branches
$ git checkout branchname
Merge a branch
$ git merge branchname
How to connect a repository to Github
To connect a GitHub repository to your local machine, you can follow these steps:
$ git init
4. Add your GitHub repository as a remote to your local repository by running:
$ git remote add origin <repository-url>
Replace?<repository-url>?with the URL of your GitHub repository. This establishes a connection between your local repository and the remote repository on GitHub.
5. Verify the remote connection by running:
$ git remote -v
This command will display the configured remotes, and you should see the origin URL.
6. You can now push your local repository to GitHub by running:
$ git push -u origin master
This command pushes the master branch of your local repository to the origin remote on GitHub. After this initial push, you can use?git push?to send subsequent changes to GitHub.
Congrats! Your local repository is now connected to the GitHub repository, allowing you to easily push and pull changes between the two.