Git Basics
Git Basic Terminology
main: Default branch in Git origin: Default remote branch alias in Git HEAD: Current working branch position
Git Configuration Commands
git config --global user.name "{username}" git config --global user.email "{emailaddr}" git config --global core.editor "vim" git config --global core.pager "cat" git config --list
- If modifications are needed, edit values in the
.gitconfigfile:
vi ~/.gitconfig
Git Version Control Commands
git init # Initialize current directory as a git repository (repo) git clone {remoteaddr} # Download remote repo from the given address to current local directory git status # Check git version control status git add {filename} # Prepare specific file for version control → stage to staging area git commit # Save version control → upload to local repo git push origin main # Save to remote repository → upload to remote repo git log # View version control history (--pretty=oneline: show one commit per line)
Git Branch Commands
git branch # List local branches git branch -r # List remote branches (e.g. origin/HEAD -> origin/main : indicates the default(HEAD) branch of remote origin is main) git branch -a # List all branches git branch {branchname} # Create a branch git switch {branchname} # Switch to the specified branch - all files from previous workspace exist, but now in an independent space git merge {branchname} # (After switching to the target merge space) Merge the specified branch into the current branch git branch -d {branchname} # Delete the specified branch (always delete after merging is complete)
Resolving Git Merge Conflicts
- Directly open and edit the conflicting file (common approach)
$ vi {filename} # When editing (make sure to delete =====, <<<<<, >>>>>) # Add and commit the edited changes $ git add {filename} $ git commit # At this point, typically no separate title is needed
Other Git Commands
git remote -v # Remote repo alias and address
Github & Git Example
# 1. Clone remote repository from GitHub $ git clone https://github.com/username/project.git $ ls $ cd project # 2. Add .gitignore, write content, and commit $ touch .gitignore $ vi .gitignore $ git add .gitignore $ git commit # conf: Create .gitignore # 3. Create and switch to a new working branch $ git branch function-modify $ git branch -a $ git switch function-modify # 4. Modify and commit func.py file $ vi func.py $ git add func.py $ git commit # fix: Fixed Potential XYZ Loop Error in func.py # 5. Edit and commit README.md file $ vi README.md $ git add README.md $ git commit # docs: Update README.md with modified usage instructions # 6. Merge into main branch $ git switch main $ git merge function-modify # if CONFLICT → manually resolve in the file, then add, commit # 7. Push main branch to remote repository $ git push origin main
Good Habits to Develop When Using Git
- Always create a
.gitignorefile first when making a repo - Use prefix and capitalization when doing
$ git add {filename} - Develop the habit of using
$ git add {filename}instead of$ git add .(staging all files in current directory): build the habit of writing what was modified in eachcommit - Develop the habit of writing the first line directly in the editor window that appears after
$ git commitinstead of$ git commit -m "{title}": in special cases like merge, a title may already be written. Changing it in such cases can be confusing.