Github
Introduction
This guide provides a step-by-step process for using Git and GitHub, from installation to managing repositories, branches, and commits. It is designed for beginners and intermediate users, covering essential commands and common workflows.
Prerequisites
Before starting, ensure you have: - A GitHub account (sign up at GitHub). - Git installed on your system. - Basic familiarity with the command line.
Installing Git
For Ubuntu
sudo apt install git
For Windows
Windows: Download and install Git from git-scm.com.
Verify installation:
git --version
Configuring Git
Set up your Git identity (run these commands once):
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
To store credentials for convenience:
git config --global credential.helper store
Note
The credential.helper store
command saves your GitHub credentials locally, so you don’t need to re-enter them for each push. Use with caution on shared computers.
Creating a GitHub Repository
Log in to your GitHub account.
Click the + icon in the top-right corner and select New repository.
Provide a repository name, description (optional), and choose visibility (public/private).
Optionally, initialize with a README, .gitignore, or license.
Click Create repository to generate the repository URL (e.g.,
https://github.com/username/repo.git
).
Initializing a Local Repository
Navigate to your project directory:
cd /path/to/your/project
Initialize a local Git repository:
git init
Note
If you accidentally initialize a repository in the wrong folder:
- On Windows, list hidden files with dir /a
to find the .git
folder, then delete it with rmdir /s /q .git
.
- On Linux/Mac, use ls -a
to find .git
, then delete with rm -rf .git
.
Ignoring Files with .gitignore
Create a .gitignore
file to exclude files/folders from being tracked (e.g., logs, temporary files):
touch .gitignore
Example .gitignore
content:
# Ignore all .log files
*.log
# Except specific log file
!important.log
# Ignore entire folder
web-app/*
If a file/folder is already tracked, remove it from Git’s index:
git rm -r --cached <folder_or_file>
git commit -m "Update .gitignore to ignore <folder_or_file>"
Staging and Committing Changes
Stage all changes:
git add .
Commit changes with a message:
git commit -m "Descriptive commit message"
View commit history:
git log
Linking to a Remote Repository
Connect your local repository to the GitHub repository:
git remote add origin <repository_url>
Verify the remote:
git remote -v
Pushing to GitHub
Push changes to the main
branch (or master
in older repositories):
git push -u origin main
Note
The -u
flag sets the upstream branch, so subsequent pushes can use simply git push
.
Pulling Updates from GitHub
Update your local repository with changes from the remote branch:
git pull origin main
Working with Branches
List Branches
View all local and remote branches:
git branch -a
Create and Switch to a New Branch
git checkout -b <branch_name>
Note
For Git 2.23+, you can use git switch -c <branch_name>
to create and switch to a branch.
Switch to an Existing Branch
git checkout <branch_name>
Or with Git 2.23+:
git switch <branch_name>
Merge a Branch
Switch to the target branch (e.g., main
) and merge another branch:
git checkout main
git merge <branch_name>
Delete a Branch
Delete a local branch:
git branch -d <branch_name>
Delete a remote branch:
git push origin --delete <branch_name>
Note
Use -D
instead of -d
to force-delete a local branch with unmerged changes.
Cloning a Repository
Clone a repository to your local machine:
git clone <repository_url>
For a shallow clone (only the latest state):
git clone --recurse-submodules --depth 1 <repository_url>
Managing Commits
Revert to a Previous Commit
Undo changes by reverting to a specific commit:
git revert <commit_id>
Reset to a Previous Commit
Reset to a previous commit, discarding later commits:
git reset --hard <commit_id>
Warning
Use git reset --hard
cautiously, as it permanently deletes commits after the specified ID.
Soft reset (keeps changes in working directory):
git reset --soft <commit_id>
Force Push
Force push to overwrite remote commit history (use with caution):
git push -f
Restoring Files
Restore a Specific File
Revert a modified but uncommitted file to its last committed state:
git checkout -- <file_name>
Or with Git 2.23+:
git restore <file_name>
Restore All Files
Revert all uncommitted changes:
git checkout .
# Or
git restore .
Reset Local Repository to Match Remote
Fetch the latest remote state and reset local repository:
git fetch origin
git reset --hard origin/<branch_name>
Cleaning Untracked Files
Remove untracked files and directories:
git clean -df
Troubleshooting
Write Access Denied
If you encounter: Write access to repository not granted. fatal: unable to access
Ensure you have write permissions for the repository.
Check your credentials or SSH key configuration.
Refer to this StackOverflow thread for detailed solutions.
Detached HEAD State
If you checkout a specific commit (e.g., git checkout <commit_id>
), you enter a detached HEAD state. To save changes:
Create a new branch:
git checkout -b new-branch-name
Commit changes and push as usual.
Additional Commands
List Files in Current Branch
git ls-files
Check Current Branch
git branch
Conclusion
This guide covers the essential Git and GitHub workflows for managing repositories, branches, and commits. Practice these commands in a test repository to build confidence before applying them to critical projects. For further details, refer to the Git documentation or GitHub Docs.