"git checkout" Doesn't Update Files - visual-studio

my workflow with git is something like this:
1. pull --rebase from origin/master
2. create a new branch for a specific issue and make my changes on that branch
3. switch head back to master and then merge the new branch I've created to to master
from the documentation page, git checkout is suppose to
Updates files in the working tree to match the version in the index or the specified tree
however after I made changes to the new branch and checkout master and check the status using 'git status', the changed files is still present.
Worst of all, I have used the 'undo file changes' option in git extension for visual studio, now that even if I switch back to the branch I created using the 'checkout' command, I no longer see my changes. Is there anyway that I can redo those changes?

Before "checkout master", are you sure you are committing the files in the branch you are working on?
Make sure you commit the files in the branch you are working on. Otherwise, if git discards the changed files, you'll have lost them for ever.
If you do not want to commit the files yet, try looking at git stash. Git stash allows you to temporarily save your changes, without committing them to any branch.

Related

How do I merge current master branch into my branch visual studio/azure devops?

So I have the master branch, and another branch, lets call it 'branch2', that I am working on that is based off of the master branch. There are important changes in the current master branch that were made from someone else that I need to merge into branch2. Just wondering how to do this within visual studio or azure devops.
In VS,
open the git pane.
View the branches sub-pane.
Expand the'origin' branch
right click the master branch (under origin)
select merge master branch in origin\branch 2
under local branches right branch 2 pull latest
done
Download gitbash for windows, or install git on linux. I recommend command line rather than trying to use the VS or DevOps GUI. I prefer command line simply because I have better control over my commands and are less likely to mess things up. When things do get messed up, I can review the commands I used that led me there. Be sure to be in the root folder of your git repo when you use the commands. These are the three commands you would use:
git checkout branch2
git fetch
git pull origin master
What this is doing, you want to be in your feature branch, "Branch 2". Then you do a fetch, which pulls changes down from the server but doesn't apply them. Then when you pull origin master, you are pulling the master branch changes into your current branch.

Can tortoisegit be configured to automatically merge and it that fails to produce a conflict file to use for merge

We have been using tortoise svn for quite a while and loved it.
When we did an update it would automatically merge and if there were conflicts
it would produce a conflict file and I could use the merge tool on it.
With tortoisegit, if someone has been in the file it does not even attempt to
merge just produces message similar to "Cant". Is this a problem with git or
tortoisegit. We used tortoisegit because of how easy tortoise svn was to use and did not get in the way. Whereas with tortoise git we get the "CANT" and many dialogs to
click on when we do anything.
How can I merge in tortoisegit if there is a conflict, and is there someway to tell
it to produce conflict files.
I will try to summarize how Git synchronization with remote repository works.
You Commit your changes into a local branch.
You want to Push your changes into remote repository. If Git finds out there are new changes in remote tracking branch, it will not allow the Push and will say "you have to do Pull first". (This is a restriction in the Git itself, not only in TortoiseGit.)
As per instructions you do Pull. New merge commit combining yours and others' changes has to be created. If there is no conflict, merge commit is created automatically. If there is a conflict, TortoiseGit force you to resolve it and Commit changes manually.
Finally, you do Push.
I think the main difference from SVN schema is the creation of merge commit, but I don't see this as a disadvantage at all.
A good way to avoid merge commits is to always do Pull before committing so you commit to the newest version of the branch. However if you have working tree changes in a file which needs to be updated during Pull, Git will say Your local changes to the following files would be overwritten by merge and Please commit your changes or stash them before you merge. In this case you should do Stash changes, repeat Pull and finally do Stash Pop.
You may call all these operations from TortoiseGit context menu or you may try to use Git Sync dialog.

xcode refuses to allow switching branches when there are uncommitted changes

I'm using Xcode 9 and in my project I started to work on a completely different feature but unfortunately I've forgotten to create a new branch before doing that. So, now I have a lot of changes which don't refer to an old branch, so I create a new one, but I can't change my current branch without committing them.
Is there some way to change the branch, while having uncommitted changes, or I don't have other choice but to commit them to the current branch?
Don't know Xcode 9, but from the command line, you can use the "git stash" command.
"git stash" allows you to store your changes and re-apply them latter. This way you can store them and remove them from the current branch, switch the branch and then re-apply them on the new branch:
git stash
git checkout new_branch
git stash apply

How can I revert a file in a commit or pull request to the state it was in when I created the branch?

