Git is a version control tool commonly used in software and hardware engineering.
The main premise is local copies (for each person working on the project) and a global repository. When people are ready to push or pull changes from the repository, they can. The idea is that we work on our own copy — and change, build, and test on our local copy before syncing with the global repository.
We can also control when others’ changes are visible. Then we immediately re-build and re-test right after we pull. This allows us to fix any integration errors right away. It’s easier to commit/push and pull frequently — many small integrations are easier to debug than one huge one.
Commands
All commands are prefixed with git. Sync commands:
addto stage changes for committing later..to stage everything, add a period after the command.file-nameto add a specific file.
committo commit changes to the repository.-m "commit message"should always be specified. Make these messages meaningful!--amendto modify the most recent commit by combining staged changes with it.
fetchdownloads the remote content without merging to your local repo.pullto sync all remote changes and merge (by default) to your local copy.- Immediately build and test after a
git pull. This fixes bugs right away. It’s always better to commit/push/pull frequently, since many small integrations are easier to debug than one huge one. -rwill sync remote changes and rebase.
- Immediately build and test after a
push -uto push staged/committed changes to the repository. Best to push after committing.rmto remove files. By default this removes from both the remote repository and the local filesystem.--cachedonly removes from the remote repository.-rspecifies removing an entire directory. Pair with above if necessary.
Branch and revision commands:
checkoutto switch to a different version of the repository. This will change theheadto the commit we’re working with.branch-nameto switch to a different branch.revision-idto checkout an old revision.masterto the last commit. This will changehead.
revert revision-idto unto a certain revision commit.- We also have to
pushafter this.
- We also have to
logto see a list of files modified by every commit and a message for each commit.--onelineto condense each commit to a single line.-n numto limit the displayed number of commits.--author="string"to display commits from a given author. This can be a regular expression.-pto display the full diff of each commit.file-nameto display changes to a given file.
diffto show the difference between the last commit and what you have currently.r1 r2to show the difference between two commits. They don’t have to be one after the other!--cachedshows what exactly is going into the commit.
show revision-idto see everything changed by the given revision.branchfor branch options:branch-nameto create a new branch without checking out the new branch.- Prefix this with
-dto delete the specified branch only if it doesn’t have unmerged changes. -Dfor a force delete.
- Prefix this with
-m new-nameto rename the branch.-ato list all remote branches.
stashto save uncommitted changespushto restore changes previously stashed
Other useful commands:
cloneto clone an existing repository and set-up for the first-time on our local machine.urlto clone from a repository accessible from a URL.
statusto show information about the current state of the repo.- Only talks about the local repository, not the remote repo.
configto change configuration options.-llists the currently saved options.
cleanremoves untracked files from the working directory.resetremoves tracked files from the working directory.--hardto reset back to the last pulled revision.
initinitialises a new repo.remotemainly configures the remote repository.-vlists the remote repository locations.
Addendums
Git has several conventions (commit messages, what to include in a commit, what to check into version control). One commit message convention is the conventional commit standard.
Whenever you forget to specify a message when you commit changes, Git will spit out something like the below. Once you type in a commit message, you can hit the ESC key and type in :wq.
Please enter the commit message for your changes. Lines starting with ’#’ will be ignored…
Git doesn’t take a lot of space! That’s because it only stores the differences rather than the entire files.
What happens when we have conflicting edits? We should build and test. What if we modified the same file? It’s fine if we modify different lines, but if there’s a real conflict, then Git will tell us what lines are conflicting.
Git is divided into remote/local repositories.
We can specify files for Git to automatically ignore in a gitignore file.
Resources
- Pro Git, by Scott Chacon and Ben Straub