Git as a time machine for your code. It keeps track of every change you make to your files, so if you ever mess something up or want to go back to an earlier version, you can do it easily. It also helps you collaborate with others on the same code without stepping on each other's toes. So, in short, Git helps you manage your code's history and work together with others smoothly.
-
Installing Git:
• First, you need to install Git on your computer. You can download it from the official website (https://git-scm.com/), and the installation process is usually straightforward.
-
Configuring Git:
• After installing Git, you should configure your username and email address using the following commands:
git config --global user.name "Your Name" git config --global user.email "youremail@example.com"Replace "Your Name" and "youremail@example.com" with your actual name and email address.
-
Initializing a Repository:
• To start using Git in a project, navigate to its directory in your terminal or command prompt and run:
git initThis command initializes a new Git repository in the current directory.
-
Adding Files: • After initializing the repository, you can start adding files to it. Use the following command to add a file to the staging area (prepare it for a commit):
git add filenameReplace "filename" with the name of the file you want to add.
-
Making Commits: • Once you've added files to the staging area, you can create a commit with a message describing the changes. Use the following command:
git commit -m "Your commit message"Replace "Your commit message" with a brief description of the changes you've made.
-
Checking Status and History:
• To see the status of your repository (which files have been modified, staged, or committed), use:
git status• To view the commit history, including commit messages and changes made, run:
git log -
Branching and Merging:
• Branching allows you to work on different features or fixes independently. To create a new branch, use:
git branch branchnameReplace "branchname" with the name of your new branch. • To switch to a different branch, use:
git checkout branchname• After making changes in a branch, you can merge those changes back into another branch (often the main branch) using:
git merge branchname -
Remote Repositories and Collaboration:
• You can collaborate with others by using remote repositories hosted on platforms like GitHub or GitLab.
• To add a remote repository, use:
git remote add origin remote_repository_urlReplace "remote_repository_url" with the URL of the remote repository.
• To push your changes to the remote repository, use:
git push -u origin branchname• To fetch changes from the remote repository and merge them into your local branch, use:
git pull origin branchname -
Ignoring Files:
• You can specify files or directories to be ignored by Git using a .gitignore file. This is useful for excluding temporary files, build artifacts, or sensitive data.
• Create a .gitignore file in your project directory and list the files or patterns to ignore. For example:
*.log build/ secret.txt -
Undoing Changes:
• If you want to discard changes made to a file and revert it to the last committed version, you can use:
git checkout -- filename• If you want to unstage a file that you previously added, you can use:
git reset HEAD filename -
Interactive Staging:
• Git offers an interactive mode for staging changes, which allows you to selectively stage portions of a file instead of the entire file.
• Use the following command to enter interactive staging mode:
git add -i -
Rewriting History:
• You can rewrite the commit history using commands like
git commit --amend, git rebase, or git cherry-pick.However, use these commands with caution, especially if you've already shared your changes with others.
• Rewriting history can be useful for cleaning up commits, squashing multiple commits into one, or reordering commits.
-
Tags:
• Git tags are pointers to specific commits in the repository. They're commonly used to mark release points or significant milestones.
• To create a lightweight tag (just a pointer), use:
git tag tagname• To create an annotated tag (contains additional information like tagger name and date), use:
git tag -a tagname -m "Tag message" -
Git GUI:
• While most Git commands are executed via the command line, there are also graphical user interfaces (GUIs) available for Git.
• Examples include GitKraken, Sourcetree, and GitHub Desktop. These tools provide a visual representation of your repository and often
include additional features like drag-and-drop commits and conflict resolution.
-
Git Hooks:
• Git hooks are scripts that run automatically in response to certain actions in Git. They allow you to customize and automate your workflow.
• Examples of Git hooks include pre-commit (runs before a commit is created) and post-merge (runs after a successful merge).
-
Git Workflows:
• There are various Git workflows tailored to different development scenarios, such as the Centralized Workflow, Feature Branch Workflow,
Gitflow Workflow, and Forking Workflow.
• Each workflow defines a set of rules and best practices for collaborating on a project using Git.
-
Continuous Integration and Deployment (CI/CD):
• Git integrates with CI/CD systems like Jenkins, Travis CI, and CircleCI to automate the testing and deployment of your code.
• These systems can be configured to trigger builds and tests whenever changes are pushed to a repository,
ensuring code quality and facilitating rapid deployment.
-
Git Aliases:
• Git aliases allow you to create shortcuts for frequently used Git commands or command sequences.
• For example, you can create an alias named co for checkout using:
git config --global alias.co checkout• You can then use git co instead of git checkout.
-
Learning Resources:
• Git Pro book: https://git-scm.com/book/en/v2