Hiển thị các bài đăng có nhãn git. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn git. Hiển thị tất cả bài đăng

Git Undo Merge: A Guide

 https://careerkarma.com/blog/git-undo-merge/

You can undo a Git merge using the git reset –merge command. This command changes all files that are different between your current repository and a particular commit. There is no “git undo merge” command but the git reset command works well to undo a merge.

How to Undo a Git Merge

Have you just merged two branches that you’re not ready to merge? Not to worry, Git has a solution for you. Developers merge branches all the time that should not be branched, so you’re definitely not alone in experiencing this issue.

In this tutorial, we’re going to talk about git merges and how to undo a git merge. We’ll walk through an example of two approaches you can use to undo a git merge. Let’s begin!

Git Merges

Git relies heavily on branches. These are used to maintain separate lines of development inside a project. Branches allow you to work on multiple different versions of a repository at once. Merging combines two branches into one.

You can merge a branch into another branch whenever you are ready. This means that you can develop a feature or a bug fix on a separate branch. Then, you can make it part of your main project later.

Git Undo Merge

To undo a git merge, you need to find the commit ID of your last commit. Then, you need to use the git reset command to reset your repository to its state in that commit. There is no “git revert merge” command.

The steps to revert a merge, in order, are:

  • git log OR git reflog (to find the last commit ID)
  • git reset –merge (to revert to the commit you specify)

Say we have accidentally merged two branches that should not be merged. We can undo these changes.

Undo Merge Git Example

Find the Commit ID

To start, we need to find out the commit ID of the commit before our merge on our remote repository. You can do this using git reflog:

git reflog

Let’s run this command on a Git repository:

ac7188c HEAD@{6}: commit: feat: Push example code for innerText and innerHTML tutorial
a9fdeb5 HEAD@{7}: commit (initial): feat: Merge dev-fix-7 into master

This command tells us that the last commit has the hash a9fdeb5.

Alternatively, you can use the git log command. But, the reflog command returns an output that is easier to read. This is why we opted to use reflog instead of the log command.

Revert to the Commit

We can use this hash to revert the merge commit with the git reset –merge command:

git reset --merge a9fdeb5

This command resets our repository to the state it was at in the a9fdeb5 commit on the master branch.

The –merge flag resets an index and updates all the files that are different between the current state of your repository and the HEAD.

This flag does not reset files with changes that have not been added to a commit. This makes it safer than using the oft-recommended git reset –hard command.

Once we have reset our repository, we can make the relevant changes to our code. Then, we can push them to a remote branch using the git push command.

A Word on the HEAD Shorthand

The Git HEAD keyword refers to the latest commit in your repository. You can use the Git HEAD shorthand to undo a merge:

git reset --merge HEAD~1

This command reverts our repository to the last commit. HEAD refers to the current state of your repository; HEAD~1 is the last commit in your repository.

This command reverts our repository to the last commit. HEAD refers to the current state of your repository; HEAD~1 is the last commit in your repository.

Conclusion

The git reset command undoes a merge. The –merge flag resets a repository but keeps files to which changes have been made that have not been added to your repository.

Are you interested in learning more about Git? Check out our How to Learn Git guide. In this guide, you’ll find advice on how to learn Git and a list of top learning resources.

GIT: Bitbucket delete master branch

 

git - Deleting remote master branch, refused due to being the current branch - Stack Overflow

Note: for Bitbucket, you would change the default branch by accessing the settings of your repository, and changing the branch at the "Main branch" combo box.




Once the main branch is no longer master, then you can push and remove master.

MarsAndBack confirms in the comments this applies to GitHub as well.

New branch from current branch

 How do I create a new branch in Git? | Learn Version Control with Git (git-tower.com)


How do I create a new branch based on the current HEAD?

To create a new branch that is based on your currently checked out (HEAD) branch, simply use "git branch" with the name of the new branch as the only parameter:

$ git branch <new-branch>

How do I create a new branch based on some existing one?

If you want to base your new branch on a different existing branch, simply add that branch's name as a starting point:

