Recently, I've found myself forgetting many Git commands when trying to submit code. So, I decided to revisit Git commands, compile a list of the most frequently used ones, and enrich the content for better understanding. This not only reinforces my memory but also serves as a handy reference for future use.
In this blog, we'll cover the Git commands that are most commonly used in daily work, focusing on basic usage. Parameters within [] are optional, and those within <> are placeholders to be replaced with actual names relevant to your project. Note that in actual commands, you don't include the <> symbols. For instance, <origin> represents the remote repository name, and <master>, <next>, etc., represent branch names.
1. Git Configuration
Generating an SSH Key
To securely communicate with remote repositories, generate an SSH key:
ssh-keygen -t rsa -C "yourEmail@example.com"-t rsa: Specifies the type of key to create (RSA).-C: Adds a comment, such as your email.
Configuring Global Username and Email
Set your global username and email for all Git repositories:
git config --global user.name "Your Name"
git config --global user.email "yourEmail@example.com"- These settings are used in commit messages to identify the author.
Viewing Configuration Settings
To view your current Git configuration:
git config --list2. Cloning a Repository
Clone a remote repository to your local machine:
git clone http://yourGitAddress.git- This copies the entire repository, including its history.
3. Checking Status
View the status of your working directory and staging area:
git status- Shows tracked and untracked files, changes staged for commit, etc.
4. Staging Changes with git add
Adding Specific Files or Directories
Add a single file or directory to the staging area:
git add file_name.txt
git add path/to/directoryAdding All Changes in the Current Directory
Add all changes (excluding ignored files):
git add .Updating Tracked Files
Stage modifications and deletions of tracked files:
git add -u [path]- Omitting
[path]applies it to the current directory.
Adding All Changes Including Untracked Files
Stage all changes, including new files:
git add -A [path]Interactive Staging
Review and stage changes interactively:
git add -i [path]5. Committing Changes with git commit
Commit staged changes with a descriptive message:
git commit -m "Your commit message"- Use present tense and be concise.
Amending the Last Commit
Modify the last commit (e.g., to fix the message):
git commit --amend -m "Updated commit message"6. Pulling Updates with git pull
Pulling from a Remote Branch to a Local Branch
git pull origin remote-branch:local-branch- Fetches
remote-branchfromoriginand merges it intolocal-branch.
Pulling into the Current Branch
git pull origin master- Pulls from
origin/masterand merges into the current branch.
Resolving Merge Conflicts
If there are conflicts:
- Open conflicting files and resolve issues.
- Stage the resolved files:
git add resolved_file.txt - Continue the merge:
git commit
7. Pushing Changes with git push
Pushing Local Branch to Remote Branch
git push origin local-branch:remote-branch- Pushes
local-branchtooriginasremote-branch.
Pushing to a Same-Named Remote Branch
git push origin local-branch- If the remote branch doesn't exist, it's created.
Deleting a Remote Branch
git push origin --delete remote-branchOr:
git push origin :remote-branchPushing Current Branch to Remote
git push origin HEAD:remote-branch- Pushes current branch to
remote-branchonorigin.
Setting Upstream Tracking
Set the remote branch as upstream:
git push -u origin local-branch- After this,
git pushwill default toorigin/local-branch.
Pushing All Local Branches
git push --all origin8. Branch Management with git branch
Listing Branches
List all local branches:
git branchList all local and remote branches:
git branch -aCreating a New Branch
git branch new-branchDeleting a Local Branch
Delete a branch (force delete with -D if necessary):
git branch -d branch-nameDeleting a Remote Branch
git push origin --delete branch-name9. Merging Branches with git merge
Merging a Branch into Current Branch
git merge branch-to-merge- Merges
branch-to-mergeinto the current branch.
Merging Without Committing
git merge --no-commit branch-to-merge- Allows you to review changes before committing.
Merging Multiple Branches
git merge master branch-to-merge10. Switching Branches and Files with git checkout
Switching to Another Branch
git checkout branch-nameChecking Out a Specific Tag or Commit
git checkout tag-nameDiscarding Changes in a File
git checkout -- file_name.txt- Reverts
file_name.txtto the last committed state.
Creating and Switching to a New Branch
git checkout -b new-branch11. Resetting Changes with git reset
Unstaging a Staged File
git reset HEAD file_name.txt- Moves
file_name.txtfrom staging back to working directory.
Resetting to a Previous Commit
Soft Reset (Keeps Changes Staged)
git reset --soft HEAD~n- Moves HEAD back
ncommits; changes remain staged.
Mixed Reset (Default; Unstages Changes)
git reset [--mixed] HEAD~n- Moves HEAD back
ncommits; changes are unstaged.
Hard Reset (Discards Changes)
git reset --hard HEAD~n- Moves HEAD back
ncommits; changes in working directory are discarded.
Reset to a Specific Commit
git reset --hard commit_id12. Removing Files with git rm
Deleting a File from Repository and Local Filesystem
git rm file_name.txtDeleting a Directory
git rm -r path/to/directoryRemoving from Repository but Keeping Local Copy
git rm --cached file_name.txt13. Moving and Renaming with git mv
Moving a File
git mv file.txt new/path/Renaming a File
git mv old_name.txt new_name.txt14. Rewriting History with git rebase
Rebasing Current Branch onto Another
git rebase master- Reapplies commits from the current branch onto
master.
Continuing a Rebase After Conflict Resolution
git rebase --continueAborting a Rebase
git rebase --abortSkipping a Commit During Rebase
git rebase --skip15. Conclusion
This guide is a compilation of essential Git commands that are frequently used in daily development work. Understanding these commands can significantly improve your workflow and efficiency. If you find any errors or have suggestions for improvement, please let me know so I can make corrections promptly. Let's learn and grow together!
16. Additional Resources
Happy Coding!


