When git tfs could not create a merge commit, how to correct it - git-tfs

When git tfs could not create a merge commit, it says
warning: this changeset 7504 is a merge changeset. But it can't have been managed accordingly because one of the parent changeset 7494 is not present in the repository! If you want to do it, fetch the branch containing this changeset before retrying...
As per the documentation, Note: if you see a warning, you could correct that by reseting the tfs remote to a previous commit. Then fetch the merged branch and retry to fetch the branch.
Can anyone please elaborate on reseting the tfs remote to a previous commit. Though, I've now fetched the merged branch, I don't understand how do I reset it to previous commit to the failed one. I'm not sure but do I have to git checkout <hash of the previous commit>?

Yes, now git-tfs try to create merge commit when it encounter merge changesets (now that it has a satisfying branch support).
This message is just a warning message and when you see it, you have 2 options...
The first one is to do nothing because you know that it was, for exemple, an old feature branch that you will never work on that and MORE IMPORTANT that in the future you will never merge again in your parent branch.
The second one is if you really want to have this merge commit. Because you want a good history or MORE IMPORTANT because you still work on this branch and will have to merge it in the parent branch.
To do that, you will have to reset your tfs remote (because in fact the commit has already been created --to keep compatibility with how git-tfs works in previous version and for those that can't work with branches--).
To reset your remote, you must use reset-remote command.
Then intitialized the branch that is merged in the parent branch with branch --init.
Reset also the local branch to the tfs remote (due to an internal git-tfs optimization).
And fetch again the parent branch. Now that the merged branch exist and is fetch, git-tfs will find the parent changeset from the merged branch and you will have a beautiful merge commit in your git repository ;)
So, If you did this earlier
git tfs clone https://CompanyName.visualstudio.com/DefaultCollection "$/CompanyName/Main" KfGitMain --workspace="C:\TFS\Main"
cd GitMain
git tfs branch --init "$/CompanyName/Release/20140121.1" live20140121.1
git tfs branch --init "$/CompanyName/Release/20140121.1-hotfix" hotfix20140121.1
and if you received the warning for all the three due to code merges to and from each other, so you will have to
git checkout hotfix
git tfs reset-remote 5fb83335b8dfc6fbb96e0a54a48dc06c506e3277 ## previous commit of the first failed commit
git reset --hard tfs/hotfix
git tfs pull -i hotfix
git checkout live
git tfs reset-remote eba62a1446f3f81676d051336ca254fe54c37d74
git reset --hard tfs/live
git tfs pull -i live
git checkout master
git tfs reset-remote 72727737ec52f9b96d22d343770186a342c8b166
git reset --hard tfs/default
git tfs pull -i default
Note: if you don't have too many branches and/or strange tfs history, all that could be avoided using git clone with the option --with-branches that will init and fetch all the branches taking care of the merge changesets

Related

Edit and create more text files in git with command line [duplicate]

