GitHub, a platform for hosting and collaborating on Git repositories, relies on Git for version control. Below are important Git commands commonly used with GitHub workflows:
Basic Git Commands
- Configuration:
git config --global user.name "Your Name"
Sets the username for commits.git config --global user.email "your_email@example.com"
Sets the email address for commits.
- Initialize a Repository:
git init
Creates a new Git repository in the current directory.
- Clone a Repository:
git clone <repository_url>
Clones an existing repository to your local machine.
- Check Repository Status:
git status
Shows the status of the working directory and staging area.
- Add Changes:
git add <file_name>
Stages a specific file for commit.git add .
Stages all changes in the current directory.
- Commit Changes:
git commit -m "Commit message"
Saves staged changes with a message.
- Push Changes:
git push origin <branch_name>
Pushes local commits to the remote repository.
- Pull Changes:
git pull origin <branch_name>
Fetches and merges changes from the remote branch to the local branch.
Branch Management
- Create a Branch:
git branch <branch_name>
Creates a new branch.git checkout -b <branch_name>
Creates and switches to a new branch.
- Switch Branches:
git checkout <branch_name>
Switches to the specified branch.
- List Branches:
git branch
Lists all local branches.git branch -r
Lists all remote branches.
- Delete a Branch:
git branch -d <branch_name>
Deletes a branch locally.
Merging and Rebasing
- Merge Branches:
git merge <branch_name>
Merges the specified branch into the current branch.
- Rebase Branches:
git rebase <branch_name>
Re-applies commits from one branch onto another.
Remote Repository Management
- Add a Remote Repository:
git remote add origin <repository_url>
Links a local repository to a remote repository.
- List Remote Repositories:
git remote -v
Shows the URLs of remote repositories.
- Remove a Remote:
git remote remove <name>
Removes a remote repository.
Inspect and Compare
- View Commit History:
git log
Displays commit history.git log --oneline --graph --all
Shows a compact, graphical view of commit history.
- View Differences:
git diff
Shows differences between working directory and staging area.git diff <branch_name>
Compares the current branch with another branch.
- Show Commit Details:
git show <commit_hash>
Displays details about a specific commit.
Stashing and Cleaning
- Stash Changes:
git stash
Temporarily saves changes not ready for commit.git stash apply
Re-applies stashed changes.git stash drop
Deletes a stash.
- Clean Untracked Files:
git clean -f
Removes untracked files.
Tagging
- Create a Tag:
git tag <tag_name>
Creates a lightweight tag.git tag -a <tag_name> -m "Message"
Creates an annotated tag.
- Push Tags:
git push origin <tag_name>
Pushes a specific tag.git push origin --tags
Pushes all tags.
- List Tags:
git tag
Lists all tags.
Undo Changes
- Unstage Changes:
git reset <file_name>
Unstages a file from the staging area.
- Undo Last Commit:
git reset --soft HEAD~1
Keeps changes in the working directory.git reset --hard HEAD~1
Deletes the last commit and changes.
- Revert a Commit:
git revert <commit_hash>
Creates a new commit that undoes the specified commit.
GitHub-Specific Commands
- Fork a Repository:
- Done through the GitHub interface, not via command line.
- Open a Pull Request:
- Use GitHub’s web interface to propose changes to a repository.
- Resolve Conflicts:
- Fetch changes with
git pull
, resolve conflicts in the code editor, and commit.
- Fetch changes with
Tips for Using Git Commands
- Use
git help <command>
for detailed help on any command. - Always check the status with
git status
before committing or pushing. - Write meaningful commit messages to maintain a clear history.
- Use
.gitignore
to exclude files from being tracked.
These commands form the foundation of GitHub workflows and can handle most version control tasks effectively.
Discover more from Technology with Vivek Johari
Subscribe to get the latest posts sent to your email.