git
Git User Manual
Table of Contents
- Git User Manual
- Table of Contents
- What is Git?
- Installing Git
- Basic Git Commands
- Branch Operations
- Advanced Git Features
- Troubleshooting
What is Git?
Git is a distributed version control system (DVCS) widely used for source code management (SCM) in projects involving multiple developers or requiring version control.
Installing Git
Windows
- Download the installer from the official Git website.
- Run the installer and follow the on-screen instructions to complete the installation.
macOS
-
Use Homebrew to install Git:
brew install git
Linux
-
Use the package manager for your distribution:
sudo apt-get install git # For Debian-based systems
sudo yum install git # For RedHat-based systems
Basic Git Commands
Initializing a Repository
To create a new repository, use the following command:
git init
Staging Files
To add files to the staging area, use:
git add <filename>
# Or stage all changes
git add .
Committing Changes
To commit staged changes to the repository, use:
git commit -m "Commit message"
Adding a Remote Repository
To add a remote repository, use:
git remote add origin <remote-repository-URL>
Pushing Changes
To push local commits to the remote repository, use:
git push origin <branch-name>
Cloning a Repository
To clone a remote repository, use:
git clone <remote-repository-URL>
Pulling Changes
To fetch and merge the latest changes from a remote repository, use:
git pull origin <branch-name>
Branch Operations
Creating and Switching Branches
To create a new branch and switch to it, use:
git checkout -b <new-branch-name>
Merging Branches
To merge another branch into the current branch, use:
git merge <branch-name>
Deleting Branches
To delete an unnecessary branch, use:
git branch -d <branch-name>
Advanced Git Features
Rebase
To rebase and reorganize your commit history, use:
git rebase <branch-name>
Stash
To temporarily save your current working state, use:
git stash
To apply stashed changes, use:
git stash apply
Tagging
To add a tag to a specific commit, use:
git tag <tag-name>
To push tags to a remote repository, use:
git push origin <tag-name>
Troubleshooting
Resolving Conflicts
If a conflict occurs during a merge, resolve it manually and complete the resolution with:
git add <resolved-file>
git commit
Reset
To undo a commit, use one of the following:
-
Soft reset (keep changes staged):
git reset --soft HEAD^ -
Hard reset (discard all changes):
git reset --hard HEAD^
This concludes the Git tutorial, covering both basic and advanced features. Use it to enhance your Git workflow!