Essential Git and GitHub Commands for Version Control
This guide provides a comprehensive list of commonly used Git and GitHub commands, along with their purposes. Whether you're a beginner or an experienced developer, understanding these commands is crucial for effective version control and collaboration.
Sr. | Command | Purpose |
---|---|---|
1 | git config --global user.name |
Displays the global username configured for Git commits. |
2 | git config --global user.name "Khurram Shahzad" |
Sets the global username for Git commits to "Khurram Shahzad". |
3 | git config --global user.email |
Displays the global email address configured for Git commits. |
4 | git config --global user.email "kswork@gmail.com" |
Sets the global email address for Git commits to "kswork@gmail.com". |
5 | git init |
Initializes a new Git repository in the current directory. |
6 | git add index.js |
Stages the file index.js for the next commit. |
7 | git diff |
Shows changes between the working directory and the last commit (or staged changes). |
8 | git add . |
Stages all modified and new files in the current directory for the next commit. |
9 | git rm <FILE_PATH> |
Removes a file from the working directory and stages the deletion for the next commit. |
10 | git commit -m "initial code added" |
Commits staged changes with the message "initial code added". |
11 | git log |
Displays the commit history for the repository. |
12 | git blame <FILE_PATH> |
Shows who last modified each line of a file and in which commit. |
13 | git status |
Shows the current status of the working directory and staging area. |
14 | git log --oneline |
Displays the commit history in a compact, one-line-per-commit format. |
15 | git reset --hard <COMMIT_ID> |
Resets the current branch to the specified commit, discarding all changes in the working directory and index. |
16 | git revert <COMMIT_ID> |
Creates a new commit that undoes the changes made in the specified commit. |
17 | git remote -v |
Lists all remote repositories and their URLs. |
18 | git push -u origin main |
Pushes the local main branch to the origin remote and sets it as the upstream branch. |
19 | ssh-keygen -t rsa -b 4096 -C "YOUR_EMAIL" |
Generates a new SSH key pair with RSA encryption, 4096-bit length, and a comment with the specified email. |
20 | git push -f |
Force-pushes the local branch to the remote, overwriting the remote branch. Warning: Use with caution, as it can overwrite changes. |
21 | git branch |
Lists all branches in the repository, with the current branch marked by an asterisk. |
22 | git branch <BRANCH_NAME> |
Creates a new branch with the specified name. |
23 | git checkout <BRANCH_NAME> |
Switches to the specified branch. |
24 | git push --set-upstream origin <BRANCH_NAME> |
Pushes the local branch to the origin remote and sets it as the upstream branch. |
25 | git merge <BRANCH_NAME> |
Merges the specified branch into the current branch. |
26 | Branch Name Convention (Not a command) | Common prefixes for branch names:
|
27 | git stash |
Temporarily saves uncommitted changes in the working directory and index, allowing a clean working state. |
28 | git stash apply |
Applies the most recent stashed changes to the working directory without removing them from the stash. |
29 | git stash pop |
Applies the most recent stashed changes and removes them from the stash. |
30 | git stash drop |
Removes the most recent stash from the stash list without applying it. |
Notes
- Ensure you replace placeholders like
<FILE_PATH>
,<COMMIT_ID>
, and<BRANCH_NAME>
with actual values. - Use
git push -f
cautiously, as it can overwrite remote changes. - The branch naming convention helps organize workflows in collaborative projects.
Mastering these commands will streamline your Git workflow and enhance collaboration on platforms like GitHub. Let me know in the comments if you need further clarification!
0 Comments