Here are some common Git commands to help you manage your repository, including committing changes, pushing to a remote repository, and more:
Basic Git Workflow
Initial Setup
Initialize a New Git Repository:
git init
Clone an Existing Repository:
git clone <repository_url>
Staging and Committing Changes
Check the Status of Your Repository:
git status
Stage Changes:
- Stage all changes:
git add .
- Stage specific files:
git add <file1> <file2>
- Stage all changes:
Commit Changes:
git commit -m "Your commit message"
Working with Remote Repositories
Add a Remote Repository:
git remote add origin <repository_url>
Push Changes to a Remote Repository:
- Push to the master branch (or main branch):
git push -u origin master
- Push to a specific branch:
git push -u origin <branch_name>
- Push to the master branch (or main branch):
Pull Changes from a Remote Repository:
git pull origin <branch_name>
Branching and Merging
Create a New Branch:
git branch <new_branch_name>
Switch to a Different Branch:
git checkout <branch_name>
Create and Switch to a New Branch:
git checkout -b <new_branch_name>
Merge a Branch into the Current Branch:
git merge <branch_name>
Viewing History
View Commit History:
git log
View a Simplified Commit History:
git log --oneline
Undoing Changes
Unstage Changes:
git reset <file>
Undo Local Changes to a File:
git checkout -- <file>
Revert to a Previous Commit:
git revert <commit_id>
Reset to a Previous Commit:
- Soft reset (keeps changes in the working directory):
git reset --soft <commit_id>
- Hard reset (discards all changes):
git reset --hard <commit_id>
- Soft reset (keeps changes in the working directory):
Example Workflow
Initialize a Repository:
git init
Add a Remote Repository:
git remote add origin https://github.com/username/repository.git
Make Changes and Check Status:
git status
Stage Changes:
git add .
Commit Changes:
git commit -m "Initial commit"
Push Changes to Remote Repository:
git push -u origin master
This set of commands covers most of the basic operations you will need to manage your Git repositories effectively.
Comments
Post a Comment
If you have any doubts, please let me know.