Change background image
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

[WIP] Setting up Git; how to squash

Discussion in 'Coding' started by Sabira, May 2, 2018.

Thread Status:
Not open for further replies.
  1. Sabira

    Sabira Donator

    Hello and welcome to thread. This isn't a 'how to code', so concepts such as coding and compiling and so on will not be covered here. The web editor isn't sufficient for most use cases, so if you want to contribute, you'll need to set up Git on your computer.

    Git is a version control system and how we interact with the repository (repo) (https://github.com/Baystation12/Baystation12/)

    Some terminology for me to badly and incorrectly summarise:
    • Repository (repo) - the thing where the code is kept. The main repo is Baystation12/Baystation12, your fork will be yourname/Baystation12.
    • Commit - a thing containing information about what you changed and where, and a message describing those changes
    • Push - moving your commits to your repo
    • Pull - moving other people's commits to your local copy of the repo
    • Branch - different versions of the repo, allowing you to make different changes touching different files without having to commit them all at once
    • Pull request - a request that the main repo pull yours, merging your code changes into the master version
    Writing commit messages
    Commit messages should follow a few guidelines:
    1. The first line is the commit subject line and:
      1. should be less than 50 characters
      2. should be imperative, as if you're telling the computer what to do (Add new uniforms, Remove Security, Make changes to jobs file)
        1. An easy way to tell if you're doing it right is to enter it into the following sentence: If merged, this commit will your subject line
    2. Every following line makes up the body of the commit message, and should be less than 72 characters long each. This is where you can (optionally) describe why you're making your changes, but you can always do this later on GitHub.

    Always make your changes on a (fresh, up-to-date) branch.
    Always make your changes on a (fresh, up-to-date) branch.
    Always make your changes on a (fresh, up-to-date) branch.
    Always make your changes on a (fresh, up-to-date) branch.

    Always make your changes on a (fresh, up-to-date) branch.

    Setting up and using Git via the command line
    These instructions will be for Windows, since that's what I use. Commands will be in bold face.
    1. Go to https://git-scm.com/download and download the relevant version.
    2. Head over to https://github.com/ and create an account. It's helpful if your name there is similar to the one you use here.
    3. Install Git, leaving everything at their defaults.
    4. Open the command line (WIN-X -> Command Prompt)
    5. Enter the following commands to set up your Git identity:
      • git config --global user.name "your name" (can be your real name or username or whatever you want, but it will be viewable
      • git config --global user.email "[email protected]" (same deal as above, this should be a real email address, and probably the same as the one you registered on github with)
    6. Go to https://github.com/Baystation12/Baystation12 and click the Fork button in the top-right. GitHub will do some stuff.
    7. It's time to download the repo, so find a folder you're happy the files being in and use the cd command (ex: cd C:\folder\folder and remember you can copy-paste into command prompt) to move there, or open the command prompt there using the File tab [​IMG]
    8. Enter the command git clone https://github.com/Baystation12/Baystation12.git and wait for it to complete.
    9. Once it's complete, you'll need to cd Baystation12 - then enter git status to ensure that cloning was successful. You should see
      On branch dev - Your branch is up to date with origin/dev - Nothing to commit, working tree clean
    10. Now enter git remote add fork https://github.com/YOUR GITHUB USER NAME/Baystation12.git. This will be what you commit and push to so you can open pull requests to the main repository.
    11. Now you need to create and switch to a new branch. Type git branch nameofbranch to create a branch, then git checkout nameofbranch to switch to it. You should get confirmation messages for the checkout command, such as 'Switched to branch 'nameofbranch''
    12. It's time to make some changes. Open a file in your local copy and change anything. This is just for testing and demonstration purposes, so anything is fine. Then type git status and you'll see a message about changes not staged for commit.
      1. If you're making changes for real, compile and test your changes before committing.
    13. Type git commit -a and a text editor will open. Crazy, right? Any lines preceded by a hashtag/pound sign/# is a comment - Type your commit message above these lines.
    14. Once you've finished writing your commit, save the text file and close the text editor. The command line will do some stuff and then give you your commit subject and a short summary of changes.
      [​IMG]
    15. Now it's time to push your changes to your fork. Enter the following command: git push fork. This shoves your branch's changes up to your repo.
      [​IMG]
    16. Head back to https://github.com/Baystation12/Baystation12 and click the New Pull Request button (if you don't see the green button on the left hand side, which I couldn't get to show up for this guide, but you'll know it if you see it)[​IMG]
    17. Once you're in the pull request menu, first click 'Compare accross forks, then select your repository from the list, then select your branch from the menu named 'compare: dev'.[​IMG]
    18. You'll now see the pull request editor gimmick, which is pretty self explanatory. The title is the title of the PR (automatically filled with the 1st commit's subject) and the body is the description, what it does, etc (automatically filled with the commit descriptions)
    19. Click the green 'create pull request' and you've successfully created a pull request! Wow!
    20. After you've opened your pull request, you might want to switch back to the base branch of the repo, dev. You can do that by typing the command git checkout dev. After a few days, your repo will be out of date, so you'll need to type git pull to update it.

    Setting up and using Git via TortoiseGit
    TortoiseGit is a GUI which integrates into Windows context menus, so instead of needing to use commands, you can use right-click menus and so on. This guide will assume you've done nothing in the guide above, so we're starting from the beginning.

    1. Go to https://git-scm.com/download and download the relevant version.
    2. Head over to https://github.com/ and create an account. It's helpful if your name there is similar to the one you use here.
    3. Install Git, leaving everything at their defaults.
    4. Go to https://tortoisegit.org/download/ and download the relevant version.
    5. Install TortoiseGit, leaving it all at the default. Set your name and email address when asked to; these will be publicly viewable, so keep that in mind.
    6. Move to a folder you're happy with the code living in and right click the background and select Git Clone and enter https://github.com/Baystation12/Baystation12.git into the URL field. Press OK and it will download the repo.
    7. Once it's finished downloading, it's time to set up your fork, which is where you'll push your commits to. Open the newly-created folder (it should be named Baystation12), and select Settings near the bottom of the TortoiseGit sub-menu.
    8. Move to Git -> Remote in the menu and you'll see this screen pop up:[​IMG]
      1. Enter 'fork' into the Remote field
      2. Enter https://github.com/YOUR GITHUB USER NAME/Baystation12.git into the URL field
      3. Enable the 'Push Default' button
      4. Press Add New/Save
      5. Click 'Yes' on both of the dialogues that pop up
    9. It's time to make some changes. For demonstration purposes, open any .dm file and make a change. Edit a name, remove some lines, whatever. It doesn't need to work. Save your edits.
      1. Once you're making changes for real, always compile and test your changes before committing.
    10. Right click your repo folder background and click Git commit -> dev to pull up the commit interface. Enable the 'new branch' option (labelled 1) and type a new branch name in the field that appears (labelled 2), and type your commit message in field 3. Once you've finished (and ensured you're creating a fresh branch), hit the Commit and Push button (labelled 4). This will pull up a log screen detailing some things, and then it will pull up the Push menu. You may be asked to log into GitHub at this point.[​IMG]
    11. Ensure your branch (labelled 1) matches the branch you've just created, that the Remote: (labelled 2) is pointing to 'fork, and then press OK (labelled 3). This will push your changes to your fork.
      [​IMG]
    12. Head back to https://github.com/Baystation12/Baystation12 and click the New Pull Request button (if you don't see the green button on the left hand side, which I couldn't get to show up for this guide, but you'll know it if you see it)[​IMG]
    13. Once you're in the pull request menu, first click 'Compare accross forks, then select your repository from the list, then select your branch from the menu named 'compare: dev'.[​IMG]
    14. You'll now see the pull request editor gimmick, which is pretty self explanatory. The title is the title of the PR (automatically filled with the 1st commit's subject) and the body is the description, what it does, etc (automatically filled with the commit descriptions)
    15. Click the green 'create pull request' and you've successfully created a pull request! Wow!
    16. After you've opened your pull request, you might want to switch back to the base branch of the repo, dev. Right click your folder again and select Switch/Checkout in the TortoiseGit sub-menu, and select 'dev' in the 'Branch' drop-down menu. Click OK.
    17. To update your local copy, click Git Sync in the right-click menu and then Pull:
      [​IMG]

    Squashing or combining commits via the command line
    Squashing is the processing of combining several commits into a single resultant commit. This is done to keep the commit history clean. I'll only explain how to do this via the command line since it's just punching in commands. You'll need a command line open pointed at your repository (use the cd command in Windows to move through directories, or open it via the File menu)

    There are several ways to squash, so a few different methods will be covered.

    Rebase
    1. First, you need to know how many commits you want to squash. Usually, you'll be able to look at the open pull request and it will tell you know many commits you have.
    2. After you know how many commits you want to squash, enter the command git rebase -i HEAD~# where # is the number of commits you want to squash. You'll then get something that looks like this in your text editor:
      [​IMG]
    3. The commands tell you what you can do. For our purposes, we want to set 222222, 3333333 and 444444 to squash, and 1111111 to pick. After you've done that, save and close the editor.
    4. After that, you'll get another text editor to allow you to modify the newly squashed commit message. Save and close the editor once you're finished.
    5. After you've squashed, you'll need to force push the changes up to your repo. Keep in mind that force push will overwrite all commits on this branch, which is what we need it to do in this case. In your command line, enter git push FORK BRANCH --force where FORK is the name of your personal repository (it will be fork, if you set up git according to the guide above) and BRANCH is the name of the branch.
    Reset
    1. Open your command line pointed at your repository, and switch to the dev branch and ensure it's up to date: git checkout dev then git pull
    2. Now enter git merge BRANCH where BRANCH is the name of the branch you want to squash
    3. Enter git reset origin/dev, which will reset your dev branch to the main repository's version.
    4. You can now add your files to commit using either the command line (git commit -a) or your client of choice.
    5. You'll need to force push; refer to the final point in the guide above.
     
    Last edited: May 3, 2018
    TheWelp and afterthought like this.
  2. Sabira

    Sabira Donator

Thread Status:
Not open for further replies.