Git
Initial Setup
Section titled “Initial Setup”| Command | Description |
|---|---|
git config --global user.name "Your Name" | Set your name for all Git repositories |
git config --global user.email "your.email@example.com" | Set your email |
git init | Create a new Git repository in your current directory |
Basic Commands
Section titled “Basic Commands”| Command | Description |
|---|---|
git status | Show the current state of your working directory and staging area |
git add <file> | Add a specific file to the staging area |
git add . | Add all changed files to the staging area |
git commit -m "message" | Create a commit with your staged changes |
git log | View the commit history |
git diff | Show unstaged changes in your working directory |
git diff --staged | Show changes staged for commit |
Branching and Merging
Section titled “Branching and Merging”| Command | Description |
|---|---|
git branch | List all local branches |
git branch <branch-name> | Create a new branch |
git checkout <branch-name> | Switch to a different branch |
git checkout -b <branch-name> | Create and switch to a new branch |
git merge <branch-name> | Merge specified branch into current branch |
git branch -d <branch-name> | Delete a branch |
Remote Repository Commands
Section titled “Remote Repository Commands”| Command | Description |
|---|---|
git remote add origin <url> | Connect your local repository to a remote one |
git push origin <branch-name> | Upload your changes to the remote repository |
git pull origin <branch-name> | Download and integrate remote changes |
Create a new feature branch
Terminal window git checkout -b username/featureMake changes and track them
Terminal window # Edit your filesgit status # Check what files have changedgit add script.py notebook.ipynbgit commit -m "Add new script and notebook"Update your branch with main branch changes
Terminal window git checkout maingit pull origin maingit checkout username/featuregit merge main # Merge main into your feature branchPush your changes and create a pull request
Terminal window git push origin username/featureAfter pull request approval, merge and cleanup
Terminal window git checkout maingit pull origin maingit branch -d username/feature # Delete the feature branch locally
Common Scenarios
Section titled “Common Scenarios”Undo changes to a file
git restore HEAD <file>Amending the last commit
git commit --amend -m "New commit message"Creating a .gitignore file
# Create .gitignoreecho "node_modules/" > .gitignoreecho "*.log" >> .gitignoregit add .gitignoregit commit -m "Add .gitignore file"Full tutorial
Section titled “Full tutorial”You can find the official Git tutorial here which is more in-depth.