Git Bash (Pull 및 Push 코드, 변경 사항 추적 및 버전 관리)
Source: Dev.to
Prerequisites
- GitHub 계정
- Git Bash가 설치되어 있고 GitHub와 연결되도록 설정됨
- IDE (예: Visual Studio Code)
Introduction
Git Bash는 Windows 명령줄 애플리케이션으로, Git 및 GitHub 작업을 위한 Unix와 유사한 터미널 환경을 제공합니다. 이를 통해 push, pull, commit, clone 등 GitHub에 호스팅된 저장소에 대한 명령을 실행할 수 있습니다.
Git은 소스 코드의 변경 사항을 추적하는 버전 관리 시스템입니다. 무엇이 바뀌었는지, 언제 바뀌었는지, 누가 변경했는지를 기록하고, 필요할 때 이전 버전으로 되돌릴 수 있게 해줍니다. 이를 통해 안전한 협업이 가능하고, 실수로 작업이 손실되는 것을 방지하며, 디버깅이 쉬워집니다.
Creating a Local Repository
# Initialize a new Git repository in the current folder
git init
# Add all files to the staging area
git add .
# Or add a specific file
git add filename.ext
# Check the repository status
git status
Making the First Commit
git commit -m "Add initial project files"
# Set the default branch name to 'main'
git branch -M main
Connecting to a Remote Repository on GitHub
# Replace <URL> with the URL of your GitHub repository
git remote add origin <URL>
Pushing Code to GitHub
git push -u origin main
You can view the commit history with:
git log
git log shows the commit hash, author, date, and commit message.
Pulling Code from GitHub
Method 1 – Clone (first time)
# Replace <URL> with the URL of the remote repository
git clone <URL>
This creates a folder named after the repository, downloads all files, and sets origin to the GitHub repo.
Method 2 – Pull (subsequent updates)
# Verify you are on the correct branch
git branch
# Pull all updates and merge them into the current branch
git pull
# Or pull a specific branch (e.g., development)
git pull origin development
Tracking Changes
git log
git log displays when commits were made, who made them, and the associated commit messages.
Reverting a Problematic Change
If a recent commit introduces a bug, you can revert it:
# Replace <commit-hash> with the commit hash you want to revert
git revert <commit-hash>
After reverting, push the updated history to keep the remote repository in sync:
git push -u origin main
Summary
You now understand how to:
- Initialize a local Git repository
- Commit changes and set a default branch
- Connect to and push code to a remote GitHub repository
- Clone and pull updates from GitHub
- Track commit history with
git log - Revert unwanted changes
These fundamentals enable effective collaboration and version control for any software project.