Here are some advanced Git commands and their examples:
git bisect — used to find the commit that introduced a bug.
Example: git bisect start, git bisect bad, git bisect good [commit-hash], git bisect run [script-to-test]
git stash — temporarily saves changes that have not been committed.`````````````````````
Example: git stash, git stash apply, git stash drop
git rebase — used to reapply commits on a different branch.
Example: git rebase [branch-name], git rebase — onto [base-branch] [new-base] [branch-to-rebase]
git cherry-pick — allows you to apply a specific commit from one branch to another.
Example: git cherry-pick [commit-hash]
git reflog — shows a log of all reference changes in the local repository.
Example: git reflog
git reset — used to reset the current branch head to a specified commit.
Example: git reset [commit-hash], git reset — hard [commit-hash]
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
To merge local commits with a pull and reset, you can use the following steps:
Commit your local changes:
Use the command: git add . to stage your changes.
Use the command: git commit -m “commit message” to commit your changes to your local branch.
Git Pull:
Use the command: git pull origin branch_name to fetch and merge the changes from the remote repository into your local branch.
This will merge any new changes from the remote branch into your local branch, potentially creating merge commits in the process.
Git Reset:
Use the command: git reset — hard HEAD~n where n is the number of commits you want to “rewind” to before the merge commit.
This command will “reset” your branch to the specified commit, effectively “erasing” all the subsequent commits in your branch’s history.
Reapply your local changes:
Use the command: git cherry-pick commit_hash where commit_hash is the hash of the commit you want to reapply.
Repeat this step for each commit you want to reapply.
Note: It is important to make sure you have a backup of your work before using these commands, especially the git reset command.
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
To make multiple local commits merge with the remote commit history in a linear fashion, you can use the following steps:
Commit your local changes:
Use the command: git add . to stage your changes.
Use the command: git commit -m “commit message” to commit your changes to your local branch. Repeat this step for each local commit.
Git Pull:
Use the command: git pull origin branch_name to fetch and merge the changes from the remote repository into your local branch.
This will merge any new changes from the remote branch into your local branch, potentially creating merge commits in the process.
Git Rebase:
Use the command: git rebase -i HEAD~n where n is the number of local commits you have made.
This will open an interactive rebase session, allowing you to manipulate the order and structure of your local commits.
In the rebase session, you can use the “squash” command to combine multiple local commits into a single commit, effectively making your local commit history linear.
Note: It is important to make sure you have a backup of your work before using these commands, especially the git rebase command.
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
give the scenario and example for these two commands: `git pull — rebase` and `git rebase origin/master`
git pull — rebase
Scenario: You have made some local changes on your branch and want to merge the latest changes from the remote repository into your local branch, but you want to keep your local branch’s commit history linear.
Example: Let’s say you have a branch called “feature” and you have made two local commits on it. Meanwhile, someone else has pushed two new commits to the remote “master” branch. To merge the remote changes into your local branch while keeping the commit history linear, you can run the following command:
git checkout feature
git pull — rebase origin master
This will fetch the latest changes from the remote “master” branch and reapply your local commits on top of the updated remote branch, effectively merging the remote changes into your local branch while keeping the commit history linear.
git rebase origin/master
Scenario: You have made some local changes on your branch and want to reapply your local commits on top of the updated remote “master” branch, keeping your local branch’s commit history linear.
Example: Let’s say you have a branch called “feature” and you have made two local commits on it. Meanwhile, someone else has pushed two new commits to the remote “master” branch. To reapply your local commits on top of the updated remote branch, you can run the following command:
git checkout feature
git rebase origin/master
This will reapply your local commits on top of the updated remote “master” branch, effectively merging the remote changes into your local branch while keeping the commit history linear.
Note: It is important to make sure you have a backup of your work before using these commands, especially the git rebase command.
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
To remove all local changes and merge the latest changes from the remote repository, you can use the following steps:
Discard all local changes:
Use the command: git checkout . to discard all local changes in the working tree.
Git Pull:
Use the command: git pull origin branch_name to fetch and merge the changes from the remote repository into your local branch.
This will merge any new changes from the remote branch into your local branch, potentially creating merge commits in the process.
Note: It is important to make sure you have a backup of your work before discarding your local changes.
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
To merge detached head commits into the main branch, follow these steps:
Checkout the main branch: git checkout main
Fetch the latest changes from remote: git fetch origin
Merge the detached head into the main branch: git merge <commit-hash>
Push the changes to remote: git push origin main
Note: Replace <commit-hash> with the hash of the detached head commit. Also, it’s recommended to perform a git pull before merging to ensure the main branch is up to date with the remote repository.
No comments:
Post a Comment