# Software WG tutorial on Git at CNS2021 Online - URL: https://ocns.github.io/SoftwareWG/2021/06/09/software-wg-tutorials-at-cns-2021-online-bash-git-and-python.html - Feedback form: TBA - Time: 1700 UTC ## Basics - Get the version of Git you are running: `git --version` - Create a new folder: `mkdir <name of folder>` - Enter the folder: `cd <name of folder>` - Initialise your local repository: `git init` - Observe a new `.git` folder: `ls -lash` - Very very rare for us to modify the `.git` folder by hand! - Create a new file: `echo "Hello Git world" >> Readme.txt` - Inspect the file in your editor, or use `cat Readme.txt` - Look at the status of your Git folder using `git status` - Tell git to track your file: `git add Readme.txt` (Windows users may see a warning about LF being converted to CRLF) - Let's add another line and see what git thinks: `echo "Hello again" >> Readme.txt` - Git says something has changed: `git status` - See what changed: `git diff` - The `@@ ...@@` bit in `git diff` tells us what lines have changed. - Next commit the version of the file that we staged: `git commit` - A step back: if you are using git for the first time, it'll ask you to provide your e-mail address: `git config global user.email "myemail@something.com"` - Write a good commit message: good commit messages save time later when you're going through long lists of commits - A summary that completes the sentence: "if applied, this commit will ...". It should be short, no longer than 70 characters generally - Then, leave a blank line and add *whatever* information you wish: describe what you did, why, how, write your musings, whatever! - See your commit: `git log` - the first line shows the git hash: unique for every commit - next, the author of the commit - next, the date/time when the commit was made - then, whatever the commit message was. - Use `git log --oneline` to see only the commit summary messages - If you get a "vi" editor for the commit message: - press "i" to enter insert mode - write what you wish - press ESCAPE to leave insert mode - press ZZ (capital z twice) to save the file - To change your default editor to `nano` (or something else that's installed on your system): `git config --global core.editor "nano"` - To see what you've staged using `git add`, use: `git diff --cached`. - For quick commits, where you think a short commit summary is enough, you can use `git commit -m "your awesome commit message"`. - See what branches you have: `git branch` - by default, you should be on "main" or "master" - in `git log` output: `hash (HEAD -> main)` means we're at this commit, on this "main" branch. - Create a new branch: `git checkout -b <new branch name>`: by default creates it at the current HEAD - Create a new branch in any other commit: `git checkout -b <name of branch> <commit hash to branch at>` - See complete git log tree: `git log --all` - Add a graph, to see the "tree": `git log --all --tree`. - See what changed in each commit: `git log -p` (shows usual diff output) - Suggested commit habits: - Commit early commit often! - Group logical changes into one commit! - Pushing to remotes, e.g. GitHub: - Create a repository on GitHub - Set the URL for the remote, calling it "origin": `git remote add origin <URL from GitHub` - Push your branches to the remote: `git push origin --all`