git failed to push some refs to | git push command explained

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
we all have faced this situation before we make some amazing code changes to the code base that we're working on and then push it to our local branch we then raise a pull request to merge those amazing changes from our branch into the main branch we add our peers to the pull request so that they can evaluate the awesomeness of our code but to our utter shock and surprise our code is not as perfect as we initially thought and the group of peers that we added to the code review just as a formality actually give us some review comments this is the time when we question our life choices and go back to the pr in order to make some changes to improve the code once done we want to push our code changes to reflect in the pr and hence we run the git commit command with the amend flag and then do git push but this is when it hits us we get this annoying message that stops us from pushing to our own feature branch it says that our push was rejected but why is that in order to understand why that fails we need to understand how the git push command works very simply put the git command pushes all the commits in the local repository to the remote repo in the long form push command where we specify something like git push origin main we are telling git to just take all the commits that are there in my local main branch and push them to the main branch on the remote named origin which is from where we clone the code in the first place in this example we see that the commit in green is the one that is extra on the local branch and that is the one that would get reflected on the remote branch once the push is successful but there is a catch git will allow you to push your local commits to the remote if and only if all the commits in the remote are present in the local as predecessors of the new commits that we are trying to push this is a security mechanism and get to make sure that any two users pushing one after the other in quick succession do not end up losing the previously pushed commit in the process now let us try to understand how this very security mechanism prevents us from pushing to our branches in the earlier case let us say that we make the first iteration of code changes at which point the local feature branch commit tree looks like this and the remote feature branch commit tree looks like this when we run the first push command git verifies that the history of commits in the local is the same as that of the remote and lets us to make the push then we make the second iteration of changes and run the git commit command with the amend flag this does something interesting this command does not create a new commit but edits the one on top of the git commit tree which in this case is the green one as it edits that commit the hash that is linked to that commit also gets changed and we now try to push this to the remote which at this point of time looks something like this notice that this time around the local history of commits does not have the one with a hash starting with 32 because that is what got amended into the one with the hash starting with 11. but git does not have any way of knowing this and hence it complains it tells you that the current branch is behind its counterpart because it does not have this commit with the hash starting with 32 so simple yet so intriguing drop a like on this video if that amazed you too let us now look at ways to solve this like with most get errors it provides us with a solution which is to do a git pull in order to get all the changes from the remote onto the local branch that is a legitimate solution even if it's not the most suitable in this case what a get pull command would do is create a merge commit from the remote feature branch into the local branch signifying to get that all the changes that were present on the remote branch have now been merged into the local branch if we then run the git push command it will go through successfully because git now does not see any dangers of losing any commits but there is a problem with this solution it creates an unnecessary merge commit in the local feature branch for no reason at all the merge commit makes sense when a branch is merged into a different branch after a successful code review but this is just a merge commit from the remote of the same branch into its local this can definitely be avoided and the way to do that is to use the force flag yes the push command with force could be dangerous and can lead to loss of git history when used without caution but there is a reason why the command exists let us go back to the point in time when we have amended the commit after the review comments and now want to push the changes as we are the only ones working on this branch and we are 100 sure that the changes in our local branch are the correct ones we can use the git push with force command what this command does is that it trusts you completely and thus replaces the git history in the remote feature branch with the one that got pushed from the local feature branch this time it does not check for the common git history in order to allow the push it just executes the commit tree replacement and now the pull request of this feature branch against the main can continue as expected until some more questions are raised about the awesome code that we wrote hope you learned something new about git today see you in the next one
Info
Channel: Code Sketched
Views: 11,496
Rating: undefined out of 5
Keywords: getting started with git, git, git basics, git branch, git checkout, git cli, git clone, git commands, git for beginners, git fundamentals, git merge, git push, git repo, git status, git tutorial, git tutorial for beginners, git workflow, learn git in 15 minutes, understand git, github, git and github, learn git, version control, what is git, github tutorial for beginners, git failed to push some refs to
Id: iBP5GnV0tac
Channel Id: undefined
Length: 4min 13sec (253 seconds)
Published: Fri Mar 18 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.