Essential GitHub Commands: A Comprehensive Guide for Effective Version Control
August 8th, 2024 9:11 AM Mr. Q Categories: github
GitHub is an indispensable tool for developers, enabling collaboration, version control, and seamless code management. Whether you’re working on a personal project or contributing to a large team, understanding the core Git commands is crucial for efficient workflow management. This guide covers 30 of the most commonly used GitHub commands, providing you with the knowledge to navigate repositories, manage branches, and streamline your development process. Each command is accompanied by a detailed description, sample usage, and additional options to enhance your Git skills.
1. Initialize a Repository
git init
Initializes a new Git repository in the current directory.
Copy001$ git init
002Initialized empty Git repository in /path/to/repo/.git/
2. Clone a Repository
git clone <repository-url>
Copies an existing Git repository from a remote server to your local machine.
Copy001$ git clone https://github.com/user/repo.git
002Cloning into 'repo'...
003remote: Enumerating objects: 10, done.
004remote: Counting objects: 100% (10/10), done.
005Receiving objects: 100% (10/10), done.
3. Check Repository Status
git status
Shows the current state of the working directory and staging area.
Copy001$ git status
002On branch main
003Your branch is up to date with 'origin/main'.
004
005nothing to commit, working tree clean
4. Add Files to Staging Area
git add <file>
Adds files from the working directory to the staging area.
Copy001$ git add README.md
002$ git status
003Changes to be committed:
004 (use "git restore --staged <file>..." to unstage)
005 new file: README.md
Additional Arguments:
git add .
– Adds all changes in the current directory.
5. Commit Changes
git commit -m "<message>"
Records changes to the repository with a descriptive message.
Copy001$ git commit -m "Initial commit"
002[main (root-commit) e5d7f7c] Initial commit
003 1 file changed, 10 insertions(+)
004 create mode 100644 README.md
6. View Commit History
git log
Displays a list of previous commits in reverse chronological order.
Copy001$ git log
002commit e5d7f7caa57fbc617f39b0c9f84b8cd6e5d7f7c
003Author: User <user@example.com>
004Date: Wed Aug 8 14:33:07 2024 +0000
005
006 Initial commit
Additional Arguments:
git log --oneline
– Shows a condensed view of the commit history.
7. View Changes
git diff
Shows changes between the working directory and the staging area, or between commits.
Copy001$ git diff
002diff --git a/README.md b/README.md
003index e69de29..0a73e0b 100644
004--- a/README.md
005+++ b/README.md
006@@ -0,0 +1 @@
007+# Project Title
8. Create a New Branch
git branch <branch-name>
Creates a new branch in the repository.
Copy001$ git branch feature-branch
002$ git branch
003* main
004 feature-branch
005
006$ git branch -r
007 origin/HEAD -> origin/main
008 origin/main
009 origin/feature-branch
010 origin/bugfix-branch
011
012$ git branch -a
013* main
014 feature-branch
015 bugfix-branch
016 remotes/origin/HEAD -> origin/main
017 remotes/origin/main
018 remotes/origin/feature-branch
019 remotes/origin/bugfix-branch
-r : lists branches that exist on the remote repository (origin
), including the default branch pointer (HEAD
).
-a : shows all branches available locally and on the remote repository. Remote branches are prefixed with remotes/
and reflect the state of the remote repository.
9. Switch to a Branch
git checkout <branch-name>
Switches the working directory to the specified branch.
Copy001$ git checkout feature-branch
002Switched to branch 'feature-branch'
Additional Arguments:
git checkout -b <branch-name>
– Creates and switches to a new branch in one command.
10. Merge Branches
git merge <branch-name>
Merges changes from the specified branch into the current branch.
Copy001$ git checkout main
002$ git merge feature-branch
003Updating e5d7f7c..a1b2c3d
004Fast-forward
005 README.md | 1 +
006 1 file changed, 1 insertion(+)
11. Delete a Branch
git branch -d <branch-name>
Deletes the specified branch.
Copy001$ git branch -d feature-branch
002Deleted branch feature-branch (was a1b2c3d).
12. Push Changes
git push
Uploads local repository content to a remote repository.
Copy001$ git push origin main
002Enumerating objects: 5, done.
003Counting objects: 100% (5/5), done.
004Writing objects: 100% (3/3), 255 bytes | 255.00 KiB/s, done.
005Total 3 (delta 0), reused 0 (delta 0)
006To https://github.com/user/repo.git
007 e5d7f7c..a1b2c3d main -> main
13. Pull Changes
git pull
Fetches changes from a remote repository and merges them into the current branch.
Copy001$ git pull origin main
002Already up to date.
14. Set Remote Repository
git remote add origin <repository-url>
Associates a local repository with a remote one.
Copy001$ git remote add origin https://github.com/user/repo.git
15. List Remote Repositories
git remote -v
Displays the list of remote repositories associated with the local repository.
Copy001$ git remote -v
002origin https://github.com/user/repo.git (fetch)
003origin https://github.com/user/repo.git (push)
16. Remove a Remote
git remote remove <name>
Removes the specified remote repository association.
Copy001$ git remote remove origin
002$ git remote -v
17. Stash Changes
git stash
Temporarily saves changes in the working directory that are not yet ready to be committed.
Copy001$ git stash
002Saved working directory and index state WIP on main: e5d7f7c Initial commit
18. Apply Stash
git stash apply
Restores the most recently stashed changes.
Copy001$ git stash apply
002On branch main
003Changes not staged for commit:
004 (use "git add <file>..." to update what will be committed)
005
006modified: README.md
19. List Stashes
git stash list
Shows a list of all stashed changes.
Copy001$ git stash list
002stash@{0}: WIP on main: e5d7f7c Initial commit
20. Create a Tag
git tag <tag-name>
Creates a tag in the repository.
Copy001$ git tag v1.0
002$ git tag
003v1.0
21. Push Tags
git push origin <tag-name>
Uploads tags to the remote repository.
Copy001$ git push origin v1.0
22. Show Commit Information
git show <commit>
Displays detailed information about a specific commit.
Copy001$ git show e5d7f7c
002commit e5d7f7caa57fbc617f39b0c9f84b8cd6e5d7f7c
003Author: User <user@example.com>
004Date: Wed Aug 8 14:33:07 2024 +0000
005
006 Initial commit
007
008diff --git a/README.md b/README.md
009new file mode 100644
010index 0000000..e69de29
23. Revert a Commit
git revert <commit>
Creates a new commit that undoes the changes made by the specified commit.
Copy001$ git revert e5d7f7c
002[main a1b2c3d] Revert "Initial commit"
003 1 file changed, 10 deletions(-)
24. Reset to a Previous Commit
git reset --hard <commit>
Resets the working directory, staging area, and current branch to the specified commit.
Copy001$ git reset --hard e5d7f7c
002HEAD is now at e5d7f7c Initial commit
25. Rebase Branch
git rebase <branch>
Reapplies commits from the current branch on top of another base branch.
Copy001$ git checkout feature-branch
002$ git rebase main
003First, rewinding head to replay your work on top of it...
004Applying: Initial commit
26. Cherry-Pick a Commit
git cherry-pick <commit>
Applies the changes from a specific commit into the current branch.
Copy001$ git cherry-pick e5d7f7c
002[feature-branch a1b2c3d] Initial commit
003 1 file changed, 10 insertions(+)
27. Create an Alias
git config --global alias.<alias-name> <command>
Creates a shortcut for a Git command.
Copy001$ git config --global alias.co checkout
002$ git co main
003Switched to branch 'main'
28. Amend the Last Commit
git commit --amend
Modifies the most recent commit with any currently staged changes or to edit the commit message.
Copy001$ git commit --amend -m "Updated commit message"
002[main e5d7f7c] Updated commit message
003 Date: Wed Aug 8 14:33:07 2024 +0000
004 1 file changed, 10 insertions(+)
29. View the Commit Graph
git log --graph
Shows a graphical representation of the commit history.
Copy001$ git log --graph --oneline
002* a1b2c3d (HEAD -> main) Revert "Initial commit"
003* e5d7f7c Initial commit
Additional Arguments:
git log --graph --all --decorate --oneline
– Displays a more detailed commit graph with all branches.
30. Clean Untracked Files
git clean -f
Removes untracked files from the working directory.
Copy001$ git clean -f
002Removing untracked-file.txt
Additional Arguments:
git clean -fd
– Removes untracked files and directories.git clean -f -n
– Performs a dry run to show what would be removed without actually deleting anything.
Mastering GitHub commands is vital for anyone involved in software development. This guide has introduced 30 essential Git commands, from initializing a repository to cleaning untracked files. By understanding these commands, you can efficiently manage your code, collaborate with others, and maintain a clean and organized project history. Whether you’re new to GitHub or looking to refine your skills, this guide serves as a valuable resource for mastering version control.