Mastering GitHub Commands: A Practical Guide for Version Control
Navigating Git for Seamless Collaboration and Code Management
GitHub, a powerhouse in version control and collaboration, stands as a cornerstone in the toolkit of every Flutter developer. Whether you're building mobile apps, contributing to open-source projects, or collaborating with a team, GitHub's features are tailored to streamline your workflows. In this guide, we'll explore 30 commonly used Git commands along with their explanations, use cases, and implementation details.
Let's get started
git init
Explanation: Initializes a new Git repository.
Use: Create a new repository for a project.
Implementation:
git init
git clone
Explanation: Clones a remote repository to your local machine.
Use: Obtain a copy of a repository to work on.
Implementation:
git clone
https://github.com/example/repository.git
git add
Explanation: Adds changes in a file to the staging area.
Use: Prepare changes for the next commit.
Implementation:
git add filename.txt
git commit -m "Commit message"
Explanation: Records changes to the repository.
Use: Save staged changes with a descriptive message.
Implementation:
git commit -m "Added new feature"
git status
Explanation: Shows the status of changes as untracked, modified, or staged.
Use: View the current state of the repository.
Implementation:
git status
git log
Explanation: Displays the commit history.
Use: Review the history of commits.
Implementation:
git log
git pull
Explanation: Fetches changes from a remote repository and merges them into the current branch.
Use: Update your local repository with changes from the remote.
Implementation:
git pull origin master
git push
Explanation: Pushes local changes to a remote repository.
Use: Share your changes with others.
Implementation:
git push origin master
git branch
Explanation: Lists all local branches.
Use: View available branches in your repository.
Implementation:
git branch
git branch <branch_name>
Explanation: Creates a new branch.
Use: Start working on a new feature or bugfix.
Implementation:
git branch new_feature
git checkout <branch_name>
Explanation: Switches to the specified branch.
Use: Move to a different branch.
Implementation:
git checkout new_feature
git merge <branch_name>
Explanation: Combines changes from one branch into another.
Use: Integrate changes from a feature branch into the main branch.
Implementation:
git merge new_feature
git remote -v
Explanation: Lists remote repositories.
Use: View configured remote repositories.
Implementation:
git remote -v
git remote add
Explanation: Adds a new remote repository.
Use: Link your local repository to a remote one.
Implementation:
git remote add origin
https://github.com/example/repository.git
git fetch
Explanation: Retrieves changes from a remote repository without merging.
Use: Update your local repository without merging changes.
Implementation:
git fetch origin
git diff
Explanation: Shows the differences between working directory and staging area.
Use: Examine changes made but not yet committed.
Implementation:
git diff
git reset
Explanation: Unstages changes for a file.
Use: Remove a file from the staging area.
Implementation:
git reset filename.txt
git remote remove
Explanation: Removes a remote repository.
Use: Disconnect from a remote repository.
Implementation:
git remote remove origin
git rm
Explanation: Removes a file from both the working directory and the staging area.
Use: Delete a file from the repository.
Implementation:
git rm filename.txt
git stash
Explanation: Temporarily saves changes that are not ready to be committed.
Use: Save your changes and revert the working directory to the last commit.
Implementation:
git stash
git stash apply
Explanation: Restores the most recently stashed changes.
Use: Retrieve the changes saved with
git stash
.Implementation:
git stash apply
git tag <tag_name>
Explanation: Creates a lightweight tag for the current commit.
Use: Mark important points in history (releases, milestones).
Implementation:
git tag v1.0.0
git show <tag_name>
Explanation: Displays information about a tag.
Use: View details about a specific tag.
Implementation:
git show v1.0.0
git remote show
Explanation: Displays information about a remote repository.
Use: View details about a configured remote.
Implementation:
git remote show origin
git revert
Explanation: Creates a new commit that undoes changes made in a previous commit.
Use: Reverse the effects of a specific commit.
Implementation:
git revert abc123
git cherry-pick
Explanation: Applies changes from a specific commit to the current branch.
Use: Pick a specific commit and apply its changes to the current branch.
Implementation:
git cherry-pick abc123
git fetch --prune
Explanation: Prunes remote-tracking branches that no longer exist on the remote.
Use: Remove references to branches that have been deleted on the remote.
Implementation:
git fetch --prune
git config --global user.name "Your Name"
Explanation: Configures the global username for Git.
Use: Set the author name for your commits.
Implementation:
git config --global
user.name
"John Doe"
git config --global user.email "your.email@example.com"
Explanation: Configures the global email address for Git.
Use: Set the author email for your commits.
Implementation:
git config --global
user.email
"
john.doe@example.com
"
git log --oneline --graph --all
Explanation: Displays a compact summary of the commit history with a graphical representation.
Use: Visualize the commit history in a concise and graphical format.
Implementation:
git log --oneline --graph --all
Remember to replace placeholder values (such as <repository URL>
, <branch_name>
, <name>
, <url>
, <file>
, <commit>
, etc.) with actual values relevant to your Git repository and workflow.