Git Worksheet - itcig/git-sandbox GitHub Wiki
Review the Git Best Practices page before proceeding with the worksheet ✍️
- Decided what feature you would like to add to the sandbox (this can be as complex or simple as you'd like)
- Create a feature branch using the format outlined in the Branches page
- Make some changes
- Use
git add [file name]
to stage the changes- Commit the change(s) grouping them into commits using the standards outlined on the Commits page
- When ready to commit type
git commit
and press return - Write the commit message (title and body) utilizing the standards outlined in the Commit Messages section
- Repeat the prior three steps until all changes for your branch have been committed
- Push the changes to the remote repo
git push origin [your branch name]
- Create a Pull Request on Github from
[your branch name]
into themain
branch - Use the format outline in the Pull Requests section to create a name and body for the PR's message
- Scroll down and click
Squash and merge
(if this is not an option the Merge button will have a dropdown that can be clicked to select Squash and Merge) - Using the format listed on the Merge page create the commit message for the merge (this will be the only commit that will be seen on the
main
branch) - After the merge is completed see the commit log for the main branch, you should only see the merge commit listed
- Create a new release for your merged changes (this will be automated later on) see the Releases section
- View the release on Github
- Again, create a branch off of the main branch for a bug or feature
git checkout main
thengit checkout -b [my step two branch]
- As before make some changes
- Commit the changes, but include some typos, bad information, or incorrect formatting in the commit message(s)
- Make sure there are multiple commits
- Push the changes to Github
- Now from your branch create a new branch
git checkout -b [my step three branch]
, this will be used in Step Three - Push this duplicate branch to the remote/Github
git push origin [my step three branch]
- Switch back to the branch you made in step one
git checkout [my step two branch]
- Now that we've got some incorrect commit messages we can fix them using
git rebase
- Make sure you've got the most recent changes
git pull origin [your branch]
(you should, but this is generally a good idea to make sure another developer hasn't added changes) - Now lets find the last good commit type
git log
to display all the commits on the branch from most recent to oldest - Count how many commits from the begining of the list you'd like to change
- Press
q
to quit out of the git log - To correct the git log (essentially the branch's history) run
git rebase -i HEAD~[number of commits from the begining you need to go back]
- If you made 3 commits and all 3 need to be adjusted the command would be
git rebase -i HEAD~3
- Vim will open and you'll see a list of commits and information on how to use rebase as seen below:
pick 3888316 chore!: Gitignore, Composer, and index Setup pick 4340aef feat(js): Add alert for thing pick 2e3ef06 feat(js): Add alert for thing (#5) pick 72d4605 refactor: Add empty line to EOF # Rebase 7d65997..72d4605 onto 72d4605 (4 commands) # # Commands: # p, pick <commit> = use commit # r, reword <commit> = use commit, but edit the commit message # e, edit <commit> = use commit, but stop for amending # s, squash <commit> = use commit, but meld into previous commit # f, fixup <commit> = like "squash", but discard this commit's log message # x, exec <command> = run command (the rest of the line) using shell # b, break = stop here (continue rebase later with 'git rebase --continue') # d, drop <commit> = remove commit # l, label <label> = label current HEAD with a name # t, reset <label> = reset HEAD to a label # m, merge [-C <commit> | -c <commit>] <label> [# <oneline>] # . create a merge commit using the original merge commit's # . message (or the oneline, if no original merge commit was # . specified). Use -c <commit> to reword the commit message. # # These lines can be re-ordered; they are executed from top to bottom. # # If you remove a line here THAT COMMIT WILL BE LOST.
- If you made 3 commits and all 3 need to be adjusted the command would be
- Each commit will be prefaced with
pick
by default meaning the changes and message will be uneffected, you can adjust what will happen next by pressingi
to enter Vim's insert mode - Now you can change the preface from
pick
toreword
on the commit(s) you would like to change the message(s) on - Once you've updated the prefaces press the escape key to exit Vim's insert mode then type
:wq
to save and exit Vim - The rebasing will start and when it gets to the commit(s) set to reword Vim will appear, you can press
i
to enter Vim's insert mode and then edit the message - Once the messages have been adjusted press the escape key and enter
:wq
to save and exit Vim- If you get an error try running
git rebase --continue
to keep the rebase moving forward
- If you get an error try running
- Once the rebase is completed you will need to force push the branch to the remote, you can do this with the following command
git push origin [your branch] --force
- This WILL overwrite your previous commits, so only do this part if you're absolutly certain the local changes are correct
- After this completes you can check Github and you should see the new commit messages
- Checkout the duplicate branch you created in Step Two
git checkout [my step three branch]
- This time we'll remove all the commits you had previously made
- Run the following to start the rebase
git rebase -i [the number of commits to be removed]
- Just like before each commit will be prefaced with
pick
to change that pressi
to enter Vim's insert mode - Now you can change the preface from
pick
todrop
on each of the commits you had made in Step Two - Once you've updated the prefaces press the escape key to exit Vim's insert mode then type
:wq
to save and exit Vim - The rebasing will start, this time as no messages are being updated Vim should not open, instead once it's completed you should see a message similar to:
Successfully rebased and updated refs/heads/[my step three branch].
- You will once again need to force push the branch to the remote, you can do this with the following command
git push origin [your branch] --force
- After this completes you can check Github and you should see all the commits have been removed from the history/git log
- Again, create a branch. This time for bug that doesn't exist
- As before make some changes
- Commit the changes using the new format
- Push the changes to Github, then create a pull request once again using the new format
- Merge the PR from your bug branch into the main branch using the squash and merge format outlined below
- Now that we've got an incorrect PR on the main branch we can fix it using
git rebase
- Locally checkout the main branch
git checkout main
- Make sure you've got the most recent changes
git pull origin main
- Now lets find the commit we'd like to roll back to
git log
will display all the commits on the branch from most recent to oldest - Find the commit you'd like to roll back to and count how many commits came after it
- Assuming the PR commit you made is the only commit we'd like to remove, the number of commits above the last good commit should be 1
- Press
q
to quit out of the log - To roll back the main branch X commit(s) and leave the git log (essentially the branch's history) clean and readable run
git rebase -i HEAD~[number of commits to remove]
- From here the process is the same as Step Three above