I am working on a git repository with a master branch and another the topic branch. I have switched to topic branch and modified a file. Now, if I switched to the master branch, that same file is shown as modified.
For example:
git status in git-build branch:
# On branch git-build
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: cvsup_current
#
Switch to master branch
[root#redbull builder_scripts (git-build)]# git co master
M builder_scripts/cvsup_current
Switched to branch "master"
git status in master branch
[root#redbull builder_scripts (master)]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: cvsup_current
#
Why is that the file is shown as modified in the master branch even though it was modified in git-build branch?
My understanding was that the branches are independent of each other and when I change from one branch to another the changes do not "spill over" from one branch to another. So I am obviously missing something here.
Has anyone got a clue stick?
Why is that the file is shown as modified in master branch even though it was modified in git-build branch?
The key to remember is that the file was not modified in the git-build branch. It was only modified in your working copy.
Only when you commit are the changes put back into whichever branch you have checked out
If you want to temporarily store your changes to one branch while you go off to do work on another, you can use the git stash command. It's one of the amazing little unsung perks of using git. Example workflow:
git stash #work saved
git checkout master
#edit files
git commit
git checkout git-build
git stash apply #restore earlier work
git stash stores a stack of changes, so you can safely store multiple checkpoints. You can also give them names/descriptions. Full usage info here.
This is the default behaviour of git.
You can use -f flag to checkout to do "clean checkout" if you like.
The modified files are not put in the repository until you add and commit them. If you switch back to your topic branch and commit the file, then it won't appear on the master branch.
It is not like git branches are dependent on each other but also they
do not have a complete code base separately for each branch either.
For each commit, Git stores an object that contains a pointer to the
changes. So each branch points to its own latest commit and HEAD points to the branch currently you are in.
When you switch the branch, the HEAD pointer points to that
particular commit of the branch. So if there are modified files, the
default behavior is to copy over them.
You can do the following things to overcome this issue.
Use -f option to ignore the changes.
If you want to save the changes:
Commit the changes locally in the same branch and then switch the
branch.
Use git stash, switch the branch, do your work, switch back to the
original branch and do git stash apply.
In my experience this "spillover" problem arises because git does not protect "untracked" files from git checkout (only tracked but uncommitted files would be protected, i.e. user would be forced to git commit them before a git checkout is allowed to another branch).
If you switch back to the original branch that created these files (and "untracks" them), these files become "untracked" again and can be git add'ed or git rm'ed.
For me it looks like a bug: even "untracked" files should be protected from git checkout.

VSTS rollback if any one task fails in the build process

I want to rollback to the previous commit state of that particular task if any of the task fails in the build process.
Please help me.
Thanks in advance
If you are using Git as VCS
There are two options fir you situation.
Option 1: Use Pull Request validation (recommend)
Instead of commit change on the branch (assume it's master branch) directly, you can apply changes via pull request. And you can use pull request validation before merging changes into master branch.
You can create a branch (such as dev branch) from master branch, and commit and push changes to dev branch. Then add branch policy (build validation) on master branch. Then if there has pull request to merge changes into master branch, the PR validation will be triggered.
Option 2: Reset the commit and force push to VSTS git repo
If you want to commit changes on the branch (master) directly instead of using pull request, you can reset the commit by below commands:
git checkout master #if HEAD is not point to master branch
git reset --hard HEAD~
git push -f
Now master branch is rollback to the pervious commit.
If you are using TFVC as VCS
You can use Gated Checkin which will validate the build before checkin.

Reinitializing a git repository

I have an xcode project on my desktop in a directory that originally had a git repository with a tracking branch that tracked a remote branch on github. The remote branch has some 84 commits and is 2 commits ahead of the master branch of the project I'm adding features to. I changed the name of the folder/directory on my desktop. I'm not 100% sure if this is the reason why but when I go to git status I get: fatal: Not a git repository (or any of the parent directories): .git. My plan is to simply git Init, add the remote branch and create a new tracking branch and than commit locally to that branch and than push to the remote branch. However, I'm a git beginner and I'm not sure if this is the proper way to go about it. I'm very weary of losing any commit history or accidentally breaking something. Is the method I outlined a good way of rectifying this loss of the git repo?
If you have all your code updated in remote repo then your local .git is deleted. You don't need to re-init your local repo. Rather just clone it.
git clone remote_repo
If you don't have any commit in remote repo, Simply follow
git init
git add all_local_files
If you have updated remote repo than some commits made in local but not pushed and you lost .git. Simply clone remote. Add all files in a single commit
git clone
git add all_local_files
Looks like you messed up your git repository, but not the code / contents.
One way to restore and keep local changes ( if any ) would be:
Clone another copy of your repo from github.
Copy all modified files to the new repo, omitting removed ones: rsync -duztv /old/local/repo/ /new/local/repo
git status to see what the situation is.

revert git back initial condition

I was trying to add a repository to my xcode project, but instead of adding my project files to the repository, git removed my entire project. I think I can use 'git revert' option to revert the changes, but how do I know where to revert to?
Get the list of commits using
git log this will list out all the commits with its hash, to revert the commit use
git revert (part of the hash)
for example if the commit hash is adghgd356484fghfbht76, do
git revert adghgd3564
If you have not pushed your changes to remote, you can run git reset --hard which will undo all your local changes and you will loose those changes from the history as well

Using git-tfs fetch to get latest causes a merge conflict

I have an issue attempting to pull my latest changes from TFS and adding them to a BitBucket repo. I have successfully pushed my branches into BitBucket, but when I attempt to get any new checkins from TFS I always get a merge conflict.
Steps:
git tfs clone http://TFS-ADDRESS/Collection $/Project/Name/Branch
git tfs init-branch $/Project/Name/NewBranch NewBranchName
git push -u origin --all
At this point my BitBukcet repo has all my files and history from TFS.
I then did a checking into me $/Project/Name/NewBranch on TFS.
Now I want to get the latest changesets from TFS and add/commit them into the BitBucket repository Note: At this point, no other branch in TFS or BitBucket has changed. The only change is my single check-in into TFS
When I do the following on my NewBranch, I get a merge conflict:
git tfs pull
Auto-merging Source/build.version
CONFLICT (content): Merge conflict in Source/build.version
As far as I can tell, it is trying to merge changes from my "master" to my "NewBranch", even though NewBranch is the latest code.
The ptfs pull command is pulling the last checgeset down, but then tries to do the merge. If I use
git tfs fetch --all
I see the changeset listed, but I cannot commit/push it to the BitBucket repository. I cannot find a reference online to say what I am doing wrong, or what steps I need to take (I am new to Git). Can anyone help?
I eventually want to automate this so any check-ins in TFS can be fetched and added to the BitBucket repository (until we eventually move all developers to BitBucket)
Thanks
EDIT
I am trying to take changes from TFS to BitBucket. Nothing is getting added to TFS from BitBucket.
All work is done on a branch (see below). Users are working in TFS. Users commit their changes there. NOTE: No-one is making edits to the BitBucket repo. It is a replica of TFS.
Once a day, I want to pull the latest changesets for each TFS branch down to my local git folder and them push them into BitBucket. The "git tfs fetch" retrieves the changesets:
D:\development\workspaces\git\anothertry>git tfs fetch --all
C83419 = 3abdaedf571369dce15f74a52697d86069f87d6d
but "git status" tells me there is nothing to commit
D:\development\workspaces\git\anothertry>git status
# On branch Feature
nothing to commit, working directory clean
And when I try to push this changeset into BitBucket, git tells me there is nothing to commit.
Branches
Master---o---------------o----o--o-o--o-------
\ / / / / /
Feature-----o----o--o-o--o
I seem to have found the answer after using Git Extensions.
It seems I need to do a fetch
git tfs fetch --all
This will get me all changesets from TFS for my trunk and any branches I have mapped (init-branch)
D:\development\workspaces\git\anothertry>git tfs fetch --all
C83454 = f65ba8d0cc7b40f42148ba2c06d0da8b7e968c4d
C83455 = 6c25feb6e74a09d0d3d84344002e7546044900ab
C83459 = 91c4450d702d5a97e862aedd7ad1b5af3ff2c375
C83460 = 5cacaba5bfe4197c1bf9fab498f51c1ba435e6ea
Next I need to perform a merge
git merge <guid>
NOTE: The is for the latest changeset (in this case 5cacaba...)
Updating 6c25feb..5cacaba
Fast-forward
Source/Common/w32threads.h | 1238 ++---
Source/xxxxxxx_Service/ServerAgent.cpp | 3638 ++++++-------
Source/xxxxxxx_Service/ServerAgent.h | 449 +-
Source/xxxxxxx_Service/User.cpp | 9073 ++++++++++++++++----------------
Source/xxxxxxx_Service/User.h | 647 +--
Source/build.version | 2 +-
6 files changed, 7562 insertions(+), 7485 deletions(-)
And, finally, push my changes to BitBucket
git push --all
So, it seems I was missing the git merge call and the parameters to pass to it.
Cheers
I don't exactly understand what you do bad but there is somethings that you should now in using git-tfs...
Working with a TFS branch
I see that you created a branch (step 2) and commit in that branch.
When you have checkouted your branch 'NewBranchName', you should checkin with the command :
git tfs rcheckin -i NewBranchName
and pull modifications with :
git tfs pull -i NewBranchName
or (with rebase)
git tfs pull -r -i NewBranchName
If you don't specify the -i option with the name of the TFS remote, you will check in your commit in the main branch (here, $/Project/Name/Branch ) :(
Checkin your commits on TFS
I give you above the command to check in your commits. But you should know that, for some reasons, when you check in a git commit in TFS, a new changeset in TFS is created (that's what we want ;) ) and after a new git commit is created. It's nearly the same that the original but with some informations (that you can see in the git comment).
Due to this fact, depending on what you do (and I don't exactly understand what), you could have problems because git try to merge commit with exactly the same modifications at the same place, so fail to merge automaticaly.
In fact, I think there is something wrong in your workflow because you shouldn't encounter this problem. Try to find the problem in your workflow.
I hope it will help.
Feel free to see the git-tfs wiki : https://github.com/git-tfs/git-tfs/wiki
I hope there is quite useful informations (partly did it, with love ;) )
PS : To help you better, can you tell me which command you use to checkin your commits?
checkin, checkintoo, rcheckin? rcheckin is heavily advised!
What I do to automate your workflow :
(here 'bitbucket' is the supposed name of your remote bitbucket repository)
=> in your master branch :
//Do your job & commit with git & ...
//rebase your work on TFS work
git tfs pull -r
//rcheckin with git-tfs
git tfs rcheckin
//commit in bitbucket repo
git push bitbucket master
=> in your 'NewBranchName' branch :
//Do your job & commit with git & ...
//rebase your work on TFS work
git tfs pull -r -i NewBranchName
//rcheckin with git-tfs
git tfs rcheckin -i NewBranchName
//commit in bitbucket repo
git push bitbucket NewBranchName
PS2 : Perhaps, you should have to stash your modifications if you have uncommited changes in your working directory before doing pull -r and rcheckin...
EDIT : You edit, I edit ;)
Before, I suppose that you have defined a remote for your bitbucket repository :
git remote add bitbucket https://bitbucket.org/yout_login/your_project.git
You have just to :
//checkout your branch
git checkout NewBranchName
//fetch the TFS changesets and merge your branch into
git tfs pull -r -i NewBranchName
//push your commit on bitbucket
git push bitbucket NewBranchName
And to explain a little what you are doing (and not understand totally :( )...
With "git tfs fetch --all", you are fetching the TFS changeset of all the branches but not merging (or rebasing) with your local branch. So they doesn't appear in your local branch.
You could see the DAG with the command to better understand :
git log --oneline --graph --decorate
The 'git tfs pull' given above merge the fetched commit with your branch so that will be good!
"git status" tells me there is nothing to commit
This command show you the files that are modified in the working directory. So, that's normal that you see no changes. That does say that your commit was not pulled!
And when I try to push this changeset into BitBucket, git tells me there is nothing to commit.
You should have! Are you sure to have define your remote toward your bitbucket repository? (see above)
Which command you use? What git tell you?
The git tfs fetch command doesn't update your local git repository, that's why you should do git merge after that.
To do the same in one command simply do pull from TFS remotes
git tfs pull -r
This should update local git repository with all new updates from TFS server
At this point you just need to push updates to git server:
git push -u origin --all

Resources