Git is a distributed version control system designed to handle everything from small to very large projects with speed and efficiency. It's widely used for source code management in software development and facilitates collaboration.
- Go to the Git official website.
- Download the Windows installer.
- Run the installer and follow the installation instructions.
Git is usually available in the package repositories of most Linux distributions. You can install it using the package manager of your distribution. For example, on Ubuntu, you can install Git by running the following command in the terminal:
sudo apt-get install git
You can install Git using Homebrew. First, install Homebrew if you haven't already, then run:
brew install git
To create a new repository from an existing directory, use:
git init
- This command turns the current directory into a Git repository.
To clone a repository, use:
git clone <repository_url>
- This command creates a copy of the remote repository on your local machine.
To check the status of your files in the working directory and staging area, use:
git status
- This command shows you which files are staged, unstaged, and untracked.
To stage changes, use:
git add <file_or_directory>
- This command adds the specified files or directories to the staging area, preparing them to be committed.
To commit staged changes, use:
git commit -m "Your commit message"
- This command records the staged changes along with a descriptive message.
To push changes to the remote repository, use:
git push origin <branch_name>
- This command uploads your local commits to the remote repository.
To pull the latest changes from the remote repository, use:
git pull origin <branch_name>
- This command fetches and merges changes from the remote repository into your current branch.
To create a new branch, use:
git checkout -b <new_branch_name>
- This command creates and switches to a new branch.
To merge another branch into your current branch, use:
git merge <branch_name>
- This command integrates changes from the specified branch into your current branch.
To fetch changes from the remote repository without merging them, use:
git fetch origin
- This command downloads objects and refs from another repository.
When a merge conflict occurs, Git will notify you. To resolve conflicts:
- Open the conflicting files and resolve the conflicts manually.
- Stage the resolved files:
git add <file_with_conflict>
- Commit the resolved changes:
git commit -m "Resolved merge conflict in <file>"
If you need to overwrite local changes with the content from the HEAD, use:
git checkout -- <file>
- This command replaces the changes in your working directory with the latest content in the HEAD.
To discard all local changes and commits, retrieving the latest history from the server, use:
git fetch origin
git reset --hard origin/master
To create a new tag for a release, use:
git tag <tag_name> <commit_id>
- For example,
git tag 1.0.0 1b2e1d63ff
tags the commit with ID1b2e1d63ff
as1.0.0
.
To list all tags, use:
git tag
If you haven't cloned an existing repository and want to connect your repository to a remote server, use:
git remote add origin <server>
- This command connects your local repository to the remote server.
To push a branch to the remote repository, use:
git push origin <branch_name>
Your local repository consists of three "trees" maintained by Git:
- Working Directory: Contains the actual files.
- Index: Acts as a staging area.
- HEAD: Points to the last commit you've made.
- Make changes in your working directory.
- Stage changes by adding them to the Index using
git add
. - Commit changes to the repository using
git commit
. - Push changes to the remote repository using
git push
.
Branches allow you to develop features isolated from each other. The master
branch is the default branch when you create a repository.
- Create a new branch:
git checkout -b <branch_name>
- Switch back to the master branch:
git checkout master
- Delete a branch:
git branch -d <branch_name>
- Push a branch to the remote repository:
git push origin <branch_name>
Here's a basic illustration of how branches work in Git:
* -- * -- * -- * -- * -- * (main)
\ /
* -- * -- * (feature)
- In this diagram, the
main
branch represents the main line of development. - The
feature
branch is a separate line of development created frommain
.
Git is a powerful tool for managing code changes and collaboration. It allows multiple developers to work on a project simultaneously without overwriting each other's changes. Git's branching model is particularly useful for feature development and experimentation.
This documentation was inspired by various resources and tutorials available online, but this site really helped me get an overview as well as the classes I took at my job. Feel free to explore Git further and make the most of its features!
Enjoy your coding journey!