0% found this document useful (0 votes)
5 views

Git Task

Git Task

Uploaded by

Dilli Royal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Git Task

Git Task

Uploaded by

Dilli Royal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

 1.

Configure Git with User Information

Purpose:

Identifies you as the author of commits.

Example:

bash

Copy code

git config --global user.name "John Doe"

git config --global user.email "[email protected]"

 Use the --global flag to apply settings system-wide. Use --local for a specific repository.

2. Initialize a New Git Repository

Purpose:

Prepares a directory for version control.

Example:

bash

Copy code

cd /path/to/project

git init

 Creates a hidden .git directory where Git stores the repository’s metadata and history.

3. Clone an Existing Repository

Purpose:

Copies a repository from a remote server to your local machine.

Example:

bash

Copy code

git clone https://github.com/username/repo.git

 Use HTTPS or SSH URLs for cloning. The repository will be downloaded into a folder named
repo.

4. Track the Status of Your Repository


Purpose:

Shows the current state of your working directory.

Example:

bash

Copy code

git status

 You can see untracked files, staged changes, and files with modifications.

5. Add New Files

Purpose:

Stages files for the next commit.

Example:

bash

Copy code

git add index.html

git add .

 Use git add <filename> for individual files or git add . to stage all changes.

6. Commit Changes with a Meaningful Message

Purpose:

Records changes to the repository with a descriptive message.

Example:

bash

Copy code

git commit -m "Added new landing page design"

 Commit messages should describe why a change was made, not just what.

7. Stage All Changes for Commit

Purpose:

Quickly stage all modified or new files.

Example:
bash

Copy code

git add .

 Use this when you’re confident all changes should be included in the next commit.

8. View Changes Between Working Directory and Last Commit

Purpose:

Shows the differences between your changes and the last commit.

Example:

bash

Copy code

git diff

 For staged changes, use:

bash

Copy code

git diff --cached

9. Review Commit History

Purpose:

View all commits made in the repository.

Example:

bash

Copy code

git log

git log --oneline

 Use git log --oneline --graph for a graphical history of branches.

10. Switch Between Branches

Purpose:

Switches the working directory to a different branch.

Example:
bash

Copy code

git switch feature-branch

 Use git checkout for older Git versions.

11. Create a New Branch for a Task

Purpose:

Isolates work on a new feature or task.

Example:

bash

Copy code

git branch feature-xyz

git switch feature-xyz

 Alternatively, combine both steps:

bash

Copy code

git checkout -b feature-xyz

12. Switch to a Branch

Purpose:

Switch to an existing branch.

Example:

bash

Copy code

git switch main

13. Merge a Feature Branch into the Main Branch

Purpose:

Combines changes from a feature branch into the main branch.

Example:

bash
Copy code

git switch main

git merge feature-xyz

14. Handle Merge Conflicts

Purpose:

Resolves conflicts when merging changes.

Steps:

1. Open files with conflicts (marked by <<<<<<<, =======, and >>>>>>>).

2. Resolve conflicts manually.

3. Stage the resolved files:

bash

Copy code

git add <file>

4. Commit the merge:

bash

Copy code

git commit

15. Delete an Old Branch After Merging

Purpose:

Clean up unused branches.

Example:

bash

Copy code

git branch -d feature-xyz

 To force delete:

bash

Copy code

git branch -D feature-xyz


16. Collaborate Using Pull Requests

Purpose:

Push a branch to a remote and propose merging it.

Steps:

1. Push your branch:

bash

Copy code

git push origin feature-xyz

2. Create a pull request on GitHub or GitLab.

17. Review and Approve Pull Requests

Purpose:

Ensures code quality and standards are met before merging.

 Use GitHub/GitLab to review changes, comment, and approve.

18. Push Changes to a Remote Repository

Purpose:

Sync local commits to a remote repository.

Example:

bash

Copy code

git push origin main

19. Fetch Updates from the Remote Repository

Purpose:

Downloads changes from the remote without merging.

Example:

bash

Copy code

git fetch
20. Pull Updates from the Remote Repository

Purpose:

Fetches and merges changes into your branch.

Example:

bash

Copy code

git pull origin main

21. Rebase a Feature Branch onto Main

Purpose:

Integrates changes from the main branch into a feature branch.

Example:

bash

Copy code

git checkout feature-xyz

git rebase main

22. Use Interactive Rebase to Clean Up Commit History

Purpose:

Combine or modify commits for a cleaner history.

Example:

bash

Copy code