Hypothetical:
I'm working on a project and let's say I create a branch table-settings off of master. I commit changes to three files:
plate.swift
bowl.swift
fork.swift
Someone comments on my pull request and says, "We don't need those changes to plate.swift right now.
Either on github, the command line, or a source control desktop app, is there a way that I can revert all changes to a single file in a commit or pull request, back to the state it was in when I created the branch?
What makes this different from "How do I revert a file to the previous commit", is that I'm wondering if I can revert a file that's been edited and committed multiple times, to the state that it was at when I created the feature branch it's on.
If you want to make it to a state that's in master then do following from your branch
git checkout master -- /path/to/plate.swift
above command will make it to a state, that's in master. Then commit and push normally. e.g.
git commit -m 'reverting not-required changes'
git push;
Thanks!
The duplicate link in the comments basically has you covered, but in this situation I might be inclined to do the following:
git checkout HEAD~1 path/to/plate.swift
This will reset plate.swift to the version one revision prior, when you created the branch. Then you can amend the commit via
git commit --amend
Now your commit will contain all the previous changes minus the changes to plate.swift, which file will appear unchanged. You would then have to force push using this:
git push --force origin table-settings
The reason the force push is needed is because we have rewritten the last commit in the branch. But this approach is possibly nice for the reviewers, who will see a feature branch which looks very similar as when they commented.
If your feature branch has already been checked out by reviewers, then a safer option would be to just do a normal commit:
git commit -m 'reverted Plate.swift'
But I have found that amending can be useful when you end up having to make a number of tweaks to a pull request but you don't want to uglify your branch's history with many commits.

What is the right way to try to make a revision to an existing commit of a merged feature branch?

I am still learning the features of using TortoiseGit and the concept of version control.
I have read the this article and it is not clear to me if I need to follow that procedure. It describes patching.
So, I have this feature branch that has now been merged into the master as indicated. I want to have a go at making a revision to the application with respects to one of the commits (highlighted). So I would like to try out an idea to do things differently and implement it if I am happy.
What is the correct way to do this? I am a sole developer and am not in collaboration with others for my project.
Thank you for your advice.
There will always be times when you realize that a commit is not perfect or introduced an error.
You have several options:
1) Fix the issue and commit the fix on master (or in a bugfix/feature branch). You should do this, especially, if the "faulty" commit is some longer time ago.
2) Option 1 might have the disadvantage that you don't know that this fix belogs to a recent feature branch. Here, you could switch to the feature branch again (if you already deleted the branch, re-create it on the latest commit before the merge), commit the fix there and merge the feature branch again to master. This way you can see in the log that this fix also belongs to the feature branch.
3) The third option would be to rewrite your history - the is especially discouraged if you develop with other users and the "faulty" commit is already pushed or older. For rewriting history, go to the log dialog and open the context menu on the commit just before the faulty commit and select "Rebase ... onto this". Then the rebase dialog opens, check "Preserve merges" and "Force" there - then all commits starting with the "faulty" one should be displayed. Now, click on the "faulty" commit and mark it as "Edit" and then start the rebase. - Now your working tree is put back into that old state. Do your changes on the working tree, check "Edit/Split commit" and click on "Amend" on the rebase dialog. - After that all other remaining commits are re-applied and the fix is included in your history.
PS: The article about patches and pull requests is about sharing changes with other developers.
If you are sole developer and you are absolutely sure that no external party has checked out your master you can do the following:
Create backup of your master branch by branching off from the top of current master.
Looks like you also have changes committed to your master branch. Create a feature branch on from the master as well. At this point master-backup branch and your new-branch should be identical
Reset your master to the state before your merged in your feature branch. In this case this is 'Removed MWB files....'
Checkout your feature branch you want to change
Do git rebase -i master
Edit the line that has your commit that you want to change. Change 'pick' to 'e' and save the file to kick off interactive rebase
When rebase stops at the commit you want to change do git reset HEAD~ this will put your working directory into the state before you did your commit. With all changes being on the index.
Make necessary changes to your source and commit
Do git rebase continue
Once interactive rebase finishes. Checkout master and merge your new feature branch into int.
Checkout your next branch 'lingala' translation.
Rebase it on top of new master, and then merge
Checkout your feature/new-branch, and rebase on top of new master then merge
You done. Push everything to your remote. Note that you have to use 'git push --force'
If you have code checkout on other machines, make sure to do the following
git fetch ;
git checkout master ;
git reset --hard origin/master
Note the last step will destroy any local changes you have on your master branch and any uncommitted files. If you want to preserve them either stash them or create a temp local branch that you can later rebase on top of new master
If you don't want to preserve your history tree as is just do the following steps:
Start interactive rebase git rebase -i 'xxxxxxx' where xxxxxx is commit id of the commit with the comment 'Removed MWB files....'
In your rebase sheet edit the line of the commit that you want to change by replacing 'pick' with 'edit'
When rebase stops. Do 'git reset HEAD~' this will put your working directory into the state that was right before you committed these changes.
Make changes to your files, commit them.
Do git rebase --continue
Push your master branch with --force option
PROFIT
BEFORE YOU DO THIS. MAKE SURE TO CREATE A BACKUP COPY OF YOUR MASTER BRANCH
If something goes wrong you can restore master branch from the backup by doing
git checkout master
git reset --hard backup-master
git push --force

Resources