What is a Branch?
A branch in Git is essentially a pointer to a specific commit in the history of your project. It allows you to diverge from the main line of development and work on new features, bug fixes, or other changes independently.
Git Revert and Reset:
Git Revert and Git Reset are two Git commands used to undo changes, but they operate differently and are suited for different scenarios.
Git Revert: Undo a specific commit by creating a new commit that reverses the changes.
Git Reset: Move the current branch to a different commit, effectively "undoing" commits by altering the history.
What is Git Rebase and Merge?
Git Rebase and Git Merge are two primary strategies for integrating changes from one branch into another. Both serve the purpose of combining work, but they do so in different ways and with different impacts on the project’s commit history.
Git Rebase: It takes the commits from your current branch and applies them on top of another branch, reordering them to create a linear history. This approach avoids creating extra merge commits and results in a cleaner project history.
Git Merge: It combines the changes from one branch into another by creating a merge commit that includes the history of both branches. This method keeps all original commits and maintains the full development history, making it ideal for collaboration.
Accessing Git Using SSH Keys
Accessing Git using SSH keys adds an extra layer of security and is commonly used for authenticating with GitHub, GitLab, and other Git services. Here's how to set up SSH keys and use them with Git:
Step 1: Generate SSH Keys
- Open your terminal and generate a new SSH key pair:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Replace "your_email@example.com" with your email address. This command will generate a 4096-bit RSA key pair.
2. You'll be prompted to save the key to a specific location. The default location (~/.ssh/id_rsa) is usually fine, so just press Enter.
3. If you want extra security, you can set a passphrase. Otherwise, press Enter for no passphrase.
Generating a 4096-bit RSA key pair is not strictly necessary but is recommended for stronger security. A 4096-bit RSA key provides a high level of security and is generally considered secure for the foreseeable future.
Step 2: Add the SSH Key to Your SSH Agent
- Start the SSH agent in the background:
eval "$(ssh-agent -s)"
The command eval "$(ssh-agent -s)" starts the SSH agent in the background and sets up your shell environment to interact with the agent. ssh-agent manages your SSH keys and holds them in memory, so you don't need to enter your passphrase each time you use the keys.
2. Add your SSH private key to the agent:
ssh-add ~/.ssh/id_rsa
3. Verify the SSH Key has been Added
ssh-add -l
This command lists the SSH keys currently managed by the SSH agent. The output should show the key length, the SHA256 fingerprint, and any associated comment (like your email address). If you see your key listed, it means the key has been successfully added.
Step 3: Add the SSH Key to Your Git Service (e.g., GitHub, GitLab)
- Copy the SSH public key to your clipboard:
cat ~/.ssh/id_rsa.pub
2. Add the SSH key to your Git service
For GitHub: Go to Settings > SSH and GPG keys > New SSH key, and paste your key.
For GitLab: Go to Profile > SSH Keys > Add SSH Key, and paste your key.
Step 4: Clone the Repository Using SSH
Clone the repository using the SSH URL:
git clone git@github.com:username/repository.git
Replace username with your GitHub username and repository with the name of your repository. Cloning via SSH allows you to push and pull changes without needing to enter your username and password each time, as the authentication is handled by your SSH key.
Git Workflow with SSH Key
- Create and Switch to the 'dev' Branch:
git checkout -b dev
2. Make Changes in the dev Branch:
- Add a file or modify an existing one:
echo "This is the development branch" > dev_file.txt
- Stage and commit the change:
git add dev_file.txt
git commit -m "Added dev_file in dev branch"
3. Create and Switch to the feature Branch:
git checkout -b feature
4. Make Changes in the feature Branch:
- Add another file:
echo "This is the feature branch" > feature_file.txt
- Stage and commit the change:
git add feature_file.txt
git commit -m "Added feature_file in feature branch"
5. Merge the feature Branch into dev:
- Switch back to the dev branch:
git checkout dev
- Merge the feature branch into dev:
git merge feature
6. Merge the dev Branch into main:
- Switch back to the main branch:
git checkout main
- Merge the dev branch into main:
git merge dev
Understanding git rebase
git rebase can be used as an alternative to merging. It allows you to integrate changes from one branch into another by moving or replaying commits.
- Make More Changes in the dev Branch:
- Switch to the dev branch:
git checkout dev
- Make changes:
echo "Additional changes in dev" >> dev_file.txt
git add dev_file.txt
git commit -m "Added more content to dev_file in dev"
2. Rebase the dev Branch onto main:
- Switch to the dev branch:
git checkout dev
- Rebase the dev branch onto main:
git rebase main
Step 5: Push the Changes to the Remote Repository
Finally, push your changes to the remote repository:
git push origin main