Deleting a local git branch. The most common Git-related errors and questions, and convenient ways to solve them

💖 Like it? Share the link with your friends

Remote branches are links to the state of the branches in your remote repositories. These are local branches that cannot be moved; they move automatically whenever you communicate over the network. Remote branches act as bookmarks to remind you where branches in remote repositories were when you last connected to them.

They look like (remote repo name)/(branch) . For example, if you want to see what the master branch looked like on the origin server the last time you connected to it, check the origin/master branch. If you and a partner were working on the same issue and they posted an iss53 branch, you might have your own local iss53 branch; but that branch on the server will point to a commit in origin/iss53 .

All of this is perhaps confusing, so let's look at an example. Let's say you have a Git server on your network at git.ourcompany.com . If you clone something from it, Git will automatically name it origin , take all the data from there, create a pointer to whatever the master branch points to there, and name it locally origin/master (but you can't move it) . Git will also make you your own master branch, which will start at the same location as the master branch in origin, so you have something to work with (see Figure 3-22).

Figure 3-22. Cloning a Git project gives you your own master branch and origin/master pointing to the master branch in origin.

If you do something on your local master branch and in the meantime someone else pushes changes to git.ourcompany.com and updates the master branch there, then your stories will continue differently. Also, until you contact the origin server, your origin/master pointer will not move (see Figure 3-23).



Figure 3-23. When doing local work and someone pushing changes to a remote server, each story continues differently.

To keep your work in sync, run git fetch origin . This command looks up which server origin corresponds to (in our case it is git.ourcompany.com); extracts from there all the data that you do not yet have, and updates your local data store; shifts the origin/master pointer to a new position (see Figure 3-24).


Figure 3-24. The git fetch command updates your remote references.

To illustrate what remote branches would look like in a multi-remote situation, let's say you have another back-end Git server that is only used for development by one of your development teams. This server is located at git.team1.ourcompany.com . You can add it as a new remote reference to the project you're currently working on with the git remote add command, just as described in Chapter 2. Name this remote server teamone , which is short for the full URL (see Figure .3-25).



Figure 3-25. Adding an additional remote server.

Now you can git fetch teamone to fetch everything on the server that you don't. Since in this moment this server has only a fraction of the data that is on the origin server, Git does not fetch any data, but pushes a remote branch called teamone/master , which points to the same commit as the master branch on the teamone server (see Figure 3 -26).



Figure 3-26. You now have a local link to the master branch on teamone.

Submitting Changes

If you have a serverfix branch that you want to work on with someone else, you can push it just like you pushed your first branch. Do a git push (remote server) (branch):

$ git push origin serverfix Counting objects: 20, done. Compressing objects: 100% (14/14), done. Writing objects: 100% (15/15), 1.74 KiB, done. Total 15 (delta 5), ​​reused 0 (delta 0) To [email protected]:schacon/simplegit.git * serverfix -> serverfix

It's kind of an abbreviation. Git automatically expands the name of the serverfix branch to refs/heads/serverfix:refs/heads/serverfix , which means “take my local serverfix branch and update the remote serverfix branch from there”. We'll discuss the refs/heads/ part in detail in Chapter 9, but it can usually be omitted. You can also git push origin serverfix:serverfix - the same thing happens - it says "take my serverfix and make it a remote serverfix". You can use this format to push a local branch to a remote branch with a different name. If you don't want the branch to be named serverfix on the remote server, run git push origin serverfix:awesomebranch instead of the previous command. This will send your local serverfix branch to the awesomebranch branch of the remote project.

$ git fetch origin remote: Counting objects: 20, done. remote: Compressing objects: 100% (14/14), done. remote: Total 15 (delta 5), ​​reused 0 (delta 0) Unpacking objects: 100% (15/15), done. From [email protected]:schacon/simplegit * serverfix -> origin/serverfix

It's important to note that when you get new remote branches when you fetch data, you don't automatically get local editable copies of them. In other words, in our case, you won't get a new serverfix branch - just an origin/serverfix pointer, which you can't change.

To merge these developments into your current working branch, run git merge origin/serverfix . If you need your own serverfix branch to work on, you can create it from a remote branch:

$ git checkout -b serverfix origin/serverfix Branch serverfix set up to track remote branch refs/remotes/origin/serverfix. Switched to a new branch "serverfix"

This will give you a local branch to work on. It will start where origin/serverfix is.

Branch Tracking

Checking out a local branch with git checkout from a remote branch automatically creates what is called tracked branch. Tracked branches are local branches that are directly linked to a remote branch. If you type git push while on a tracked branch, Git will already know which server and which branch to push changes to. Similarly, doing a git pull on one of these branches first fetches all the remote refs and then automatically merges into the corresponding remote branch.

Cloning a repository will typically automatically create a master branch that tracks origin/master , so git push and git pull work for that branch out of the box and require no additional arguments. However, you can track other branches of the remote repository as well. You just saw a simple example of how to do this - git checkout -b [branch] [delete. server]/[branch] . If you are using Git versions 1.6.2 or later, you can also use the --track shorthand:

$ git checkout --track origin/serverfix Branch serverfix set up to track remote branch refs/remotes/origin/serverfix. Switched to a new branch "serverfix"

To set up a local branch with a different name than the remote branch, you can easily use the first version with a different local branch name:

$ git checkout -b sf origin/serverfix Branch sf set up to track remote branch refs/remotes/origin/serverfix. Switched to a new branch "sf"

Now your local sf branch will automatically push and pull changes from origin/serverfix.

Deleting branches on a remote server

Let's say you and your co-authors have finished with an innovation and merged it into the master branch on a remote server (or some other branch where the stable code is stored). You can delete a branch on a remote server using the somewhat goofy git push [remote] syntax. server] :[branch] . To remove the serverfix branch on the server, run the following:

$ git push origin:serverfix To [email protected]:schacon/simplegit.git-serverfix

Clap. No more branch on your server. You may want to bookmark the current page, as you will need this command and you will most likely forget the syntax. You can remember this command by reverting to the git push [remote] syntax. server] [loc. branch]:[delete branch] , which we considered a little earlier. Omitting part [loc. branch] , you are essentially saying “take nothing in my repository and make [delete. branch] was the same”.

