Quello che hai qui è una situazione in cui hai alcuni file non tracciati che sono in conflitto con i file tracciati in un altro ramo. Ti piacerebbe passare a quel ramo, ma lo checkout
non ti lascerà.
La soluzione "primo livello" in Git è di promuovere questi file non monitorate all'indice:
git add folder
Ora non è ancora possibile controllare al ramo. Tuttavia, si sono presentati con una nuova possibilità: si può git stash save
le modifiche:
git stash save
Ora è possibile passare al ramo, e fare un git stash pop
. A questo punto puoi gestire conflitti di fusione, se presenti, seguiti da uno git reset
e il gioco è fatto.
[Aggiornamento: il git add
non è necessario perché git stash
ha un opzione per includere i file non ancora!]
Lavoriamo attraverso un esempio completo, che coinvolge un singolo file chiamato topicfile
che esiste solo in un ramo, e viene creato un file di lavoro mentre sul master
, ma con diversi contenuti:
~$ mkdir gittest
~$ cd gittest/
~/gittest$ git init
Initialized empty Git repository in /home/kaz/gittest/.git/
~/gittest$ touch emptyfile
~/gittest$ git add emptyfile
~/gittest$ git commit -m "starting repo"
[master (root-commit) 75ea7cd] starting repo
0 files changed
create mode 100644 emptyfile
~/gittest$ git branch
* master
~/gittest$ git checkout -b topic
Switched to a new branch 'topic'
~/gittest$ cat > topicfile
a
b
c
d
e
~/gittest$ git add topicfile
~/gittest$ git commit -m "topicfile"
[topic 875efc5] topicfile
1 file changed, 5 insertions(+)
create mode 100644 topicfile
~/gittest$ git checkout master
Switched to branch 'master'
~/gittest$ ls
emptyfile
~/gittest$ cat > topicfile
@
a
b
c
d
e
f
g
h
~/gittest$ git add topicfile
~/gittest$ git stash save topicfile
Saved working directory and index state On master: topicfile
HEAD is now at 75ea7cd starting repo
~/gittest$ git checkout topic
Switched to branch 'topic'
~/gittest$ git stash pop
Auto-merging topicfile
CONFLICT (add/add): Merge conflict in topicfile
~/gittest$ cat topicfile
<<<<<<< Updated upstream
=======
@
>>>>>>> Stashed changes
a
b
c
d
e
<<<<<<< Updated upstream
=======
f
g
h
>>>>>>> Stashed changes
~/gittest$ cat > topicfile # resolve in favor of stashed changes:
@
a
b
c
d
e
f
g
h
~/gittest$ git add topicfile
~/gittest$ git reset
Unstaged changes after reset:
M topicfile
~/gittest$ git diff
diff --git a/topicfile b/topicfile
index 9405325..bea0ebb 100644
--- a/topicfile
+++ b/topicfile
@@ -1,5 +1,9 @@
[email protected]
a
b
c
d
e
+f
+g
+h
a questo punto siamo in grado di commettere i nostri topicfile
modifiche al ramo topic
, e il file non è ancora tracciato su master
.
Perché c'erano conflitti in git stash pop
, lo stash esiste ancora. Possiamo pulirlo con git stash drop
.
L'alternativa a tutto questo è non solo di git add
i file non tracciati nell'indice, ma di git commit
per eseguire un commit corretto. Quindi possiamo selezionare il commit nel ramo. Se non vogliamo che quei file siano rintracciati su master
, va bene: possiamo più tardi il git reset --hard HEAD^
sul master per eliminare quel commit.
Non ho pensato di usare 'git stash' per questo. Grazie per quello. – risto