Search code examples
gitgit-branchgit-stash

How to take backup of the current state (including untracked files) so that if i want i can check them later


I am currently working on a commit

I have some modified files and also untracked files

I want to keep a backup of the current state for future reference.

I have read and found mainly using stash.

I want whatever files at the current stage should all be saved to the stash. (including untracked and also not to delete gitignore files)

I found git stash is being used. It will save the current state to stash and will have a clean working tree after stashing

Which of the following way to use git stash.

$ git stash save "my_stash" 

$ git git stash --include-untracked save "my_stash"

$ git stash --all save "my_stash"

then we get back the stash again using

$ git apply

Also if possible i want to know how to do the same thing by creating a branch instead of stash.


Solution

  • This is what i was looking for

    I am currently on the masters branch, I am in between a work with some unstaged files and also with some untracked files. I want to save my current state into a new branch just as a backup/reference for future.

    So i did stash push --include-untracked in my current state and then create and checkout to a new branch and stash apply there and stage all files by add -A and then commit changes. Again come back to master branch and again do stash apply there.

    there

    $ git status
    
    On branch master
    
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git checkout -- <file>..." to discard changes in working directory)
    
        modified:   filename1
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    
        filename2
    
    no changes added to commit (use "git add" and/or "git commit -a")
    
    $ git stash push --include-untracked
    
    One stash is run
    
    $ git status
    On branch master
    Your branch is up to date with 'origin/master'.
    
    nothing to commit, working tree clean
    
    $ git checkout -b backup_branch
    Switched to a new branch 'backup_branch'
    
    $ git stash apply
    On branch backup_branch
    
    
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git checkout -- <file>..." to discard changes in working directory)
    
        modified:   filename1
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    
        filename2
    
    
    $ git status
    On branch backup_branch
    
    
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git checkout -- <file>..." to discard changes in working directory)
    
        modified:   filename1
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    
        filename2
    
    $ git add -A
    
    $ git commit -m "Saved for backup"
    
    $ git checkout master
    Switched to branch 'master'
    Your branch is up to date with 'origin/master'.
    
    $ git stash apply
    On branch origin/master
    
    
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git checkout -- <file>..." to discard changes in working directory)
    
        modified:   filename1
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    
        filename2
    
    $ git status
    On branch origin/master
    
    
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git checkout -- <file>..." to discard changes in working directory)
    
        modified:   filename1
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    
        filename2
    

    Now i am back with the same working dir. With a backup of my current state in a new branch backup_branch