If you want to get to know the parts of git that you were afraid to ask about before, then this list is for you. Here are collected the most typical situations and ways to solve them, both from the author's personal experience and collected all over the Internet.

Error in commit comment

If the commit has not yet been sent to the server (push), then you can use a simple command that allows you to edit the message text for the last commit.

git commit --amend

How to undo the last commit?

You can use git reset, like this:

git reset --hard HEAD~1

HEAD~1 means one commit before HEAD i.e. to the current position. It is worth noting that this is a "nuclear" method that will undo all changes. If you need to save everything you've done but haven't committed yet, use:

git reset --soft HEAD~1

Delete branch on server

git push origin --delete branchname

What is the difference between "git pull" and "git fetch"?

git pull is essentially git fetch followed immediately by git merge . git fetch fetches changes from the server and stores them in refs/remotes/ . This does not affect local branches and current changes in any way. And git pull already merges all these changes into the local copy.

How to undo "git add" before commit?

You git add filename by accident and want to undo the addition of this file. If the commit has not yet been made, then it will help:

git reset filename

How to resolve merge conflicts?

Use git mergetool , which provides a convenient interface for resolving conflicts.

Remove all local files and directories not tracked by git from your current copy

Carefully! Better make a backup before that.

Clone all branches from the server

Most likely, you have already done this, and the branches are simply hidden. Here is the command to show them all:

You can use git checkout origin/branchname to look at the correct branch. Or git checkout -b branchname origin/branchname to create a local branch corresponding to the remote one.

Rename local branch

git branch -m oldname newname

Revert to any commit

You can use reset as shown earlier, but this would mean that you want to permanently return to the state you were in, and not just look at it (for this you need to do a checkout). The commit ID must be the same as it appears in the output of the git log command.

git reset --hard commit_id

Again, this will undo all current changes, so make sure it's really what you want. Or use --soft instead of --hard .

Delete a submodule (submodule)

Creating submodules is rarely used, but sometimes they are still needed. So here's what you need:

git submodule deinit submodulename
git rm submodulename
git rm --cached submodulename
rm -rf .git/modules/submodulename

Overwrite local files during git pull

Git reset will help you again:

git fetch --all
git reset --hard origin/master

How to add an empty directory to a repository?

No way! It's just not supported and it's considered that you don't need it. But there is one trick. You can create a .gitignore file in the desired directory with the following content:

# Ignore everything in this directory
*
# Except the .gitignore file itself
!.gitignore

Exporting sources, similar to “svn export”

Use git archive like so:

git archive --format zip --output /path/to/file/file.zip master

Revert all changes except those already added to the planned commit

git checkout -- .

Create a new branch on the server from the current local branch

git config --global push.default current
git push -u

Recover deleted file

First you need to find the last commit where the file still existed.

If password authentication is used:

  1. $ git clone https://username:password@gitsrv/opt/git/repository.git

Working with branches

Show all branches:
  1. $ git branch
