0

So I am new to Git and I am not yet working with GitHub (Although I did sign up and set GitHub up)

Today I did something wrong, I think:

I created a new branch called "develop", and then moved to that branch. Next, I started a new .py file and wrote some code. Then I committed this new file's changes.

Next, I wanted to practice merging, so I moved to my 'master' branch, and tried git merge develop, but was told that there were conflicts in my workspace.xml file. This file, I believe, just tracks my IDEs workspace so it starts me where I left off every time I re-open.

My questions:

1.) Is this workflow of mine at fault? Should I have created the new .py file while on the master branch? What could I have done differently?

2.) How do I resolve this conflict? Git says it won't let me merge because the old workspace.xml file will be overwritten.

3.) How do I get rid of "Your branch is ahead of 'origin/master' by 1 commit"? This is probably because I added a new file and committed it on the 'develop'?

rocksNwaves
  • 5,331
  • 4
  • 38
  • 77
  • 1
    It seems you did everything correct, except that it seems you have committed files you probably shouldn't have, such as that workspace.xml file. Local cache files are not really useful to have in the repository and will lead to frequent and constant merge conflicts. Better to ignore them, check out .gitignore. Since git is already tracking these files, you have some minor cleanup ahead of you, be sure to search stack overflow for similar questions. – Lasse V. Karlsen Sep 02 '19 at 18:33
  • 1
    Your workflow is fine. git has this concept of [`gitignore`](https://git-scm.com/docs/gitignore) which lets you ignore files to be tracked by git. https://git-scm.com/docs/git-merge#_how_to_resolve_conflicts – Ashwani Sep 02 '19 at 18:34
  • Duplicate of https://stackoverflow.com/questions/24487784/how-to-commit-idea-workspace-xml-in-pycharm – matt Sep 02 '19 at 18:36
  • @matt The question you linked is asking "since 'workspace.xml' is not showing in my project, how can I commit it?", which is not at all what I asked. You should read the question that I asked, then the question that you linked. Then, you should delete your comment. – rocksNwaves Sep 02 '19 at 18:40
  • @LasseVågsætherKarlsen, googling now, and thanks for confirming my workflow. – rocksNwaves Sep 02 '19 at 18:41
  • Read the comments. You should have Ignored the xml file. – matt Sep 02 '19 at 18:42
  • @matt similar compliments do not make a duplicate question. Different questions often have the same answer. – rocksNwaves Sep 02 '19 at 18:46
  • Also a merge conflict is not an issue. Resolve it and move on. – matt Sep 02 '19 at 19:37
  • @matt While I agree, merge conflicts in local workspace files is a problem that is going to appear again and again and again, I'd advise to fix those problems. – Lasse V. Karlsen Sep 03 '19 at 07:12
  • 1
    A good read: https://git-scm.com/docs It's short, and it will answer to all your questions. – mfnx Sep 03 '19 at 11:32

1 Answers1

1

You will have probably done a variant of git add -A to add all your files, which includes that xml file. (The alternative mistakes is to add . - dot meaning all the files in the folder).

So you now have master and develop which have different xml files. So when you merge they are in conflict. And you want the mistake 'gone'.

First, on master, use your viewer (e.g. gitk or git gui) to confirm where you are (take a note of the commit sha1 incase you need to get it back). I don't think you will have a completed merge commit yet.

You should then 'git reset --hard' (always a bad idea from the internet ;-) to clean up the current state of your current branch . This will make the file system have exactly what was in the last completed (current) commit (that sha1). This should make the conflict go away.

If you have the git-gui (often the easiest way to see what's happening), then use the 'ammend commit' to remove that xml file from that last commit (it is now 'rewritten' as a new sha1 value).

Change to the 'develop' branch and repeat the amending (or read the manual for amending commits for the command line interpreter (cli) way of doing it).

You should now have both a master and develop branch that do not have that conflicting xml file, and you can try the merge.

-

Aside 1: as others have commented, look at .gitignore file options for avoiding these mistakes in the future. There are lots of example gitignore files on github, ready for the taking.

Aside 2: you said you aren't using Github (or other servers) yet, so you won't have any problems with the 'remotes' (i.e. copies of you repo stored else where). But if you had, then you would need to 'force' push your changes because you have 'rewritten' the history. This is perfectly normal and acceptable for personal work branches, but should be avoided for shared branches. There's a man page section about how to apologise for stuff like that ;-)

Philip Oakley
  • 13,333
  • 9
  • 48
  • 71
  • Thanks, I definitely took the .igitignore comments to heart, and have added the xml file. Also, I was able to remove that whole .idea/ folder that contained automatically created files. I used 'git rm -r --cached' which seemed to do the trick. Any reason not to have done that? – rocksNwaves Sep 04 '19 at 18:28
  • @rocksNwaves the choice of commands depends on where in history you want to apply the fix... Neither is 'better', but one will be best for you.. – Philip Oakley Sep 05 '19 at 20:11