18.6. Git Tips¶
Now that we know about branches, we should definitely use them often. Keeping
the core, working code in main
and growing that code in branches is a very
strong technique.
This page explores some details that often come up as we work with branches.
18.6.1. Git Stash¶
It happens to the best of us. We get a great idea for our project, and we dive into creating the new code. Things are going well, until we realize that we put our brilliant idea in the wrong branch of the repo! Lines and lines of new code, all in the wrong place.
Of course Git offers us a way to remove the new work from the current branch
and move it into another. The command for this is called git stash
.
Warning
git stash
will only save our butts if we have NOT committed the new
changes to the repo yet. This is why we should always use git status
to
check things out before using the add
and commit
commands.
18.6.1.1. Try It!¶
Imagine that you want to add a few more lines to the text file in the
make-conflict
branch.
Return to the
main
branch in thegit_practice
repo.Add new lines to
conflict_demo.txt
and save the file.Type
git status
in the terminal. The first line of the output tells us that we’re in the wrong branch. We want to be inmake-conflict
, notmain
!$ git status On branch main 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: conflict_demo.txt
Enter
git stash
in the terminal and notice how the changes made in the text file disappear. Those changes are stored by Git, but they are no longer inmain
. Enteringgit status
again shows a clean branch.Use
git checkout
to move into themake-conflict
branch. Follow this with thegit stash pop
command.$ git checkout make-conflict Switched to branch 'make-conflict' $ git stash pop On branch make-conflict 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: conflict_demo.txt no changes added to commit (use "git add" and/or "git commit -a") Dropped refs/stash@{0} (dedea22f5c66ef992103fcc82d7eaed82d4463f5)
Aha! The changes we made in
main
appear in themake-conflict
branch. We can usestatus/add/commit
to add them to the repository.
stash
is similar to the append
list method in Python. We don’t need to
know the complete details for how it works, but we can imagine that stash
takes a snapshot of the branch. It then returns the branch to the state of the
last commit.
stash pop
takes the snapshot and applies it to the current branch.
18.6.2. Deleting a Branch¶
Imagine that we finish our work in a branch and successfully merge it into
main
. Now that the code exists in main
, we might not need the old
branch anymore. To delete a branch, the terminal syntax is:
git branch -d branch-name
If the branch contains changes that haven’t been merged yet, Git gives us an error message and keeps the branch intact. Git will also provide a helpful clue about how to remove the branch anyway, if we really want to go through with that action.
Warning
Deleting a local branch cannot be undone!
Example
Let’s delete hello-branch
from the git_practice
repository:
$ git branch
hello-branch
* make-conflict
main
$ git branch -d hello-branch
Deleted branch hello-branch (was d99e424).
$ git branch
* make-conflict
main
18.6.3. Git More Information¶
If you would like to explore additional discussions, tutorials, and videos about Git, here are some resources you can explore on your own:
Git video tutorial (24 topics to choose from)