Create a new branch:
  1. $ git branch
Jump to new branch:
  1. $ git checkout
Create a new branch and go to it:
  1. $ git checkout -b
Delete local branch:
  1. $ git branch -d
Delete a branch from a remote repository:
  1. $ git push origin --delete

Working with commits

How to remove the last commit?

  1. $ git reset --soft HEAD^
Git HowTo. Chapter 16 Reverting Commits
Git HowTo. Chapter 17 Removing Commits from a Branch
Official Git documentation. Git Basics - Reverting Changes

How to change the last commit?

  1. $ git add new_file.txt
  2. $ git commit --amend

How to change the comment to the last commit?

  1. $ git commit --amend
  2. $ git commit --amend -m "New Comment"

How to merge multiple commits?

  1. $ git rebase -i HEAD~3
Instead of HEAD~3, you can use the hash of the commit. You need to pass the hash of the commit up to which you want to merge (flatten) everything.
An editor will open with a list of commits, with the oldest commit at the top.
  1. pick 1111111 Commit 1 comment
  2. pick 2222222 Commit 2 comment
  3. pick 3333333 Commit 3 comment
You need to replace pick with squash to make it look like this:
  1. pick 1111111 Commit 1 comment
  2. squash 2222222 Commit 2 comments
  3. squash 3333333 Commit 3 comments
Next, you need to save the file and exit. Will be open again text editor with all commit comments. You need to edit, save and exit. After these steps, the commits will be merged.

How to revert changes to a specific file and revert it to the state it was in after the last commit?

  1. $ git checkout -- file.txt

How to undo all uncommitted (uncommitted) changes?

  1. $ git checkout

How to hold onto some files for the next commit?

Let's say you want to commit changes in some files, and commit changes in other files in the next commit. Then you can temporarily remove them from the repository (unstage files), and then add them again.
  1. $ git reset HEAD file.txt
This command will remove the file from the repository, it will remain in the old commits. Head points to the latest commit in the current branch.

If you can't push to a remote repository because the current version of the repository is less than the one on the remote repository

In this case, you can do a forced push.
  1. $ git push -f origin master

Merging branches

How to take only some files from another branch?

  1. $ git checkout branchname -- path/to/file.file

Remote repositories

Displaying information about a remote repository

  1. $ git remote show origin
The screen will display something like this:
  1. * remote origin
  2. Fetch URL: git@gitsrv:/opt/git/test-project.git
  3. Push URL: git@gitsrv:/opt/git/test-project.git
  4. HEAD branch: master
  5. remote branch:
  6. master new (next fetch will store in remotes/origin)
  7. Local ref configured for "git push":
  8. master pushes to master (local out of date)

Adding a remote repository

  1. $ git remote add origin git@gitsrv:/opt/git/test-project.git

Remote branches are links to the state of the branches in your remote repositories. These are local branches that cannot be moved; they move automatically whenever you communicate over the network. Remote branches act as bookmarks to remind you where branches in remote repositories were when you last connected to them.

They look like (remote repo name)/(branch). For example, if you want to see what the master branch looked like on the origin server the last time you connected to it, check the origin/master branch. If you and a partner were working on the same issue and they posted an iss53 branch, you might have your own local iss53 branch; but that branch on the server will point to a commit in origin/iss53.

All of this is perhaps confusing, so let's look at an example. I created a remote repository on GitHub https://github.com/n0tb0dy/RemoreBranches

There I made three commits


When cloning a remote repository, Git will automatically name it origin, will take all the data from there, create a pointer to what the branch points to master, and call it locally origin/master(but you can't move it). Git will also make you your own branch master, which will start at the same place as the branch master V origin so you have something to work with.

“origin” is not a special name

This is similar to the name of the master branch, which is given by default when creating local repository. Just like a branch master created by default with the command git init, the same name is used by default origin on command git clone. If you issue the git clone -o booyah command, you will get booyah/master as your default remote branch.

And so we return to our ... commits. On the remote repository they look like this

Team git fetch just receives updates from a server that you don't have yet and doesn't change your working directory in any way. This command simply takes the data and allows you to decide what to do with it (combine with your data, edit, etc.)

Team git-pull , In most cases, immediately merges the received data with yours.

It's usually best to just use the git fetch command and the git merge command so you can control the merge process yourself.

Deleting remote branches

Of course, this refers to the removal of branches on remote server

$ git push origin --delete serverfix


Clap! And the branch on the remote server is gone. But in principle, this command simply deletes the branch pointer on the remote server. The Git server will continue to store all commit information until you run the garbage collection command.



tell friends