简体   繁体   中英

Can't push or pull. (Git / SourceTree)

Very new to Git, and I am currently using SourceTree.

I have found myself in a very interesting situation that I cannot seem to get myself out of. I cannot seem to pull, or push code. I seem to be 2 commits ahead, and 2 commits behind,

as illustrated here. I am just trying to get current, so I can pull down all recent code, and continue to work. I am not sure how to proceed.

I don't want to lose the work for my commits, but I would if it really mattered because they are minor.

What is the easiest and most appropriate route to take in order to get the branch up to date and in sync with my local branch?

If you've no uncommitted changes

Just PULL first. SourceTree will ask you to solve merge conflicts if there are any. Then PUSH your changes.

If you've uncommitted changes

Git has a feature called stashing to save your unfinished changes. In SourceTree, do the following steps:

  1. Select your repository, click on Repository in the main menu and then on Stash Changes
  2. Give your stash a name like "uncommitted feature" and make sure the checkbox is unselected
  3. Now pull all the latest changes
  4. In the tree-menu left to your files you find a category Stashes , expand it and search for your created stash
  5. Right click on in and select "apply stash"

Resolve merge conflicts if there are any. Now you should be able to commit and push like you used to.

People probably have made updates into the project you've cloned (that explains the commits behind) and you've also made commits since you've cloned, and didn't pushed yet (and that explains the commits ahead). What you have to do is:

reseting your commits, but keeping the changes by using git reset --soft HEAD^

stash your changes by using git stash

and than,

pull things people updated on your head branch using git pull origin branchName

If you didn't stashed, you will probably get conflicts now, because the system is not smart enough to know whats the priority: what you have changed or what people from other branches changed. That might be not so easy to solve so i strongly recommend you to stash your changes before doing 'pull'.

Now, assuming that you've pulled new things into your branch,

get your updates back on it by doing: git stash apply

Now you have no commits ahead but your changes are waiting to be committed,

so separately, make your commits and than use: git push origin branchName

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM