Table of Contents
🌳 Managing branches in Git is a piece of cake using the “git branch” command! To list all branches:
$ git branch
To create a new branch:
$ git branch new_branch_name
one thing you need to note is that:
Certainly! The commands
git branch new_branch_name and
git checkout -b new_branch_name have distinct purposes in Git:
|This command is used to create a new branch with the specified name. However, after running this command, you will still be on the same branch you were before. You won’t switch to the newly created branch; it just gets created.||This command is a combination of creating a new branch and immediately switching to it. It’s a convenient shortcut that saves you from using two separate commands.|
|This command creates a new branch named “new_feature_branch,” but you’ll remain on your current branch.||In summary, |
git checkout -b
And to delete a branch:
$ git branch -d branch_name
This helps you keep track of your project’s different lines of development. 🌿🌐
🔀 Merging changes from one branch into another is a key Git operation using the “git merge” command! To integrate changes from a source branch into the current branch.
$ git merge source_branch
This combines the changes and histories of the two branches, creating a new merge commit. Be aware of potential conflicts that might arise during the merge process! 🌊🔄
🔄📥 To fetch and incorporate changes from a remote repository into your current branch, you can use the “git pull” command. It combines the “git fetch” command (which downloads changes from the remote repository) and the “git merge” command (which integrates those changes into your branch).
$ git pull origin main
This command fetches changes from the “main” branch of the remote repository called “origin” and merges them into your current branch.
Remember, conflicts might arise during the merge, so carefully review and resolve any conflicts that occur. 🌊🔗
📤 Uploading your local changes to a remote repository is easy using the “git push” command! This command sends your committed changes to the remote repository.
$ git push origin branch_name
This pushes the changes in the specified local branch to the remote repository (usually named “origin”) under the same branch name.
Make sure your local branch is up to date with the remote before pushing to avoid conflicts. Also, remember to pull and resolve any conflicts if necessary after pushing. 🚀📥
⏮️ The “git revert” command is used to create a new commit that undoes the changes made in a previous commit. This is a safer way to undo changes compared to “git reset,” as it keeps a record of the undo operation in the commit history.
$ git revert commit_to_revert
This creates a new commit that reverses the changes introduced in the specified commit.
Keep in mind that “git revert” does not delete or modify the original commit; it adds a new commit that undoes the changes. This is useful when you want to maintain a clear and complete history of changes. 🔄🔙
⏪ The “git reset” command is a powerful tool that can be used to manipulate the state of your working directory, staging area, and commit history in Git. It’s important to understand its different modes and implications:
Soft Reset (
--soft): This mode moves the branch pointer to a specified commit, but keeps the changes from the commits between the original and target commits in the staging area. It’s useful if you want to recommit the changes in a different way or organize your commit history.
Mixed Reset (
default, no flag): This is similar to
--mixed mode. It moves the branch pointer to a specified commit and resets the staging area. Changes from the commits between the original and target commits are retained in the working directory as uncommitted changes.
Hard Reset (
--hard): This mode moves the branch pointer, resets the staging area, and discards changes in the working directory to match the target commit. This can lead to permanent data loss, so use it carefully.
Here’s an example of using a mixed reset to
$ git reset HEAD file_to_unstage
And here’s an example of using a hard reset to revert changes in both the staging area and working directory:
$ git reset --hard commit_to_reset_to
Remember, hard resets can be risky as they delete unrecovered changes. Use these commands with caution and consider creating backups or using “git stash” to preserve changes if needed. ⚠️🔧
🏷️ Creating tags in Git is a way to mark specific points in history, such as important releases or versions of your project. There are two types of tags: lightweight tags and annotated tags.
Lightweight Tags: These are simple markers for specific commits, created with just a name. They don’t contain much information and are used for quick reference. To create a lightweight tag:
$ git tag tag_name commit_hash
Annotated Tags: These tags contain additional information like the tagger’s name, email, date, and an optional message. Annotated tags are recommended for marking important milestones or releases. To create an annotated tag:
$ git tag -a tag_name -m "Tag message" commit_hash
You can later push tags to a remote repository using:
$ git push origin tag_name
And to push all tags:
$ git push origin --tags
Tags help you easily refer back to specific points in your project’s history, making them useful for versioning and release management. 🏷️🔖
this series is continued and you can learn more about this on Git’s official site