Just some notes on the Git stuff I’ve learned.
1. Installation
- Installing Git on Windows
Download and install from the official Git website: https://git-scm.com/download/win
Run Git Bash once after the installation is complete.
- Installing Git on Linux
Debian: sudo apt-get install git
Fedora: dnf install git
CentOS: yum install git
Other distros: https://git-scm.com/download/linux
- Installing Git on Mac OS X
Method 1: Install Homebrew, then run brew install git. See
http://brew.sh/
for details.
Method 2: Install Xcode from the App Store. It includes Git, but it’s not installed by default. Open Xcode, go to Xcode - Preferences, find the Downloads tab, select Command Line Tools, and click Install.
Xcode is Apple’s official IDE. It’s powerful, free, and essential for Mac/iOS development.
2. Configuration
After installation, configure your identity:
| |
Replace Your Name and [email protected] with your actual info.
3. Creating a Repository
Choose a folder and run:
| |
This folder is now a Git-managed repository. Don’t mess with files inside the ./.git directory (usually hidden); Git uses it to track versions.
Alternatively, you can clone a repo from GitHub.
Cloning someone else’s repo
If you don’t need to push changes, use HTTPS:
| |
Get the link from the repository page by clicking the Code button.
If you need to push changes, the owner must add your key to the repo’s Deploy keys. In this case, use SSH:
| |
Usually, it’s better to fork the repo, make changes, and then submit a Pull Request.
References: How to give tea to the big shots using GitHub - idealclover
Cloning your own repo
First, add your SSH public key in your personal settings: SSH and GPG keys (github.com)
Then, either method works.
4. Committing Files Locally
After making changes, it’s time to commit.
Add files to the staging area:
| |
Commit the staged files:
| |
Describe your changes in some details.
What if you don’t want to track certain files?
Create a .gitignore file and list the filenames (one per line).
| |
You can force a commit of an ignored file with git add -f test.pyc.
Check out ready-made templates here: https://github.com/github/gitignore
If you need to modify
.gitignoreafter files are already tracked, clear the cache first:
1 2 3 4 5 6# Clear cache git rm -r --cached . # Re-add files git add . # Commit git commit -m "update .gitignore"
5. Logs and Undoing Changes
Check the repo status: git status
View changes since last commit: git diff
View history: git log. Use --pretty=oneline to condense it and see commit IDs.
The latest commit is marked as HEAD -> master (the current version). Git uses HEAD for current, HEAD^ for the previous, and HEAD~60 for 60 versions back.
Roll back to the previous version: git reset --hard HEAD^
Roll back to a specific version:
| |
If you messed up a rollback, don’t close the terminal. Run git reflog to find your previous commit ID and jump back.
6. Linking Local Repo to GitHub
To upload a local repo to GitHub, create a new repo on GitHub and link it:
| |
(Not needed if you cloned the repo).
To push local changes to the remote: git push origin master
Note: VS Code defaults to master as the branch name, while GitHub defaults to main. Pushing to main directly might fail. You can rename your local branch:
| |
Or set the global default to main:
| |
Set the default upstream for your branch:
| |
After this, you can just use git push origin.
If you made a mistake while linking:
Check remote info: git remote -v
Remove the remote: git remote rm RepoName (usually git remote rm origin).
7. Branch Management
Branches let you work on features or fixes without affecting the main code.
List branches: git branch
Create branch: git branch new_branch
Switch branch: git checkout new_branch or git switch new_branch
Push to a specific branch: git push origin branch_name
Example: Adding a search feature
Create and switch to the search branch:
| |
Commit changes on the search branch:
| |
Switch back to main:
| |
Merge the search branch:
| |
(Optional) Delete the search branch:
| |
8. Creating a Server
Reference: Building a Git Server and Syncing to a Specific Directory
9. Others
Using GitLab and GitHub simultaneously: document-library/Git-study.md
References
document-library/Git-study.md at master · LiangJunrong/document-library · GitHub
Git Tutorial - Liao Xuefeng’s Website
How to connect local repo with GitHub - CSDN
How to give tea to the big shots using GitHub - idealclover
How to make your first pull request on GitHub