$ git branch <new-branch> <base-branch>


How do I create a new branch from a specific commit?

If you want to start your new branch based on a specific commit (not a branch), then you can provide the commit hash as the starting point:

$ git branch <new-branch> f71ac24d

How do I create a new branch from a specific tag?

You can also base your new branch on a specific tag you already have in your repository:

$ git branch <new-branch> v1.2

How do I create a new branch from a remote branch?

To take a remote branch as the basis for your new local branch, you can use the "--track" option:

$ git branch --track <new-branch> origin/<base-branch>

Alternatively, you can also use the "checkout" command to do this. If you want to name the local branch like the remote one, you only have to specify the remote branch's name:

$ git checkout --track origin/<base-branch>

How do I create a new branch in a remote repository?

After working on your new local branch for some time, you might want to publish it in your remote repository, to share it with your team:

$ git push -u origin <local-branch>

The "-u" flag tells Git to establish a "tracking connection", which will make pushing and pulling much easier in the future.

How to interprit git create mode message

 file permissions - How to read the mode field of git-ls-tree's output - Stack Overflow


From the Git index-format.txt file, regarding the mode:

32-bit mode, split into (high to low bits)

    4-bit object type
      valid values in binary are 1000 (regular file), 1010 (symbolic link)
      and 1110 (gitlink)

    3-bit unused

    9-bit unix permission. Only 0755 and 0644 are valid for regular files.
    Symbolic links and gitlinks have value 0 in this field.

Also, a directory object type (binary 0100) and group-writeable (0664 permissions) regular file are allowed as indicated by the fsck.c fsck_tree method. The regular non-executable group-writeable file is a non-standard mode that was supported in earlier versions of Git.

This makes valid modes (as binary and octal):

  • 0100000000000000 (040000): Directory
  • 1000000110100100 (100644): Regular non-executable file
  • 1000000110110100 (100664): Regular non-executable group-writeable file
  • 1000000111101101 (100755): Regular executable file
  • 1010000000000000 (120000): Symbolic link
  • 1110000000000000 (160000): Gitlink

The 6 digits show the file mode using the classical UNIX notations. First two digits show file type, the third one is about set-uid/set-gid/sticky bits, and you know the last three.

Here is how man 2 stat documents it on my GNU/Linux system:

   The following flags are defined for the st_mode field:

       S_IFMT     0170000   bit mask for the file type bit fields
       S_IFSOCK   0140000   socket
       S_IFLNK    0120000   symbolic link
       S_IFREG    0100000   regular file
       S_IFBLK    0060000   block device
       S_IFDIR    0040000   directory
       S_IFCHR    0020000   character device
       S_IFIFO    0010000   FIFO
       S_ISUID    0004000   set UID bit
       S_ISGID    0002000   set-group-ID bit (see below)
       S_ISVTX    0001000   sticky bit (see below)
       S_IRWXU    00700     mask for file owner permissions
       S_IRUSR    00400     owner has read permission
       S_IWUSR    00200     owner has write permission
       S_IXUSR    00100     owner has execute permission
       S_IRWXG    00070     mask for group permissions
       S_IRGRP    00040     group has read permission
       S_IWGRP    00020     group has write permission
       S_IXGRP    00010     group has execute permission
       S_IRWXO    00007     mask for permissions for others (not in group)
       S_IROTH    00004     others have read permission           
       S_IWOTH    00002     others have write permission
       S_IXOTH    00001     others have execute permission

Create a new branch from the current branch

 Git create new branch from master or current branch (mytrashcode.com)


To create a GIT branch from the current branch, you can use three commands sequentially and expect git to create a new branch for you.

git checkout master
git pull
git checkout <already_exisiting_branch>
git checkout -b <New_branch_name>

How this works :

  • It will first take you to master and pull the latest changes for all the branches of the repo.
  • Then move to an existing branch
  • Then creates a new branch from the existing branch with all the changes of the original branch.

There are few other ways you can create a branch. One is from a single commit and the other one is from any release tag.

