Using Git in the Real Life for Beginners 2

Tue, Jun 16, 2020 2-minute read

Here I am going to focus on git stash, very useful commend.

3.Bugs need to fix()

😼😼😼😼

Suddenly the team need you to fix a urgent bug on the master branch. But it’s not yet to commit your local modifications.

Then you try to git checkout master, get an error said

Your local changes to the following files would be overwritten by checkout, Please commit your changes or stash them before you switch branches.

That’s because it’s a team work, the master branch get samething new that I d

Use git stash, very handy, you will love it.

The “git stash” command can help you to (temporarily but safely) store your uncommitted local changes。

below is the rules:

  • save modified codes in branch A
  • run git stash
  • git checkout branch B
  • fix bugs in branch B
  • commit and push to remote
  • git checkout A
  • git stash pop
$ git stash 
Saved working directory and index state WIP on master; d7435644 Feat: configure graphql endpoint

normally, git stash stores staged and uncommitted files, and ignore untracted files.

But you can use below option to deal with the untract files

git stash -u 
git stash --includ-untracked // stash is to stash the untracked files as well as the tracked ones.
git stash -a 或 git stash --all // stores staged and uncommitted files, also inclue the untracted files。

Now This is our file status:

$ git status -s
M  index.html
 M lib/simplegit.rb

If you already forget “M” means, right click git1 for a quite review.

$ git stash --keep-index // not only include all staged content in the stash being created,
Saved working directory and index state WIP on master: 1b65b17 added the index file
HEAD is now at 1b65b17 added the index file

$ git status -s // but simultaneously leave it in the index
M  index.html   

After you finish fixing the bugs, you can use “git stash appy” or “git stash pop” to go back the place you were working on.

  • “git stash appy” restore your stashed files and keep them
  • “git stash pop” restore and delete them from your stashed place

Use git stash pop to deledte those stashed filed is a good working habit.

Ok, It’s time for Git 3


Git1