Let’s consider an example where you have a project named “my-project” and you want to use Git for version control. Here’s how you can use Git commands in your daily workflow:
-
Initializing a Git Repository:
cd my-project git init
-
Adding and Committing Changes:
git add <file1> <file2> # Add specific files git add . # Add all files git status # Check the status of the repository git commit -m "Initial commit"
-
Cloning a Remote Repository:
git clone <repository-url>
-
Pulling and Pushing Changes:
git pull origin <branch> # Fetch and merge remote changes git push origin <branch> # Push local changes to remote repository
-
Creating and Switching Branches:
git branch # List all branches git branch <new-branch> # Create a new branch git checkout <branch> # Switch to a different branch
-
Merging Branches:
git checkout <target-branch> # Switch to the target branch git merge <source-branch> # Merge the source branch into the target branch
-
Viewing Commit History:
git log # Show commit history git log --oneline # Show condensed commit history
-
Checking Differences:
git diff # Show differences between working directory and staging area git diff --staged # Show differences between staging area and last commit
-
Adding Remote Repositories:
git remote add <name> <url> # Add a remote repository git remote -v # List all remote repositories
-
Discarding Changes:
git checkout -- <file> # Discard changes in a specific file git reset --hard # Discard all local changes and reset to last commit
These commands should give you a good starting point for using Git in your project. Remember to replace <file>
, <branch>
, <repository-url>
, and <new-branch>
with the appropriate values specific to your project.