Create a branch from a Commit

 Git create new branch from master or current branch (mytrashcode.com)


Create a branch from a Commit

To create a branch from a commit, we can use simply pass the commit hash to the checkout command. This will ignore all the changes made above and after that commit.

So your new branch will only have changes until the commit you specify. It will ignore all the changes post that.

Here’s what the command looks like.

git checkout master
git pull
git checkout -b <New_branch_name> <commit_hash_id>

Creating a branch from a release tag

Similar to creating a branch from commit, you can also create a commit from one of the release tags.

Here is how the command looks like.

git checkout master
git pull
git checkout -b <New_branch_name> <tag_version>

Example:

git checkout -b new_branch v1.0.0

Create a new branch from the current branch

 Git create new branch from master or current branch (mytrashcode.com)


Create a new branch from the current branch

To create a GIT branch from the current branch, you can use three commands sequentially and expect git to create a new branch for you.

git checkout master
git pull
git checkout <already_exisiting_branch>
git checkout -b <New_branch_name>

How this works :

  • It will first take you to master and pull the latest changes for all the branches of the repo.
  • Then move to an existing branch
  • Then creates a new branch from the existing branch with all the changes of the original branch.

There are few other ways you can create a branch. One is from a single commit and the other one is from any release tag.


How do I create a new branch based on some existing one?

If you want to base your new branch on a different existing branch, simply add that branch's name as a starting point:

$ git branch <new-branch> <base-branch>

If you're using the Tower Git client, you can simply use drag and drop to create new branches (and to merge, cherry-pick, etc.):

 https://www.educative.io/edpresso/the-fatal-refusing-to-merge-unrelated-histories-git-error

Solution

The error is resolved by toggling the allow-unrelated-histories switch. After a git pull or git merge command, add the following tag:

git pull origin master --allow-unrelated-histories

More information can be found here, ​ on Git’s official documentation.

How To Rename a Local and Remote Git Branch

 Source: https://linuxize.com/post/how-to-rename-local-and-remote-git-branch/


You are collaborating on a project with a group of people, and you have defined a naming convention for git branches. You created a new branch , pushed the changes to the remote repository, and realized that your branch name was incorrect.

Luckily, Git allows you to rename the branch very easily using the git branch -m command.

This guide explains how to rename local and remote Git branches.

Renaming Git Branch

Follow the steps below to rename a Local and Remote Git Branch:

  1. Start by switching to the local branch which you want to rename:

    git checkout <old_name>
  2. Rename the local branch by typing:

    git branch -m <new_name>

    At this point, you have renamed the local branch.

    If you’ve already pushed the <old_name> branch to the remote repository , perform the next steps to rename the remote branch.

  3. Push the <new_name> local branch and reset the upstream branch:

    git push origin -u <new_name>
  4. Delete the <old_name> remote branch:

    git push origin --delete <old_name>

That’s it. You have successfully renamed the local and remote Git branch

How to delete remote branches in Git

 https://www.educative.io/edpresso/how-to-delete-remote-branches-in-git


While working with Git, it is possible that you come across a situation where you want to delete a remote branch. But before jumping into the intricacies of deleting a remote branch, let’s revisit how you would go about deleting a branch in the local repository with Git.

Deleting local branches

  1. First, we print out all the branches (local as well as remote), using the git branch command with -a (all) flag.
  2. To delete the local branch, just run the git branch command again, this time with the -d (delete) flag, followed by the name of the branch you want to delete (test branch in this case).

Note: Comments are the output produced as a result of running these git commands

Note: You can also use the -D flag which is synonymous with --delete --force instead of -d. This will delete the branch regardless of its merge status.

Deleting remote branches

To delete a remote branch, you can’t use the git branch command. Instead, use the git push command with --delete flag, followed by the name of the branch you want to delete. You also need to specify the remote name (origin in this case) after git push.

Renewing Facebook Graph API token automatically?

  Mã truy cập dài hạn https://developers.facebook.com/docs/facebook-login/guides/access-tokens/get-long-lived/ https://community.n8n.io/t/re...