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.
$ git init
Initialized 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.
$ git clone https://github.com/user/repo.git
Cloning into 'repo'...
remote: Enumerating objects: 10, done.
remote: Counting objects: 100% (10/10), done.
Receiving objects: 100% (10/10), done.
3. Check Repository Status
git status
Shows the current state of the working directory and staging area.
$ git status
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
4. Add Files to Staging Area
git add <file>
Adds files from the working directory to the staging area.
$ git add README.md
$ git status
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
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.
$ git commit -m "Initial commit"
[main (root-commit) e5d7f7c] Initial commit
1 file changed, 10 insertions(+)
create mode 100644 README.md
6. View Commit History
git log
Displays a list of previous commits in reverse chronological order.
$ git log
commit e5d7f7caa57fbc617f39b0c9f84b8cd6e5d7f7c
Author: User <user@example.com>
Date: Wed Aug 8 14:33:07 2024 +0000
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.
$ git diff
diff --git a/README.md b/README.md
index e69de29..0a73e0b 100644
--- a/README.md
+++ b/README.md
@@ -0,0 +1 @@
+# Project Title
8. Create a New Branch
git branch <branch-name>
Creates a new branch in the repository.
$ git branch feature-branch
$ git branch
* main
feature-branch
$ git branch -r
origin/HEAD -> origin/main
origin/main
origin/feature-branch
origin/bugfix-branch
$ git branch -a
* main
feature-branch
bugfix-branch
remotes/origin/HEAD -> origin/main
remotes/origin/main
remotes/origin/feature-branch
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.
$ git checkout feature-branch
Switched 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.
$ git checkout main
$ git merge feature-branch
Updating e5d7f7c..a1b2c3d
Fast-forward
README.md | 1 +
1 file changed, 1 insertion(+)
11. Delete a Branch
git branch -d <branch-name>
Deletes the specified branch.
$ git branch -d feature-branch
Deleted branch feature-branch (was a1b2c3d).
12. Push Changes
git push
Uploads local repository content to a remote repository.
$ git push origin main
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Writing objects: 100% (3/3), 255 bytes | 255.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/user/repo.git
e5d7f7c..a1b2c3d main -> main
13. Pull Changes
git pull
Fetches changes from a remote repository and merges them into the current branch.
$ git pull origin main
Already up to date.
14. Set Remote Repository
git remote add origin <repository-url>
Associates a local repository with a remote one.
$ 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.
$ git remote -v
origin https://github.com/user/repo.git (fetch)
origin https://github.com/user/repo.git (push)
16. Remove a Remote
git remote remove <name>
Removes the specified remote repository association.
$ git remote remove origin
$ git remote -v
17. Stash Changes
git stash
Temporarily saves changes in the working directory that are not yet ready to be committed.
$ git stash
Saved working directory and index state WIP on main: e5d7f7c Initial commit
18. Apply Stash
git stash apply
Restores the most recently stashed changes.
$ git stash apply
On branch main
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
modified: README.md
19. List Stashes
git stash list
Shows a list of all stashed changes.
$ git stash list
stash@{0}: WIP on main: e5d7f7c Initial commit
20. Create a Tag
git tag <tag-name>
Creates a tag in the repository.
$ git tag v1.0
$ git tag
v1.0
21. Push Tags
git push origin <tag-name>
Uploads tags to the remote repository.
$ git push origin v1.0
22. Show Commit Information
git show <commit>
Displays detailed information about a specific commit.
$ git show e5d7f7c
commit e5d7f7caa57fbc617f39b0c9f84b8cd6e5d7f7c
Author: User <user@example.com>
Date: Wed Aug 8 14:33:07 2024 +0000
Initial commit
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..e69de29
23. Revert a Commit
git revert <commit>
Creates a new commit that undoes the changes made by the specified commit.
$ git revert e5d7f7c
[main a1b2c3d] Revert "Initial commit"
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.
$ git reset --hard e5d7f7c
HEAD is now at e5d7f7c Initial commit
25. Rebase Branch
git rebase <branch>
Reapplies commits from the current branch on top of another base branch.
$ git checkout feature-branch
$ git rebase main
First, rewinding head to replay your work on top of it...
Applying: Initial commit
26. Cherry-Pick a Commit
git cherry-pick <commit>
Applies the changes from a specific commit into the current branch.
$ git cherry-pick e5d7f7c
[feature-branch a1b2c3d] Initial commit
1 file changed, 10 insertions(+)
27. Create an Alias
git config --global alias.<alias-name> <command>
Creates a shortcut for a Git command.
$ git config --global alias.co checkout
$ git co main
Switched 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.
$ git commit --amend -m "Updated commit message"
[main e5d7f7c] Updated commit message
Date: Wed Aug 8 14:33:07 2024 +0000
1 file changed, 10 insertions(+)
29. View the Commit Graph
git log --graph
Shows a graphical representation of the commit history.
$ git log --graph --oneline
* a1b2c3d (HEAD -> main) Revert "Initial commit"
* 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.
$ git clean -f
Removing 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.