git rebase -i HEAD~3

 Squash, reorder, or delete commits interactively.

Let me know if you’d like me to expand on specific sections or cover advanced items!

 You said:

40 task not given pls check

 ChatGPT said:

Here's the expanded explanation for tasks 23 to 40:


23. Tag a Release Version

Purpose:

Marks a specific point in your project's history for releases.

Example:

bash

Copy code

git tag v1.0

git push origin v1.0

 Use annotated tags for detailed metadata:

bash

Copy code

git tag -a v1.0 -m "Version 1.0 release"

24. View Commit History with a Graphical Representation

Purpose:

Visualizes the branch and commit structure.

Example:

bash

Copy code

git log --oneline --graph --all

 Helps in understanding complex branching and merging workflows.

25. Revert a Commit

Purpose:

Undo the changes from a specific commit without modifying the history.

Example:

bash

Copy code

git revert <commit-id>

 Creates a new commit that reverses the changes from the specified commit.
26. Reset the Repository to a Previous Commit

Purpose:

Moves the branch pointer to a previous commit.

Example:

bash

Copy code

git reset --hard <commit-id>

 Warning: This deletes changes after the specified commit.

27. Stash Unfinished Work

Purpose:

Temporarily saves changes without committing.

Example:

bash

Copy code

git stash

 Use git stash save "description" to add context to the stash.

28. Apply Stashed Changes Back to the Working Directory

Purpose:

Restores stashed changes.

Example:

bash

Copy code

git stash apply

 To remove the stash after applying:

bash

Copy code

git stash drop


29. Push a New Branch to the Remote Repository

Purpose:

Makes a local branch available on the remote repository.

Example:

bash

Copy code

git push origin <branch-name>

30. Delete a Remote Branch

Purpose:

Removes an outdated branch from the remote repository.

Example:

bash

Copy code

git push origin --delete <branch-name>

31. List All Branches in the Repository

Purpose:

Lists all branches (local and remote).

Example:

bash

Copy code

git branch # Local branches

git branch -r # Remote branches

git branch -a # All branches

32. Track Changes with Git Blame

Purpose:

Shows who made changes to specific lines in a file.

Example:

bash
Copy code

git blame <file-name>

 Useful for identifying contributors or debugging issues.

33. Review Changes Between Two Commits

Purpose:

Compares changes between specific commits.

Example:

bash

Copy code

git diff <commit1> <commit2>

34. Use Git Hooks to Automate Actions

Purpose:

Automates tasks during Git events (e.g., pre-commit checks).

Example:

 Set up a pre-commit hook in .git/hooks/pre-commit:

bash

Copy code

#!/bin/sh

# Pre-commit script example

npm test

 Make the file executable:

bash

Copy code

chmod +x .git/hooks/pre-commit

35. Use Git Submodules for External Resources

Purpose:

Links another Git repository inside your repository.

Example:
bash

Copy code

git submodule add <repository-url> <path>

 To update submodules:

bash

Copy code

git submodule update --remote

36. Create and Use Feature Flags with Git

Purpose:

Allows you to enable or disable features using branches or tags.

Example:

 Use branches to separate features:

bash

Copy code

git checkout -b feature-flag

 Deploy features based on tagged releases:

bash

Copy code

git tag feature-release

37. Set Up Continuous Integration (CI) with Git

Purpose:

Automates testing and deployment.

Example:

 Integrate GitHub Actions:

yaml

Copy code

# .github/workflows/ci.yml

name: CI Pipeline

on:
push:

branches:

- main

jobs:

build:

runs-on: ubuntu-latest

steps:

- uses: actions/checkout@v2

- name: Run Tests

run: npm test

38. Create and Manage Multiple Remotes for Project Forks

Purpose:

Collaborates between multiple repositories.

Example:

bash

Copy code

git remote add origin <origin-url>

git remote add upstream <upstream-url>

 To fetch changes from upstream:

bash

Copy code

git fetch upstream

39. Automate Deployment from Git

Purpose:

Deploys code automatically when changes are pushed to a branch.

Example:

 Use a CI/CD tool like Jenkins or GitHub Actions to deploy after testing.

40. Squash Multiple Commits into One


Purpose:

Combines several commits into a single one for a cleaner history.

Example:

bash

Copy code

git rebase -i HEAD~3

 Mark all but one commit as squash to combine them:

text

Copy code

pick abc123 Commit message 1

squash def456 Commit message 2

squash ghi789 Commit message 3

 Save and update the final commit message.

You might also like