Git Learning

📢 This article was translated by gemini-3-flash-preview

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:

1
2
git config --global user.name "Your Name"
git config --global user.email "[email protected]"

Replace Your Name and [email protected] with your actual info.

3. Creating a Repository

Choose a folder and run:

1
git init

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:

1
git clone https://github.com/yexca/typecho-theme-book.git

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:

1
git clone [email protected]:yexca/typecho-theme-book.git

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

How to make your first pull request on GitHub

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:

1
2
3
git add filename
# Or to add all modified files
git add .

Commit the staged files:

1
git commit -m "some details"

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).

1
2
3
4
5
# .gitignore file
test/    # Ignore the test folder
test     # Ignore the test file
*.py[cod]    # Ignore *.pyc *.pyo *.pyd files
!app.pyc    # Do NOT ignore app.pyc

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 .gitignore after 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:

1
2
3
4
git reset --hard commitID
# For example, if commit ID is eaadf4e385e865d25c48e7ca9c8395c3f7dfaef0:
git reset --hard eaadf4
# Just the first few unique digits are enough.

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:

1
git remote add origin [email protected]:[your repo]

(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:

1
git branch -m master main

Or set the global default to main:

1
git config --global init.defaultBranch main

Set the default upstream for your branch:

1
git push --set-upstream origin main

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:

1
git switch -c search

Commit changes on the search branch:

1
2
git add .
git commit -m "Add search feature"

Switch back to main:

1
git switch main

Merge the search branch:

1
git merge search

(Optional) Delete the search branch:

1
git branch -d search

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

Submit local master code to remote main branch

Making .gitignore changes take effect

This post is licensed under CC BY-NC-SA 4.0 by the author.
Last updated on 2025-01-03 22:33 +0900