git init
git clone <repository-url>
Example:
git clone https://github.com/username/repo-name.git
git status
git add <file-name>
Add all files:
git add .
git commit -m "Your commit message"
git log
For a more concise view:
git log --oneline
git diff
git branch
git branch <branch-name>
git checkout <branch-name>
git checkout -b <branch-name>
git merge <branch-name>
git branch -d <branch-name>
git remote add <remote-name> <repository-url>
Example:
git remote add origin https://github.com/username/repo-name.git
git remote -v
git fetch <remote-name>
git pull <remote-name> <branch-name>
git push <remote-name> <branch-name>
git checkout -- <file-name>
git reset HEAD <file-name>
git commit --amend
git reset --hard <commit-hash>
git revert <commit-hash>
git rebase -i <commit-hash>
git cherry-pick <commit-hash>
git tag <tag-name>
git stash
git stash apply
git clean -f
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
git config --global core.editor "vim"
git config --list
Imagine you're a developer starting a new project called "SuperApp". You decide to use Git for version control. Let's go through the process of setting up and managing this project using Git.
First, create a new directory for your project and initialize a Git repository:
mkdir SuperApp
cd SuperApp
git init
Set up your Git configuration:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Create a few initial files for your project:
echo "# SuperApp" > README.md
echo "console.log('Hello, SuperApp!');" > app.js
echo "body { font-family: Arial, sans-serif; }" > styles.css
Check the status of your repository:
git status
You'll see that these files are untracked.
Add these files to the staging area:
git add README.md app.js styles.css
Make your first commit:
git commit -m "Initial commit: Add README, app.js, and styles.css"
You want to add a new feature for user authentication. Create a new branch for this feature:
git checkout -b feature/user-auth
Add a new file for user authentication:
echo "function login() { /* TODO: Implement login logic */ }" > auth.js
Modify app.js to include the new auth.js file:
echo "import './auth.js';" >> app.js
echo "// TODO: Implement user authentication" >> app.js
Stage and commit these changes:
git add auth.js app.js
git commit -m "Add basic structure for user authentication"
Switch back to the main branch:
git checkout main
Create another branch for a different feature:
git checkout -b feature/data-visualization
Add a new file for this feature:
echo "function createChart() { /* TODO: Implement chart creation */ }" > chart.js
Stage and commit:
git add chart.js
git commit -m "Add basic structure for data visualization"
Switch back to main and merge the user authentication feature:
git checkout main
git merge feature/user-auth
Now try to merge the data visualization feature:
git merge feature/data-visualization
This might create a merge conflict if both branches modified app.js.
Open app.js and resolve the conflict manually. Then:
git add app.js
git commit -m "Merge feature/data-visualization and resolve conflicts"
Add a remote repository (assuming you've created one on GitHub):
git remote add origin https://github.com/yourusername/SuperApp.git
Push your changes to the remote repository:
git push -u origin main
For the data visualization feature, push the branch and create a pull request:
git checkout feature/data-visualization
git push -u origin feature/data-visualization
Then go to GitHub and create a pull request for this branch.
A new team member would clone the repository:
git clone https://github.com/yourusername/SuperApp.git
cd SuperApp
Before starting work each day, fetch and pull changes:
git fetch origin
git pull origin main
Create a new branch for a bug fix:
git checkout -b bugfix/login-error
Make changes, commit, and push:
# Make changes to auth.js
git add auth.js
git commit -m "Fix login error handling"
git push -u origin bugfix/login-error
Realize you forgot to update the README. Amend the last commit:
echo "## User Authentication" >> README.md
git add README.md
git commit --amend
Suppose you want to squash the last 3 commits on your feature branch:
git checkout feature/user-auth
git rebase -i HEAD~3
In the interactive rebase editor, mark commits to squash.
After merging all features and fixing bugs, create a release tag:
git tag -a v1.0.0 -m "Release version 1.0.0"
git push origin v1.0.0
If you make a mistake and want to discard changes:
git checkout -- auth.js
If you accidentally stage a file:
git reset HEAD styles.css
If you need to undo a commit:
git revert <commit-hash>
After merging feature branches, delete them:
git branch -d feature/user-auth
git branch -d feature/data-visualization
git push origin --delete feature/user-auth
git push origin --delete feature/data-visualization
Remove untracked files (be careful with this!):
git clean -n # Dry run
git clean -f # Actually remove files
Commit often: Make small, focused commits that address a single issue or feature.
Write meaningful commit messages: Use clear and descriptive commit messages that explain what changes were made and why.
Use branches: Create a new branch for each feature or bug fix to keep your work organized and isolated.
Pull before you push: Always pull the latest changes from the remote repository before pushing your own changes to avoid conflicts.
Review your changes: Use git diff
and git status
to review your changes before committing.
Use .gitignore: Create a .gitignore file to exclude files and directories that shouldn't be tracked by Git (e.g., build artifacts, temporary files).
Keep your repository clean: Regularly delete merged branches and use git clean
to remove untracked files.
Use meaningful branch names: Name your branches descriptively, e.g., "feature/user-authentication" or "bugfix/login-error".
Rebase feature branches: Use git rebase
to keep your feature branches up-to-date with the main branch and maintain a clean history.
Use tags for releases: Tag important commits, especially for releases, to easily reference them later.
2024 © All rights reserved - buraxta.com