https://github.com/Asabeneh/git-github-beginners.git
This guide provides a thorough introduction to Git and GitHub, covering essential commands, workflows, and advanced techniques. Whether you're new to version control or seeking to master complex Git operations, this document serves as a complete reference. All commands are designed for use in a terminal.
Git is a distributed version control system that tracks changes to files, enabling multiple users to collaborate on projects efficiently. It records project history, allows branching for experimentation, and supports merging changes seamlessly.
GitHub is a platform for hosting Git repositories, facilitating collaboration through features like pull requests, issues, and code reviews. It integrates with Git to manage remote repositories.
This guide assumes basic familiarity with the command line. Commands are formatted as git <command> and can be run in a terminal (e.g., Bash, PowerShell, or Terminal.app).
brew install git) or Xcode Command Line Tools.sudo apt install git on Ubuntu).git --version.
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Optionally, set your preferred editor:
git config --global core.editor "nano"
git init .git folder to store version history.
git init
git status
git status
git log
git log
git log --oneline
git log --oneline
git add <filename>
git add README.md
git add .
git add .
git commit -m "commit message"
git commit -m "Add initial project files"
git commit --amend
git commit --amend -m "Updated commit message"
git diff
git diff
git diff --staged
git diff --staged
git reset <filename>
git reset README.md
git checkout -- <filename>
git checkout -- README.md
git revert <commit>
git revert abc1234
git reset --hard <commit>
git reset --hard abc1234
git branch <new-branch> <new-branch>.
git branch feature-branch
git checkout <new-branch>
git checkout feature-branch
git checkout -b <new-branch>
git checkout -b feature-branch
git branch
git branch
git branch -d <branch>
git branch -d feature-branch
git merge <branch>
git merge feature-branch
my-project).https://github.com/username/my-project.git).git remote add origin <url_of_remote_repo>
git remote add origin https://github.com/username/my-project.git
git remote -v
git remote -v
git clone <url_of_remote_repo>
git clone https://github.com/username/my-project.git
git push -u origin <branch>
git push -u origin main
git pull origin <branch>
git pull origin main
git fetch origin
git fetch origin
git stash
git stash
git stash pop
git stash pop
git stash list
git stash list
git tag <tagname>
git tag v1.0.0
git tag -a <tagname> -m "tag message"
git tag -a v1.0.0 -m "Release version 1.0.0"
git push origin <tagname>
git push origin v1.0.0
Rebasing rewrites commit history by moving or combining commits, creating a cleaner, linear history compared to merging.
git rebase <branch>
git rebase main
git rebase -i <commit>
Example: git rebase -i HEAD~3 to edit the last three commits.
Caution: Avoid rebasing commits that have been pushed to a shared repository, as it can disrupt collaborators' history.
git cherry-pick <commit>
git cherry-pick abc1234
Useful for selectively applying changes without merging an entire branch.
Git hooks are scripts that run automatically at specific points in the Git workflow (e.g., before a commit or push). They are stored in the .git/hooks directory.
pre-commit script to enforce code style checks:
# .git/hooks/pre-commit
#!/bin/sh
echo "Running code style checks..."
# Add your checks here (e.g., linting)
Make it executable:
chmod +x .git/hooks/pre-commit
Interactive rebasing allows you to modify commit history by squashing, reordering, or editing commits.
git rebase -i <commit>
git rebase -i HEAD~3
Options include:
pick: Keep the commit.squash: Combine with the previous commit.edit: Pause to amend the commit.Merge conflicts occur when Git cannot automatically reconcile changes from two branches. To resolve:
git merge <branch> and note the conflicting files.<<<<<<<, =======, and >>>>>>>.git add <filename>.git commit.git pull to avoid conflicts.git diff and git status before staging or committing.git reset --hard are irreversible; ensure you have backups..gitignore Updated: Prevent unwanted files (e.g., .env, node_modules) from being tracked.git init .git folder for version history. git init
git add <filename> git add file1 file2 file3
git add README.md
git add . git add .
git commit -m "commit message" git commit -m "Add initial project files"
git remote add origin <github-url> git remote add origin https://github.com/username/my-project.git
git push -u origin main main branch to the remote repository and sets it as the upstream branch. git push -u origin main
git pull git pull
git branch git branch
git branch -a git branch -a
git branch <branch-name> git branch feature-branch
git checkout <branch-name> git checkout feature-branch
git branch -d <branch-name> git branch -d feature-branch
git push -d origin <branch-name> git push -d origin feature-branch
git merge <branch-name> git merge feature-branch
git reset --hard HEAD~1