saving a snapshot of your fi le system as subversion would. this makes the repository smaller, as well as makes branching and merging a much easier thing to deal with. git is a distributed version control system. that means that almost everything you do is local, until you push your changes out to a remote shared respository. what else? i don’t know, ask me!
cd to it in your terminal, type ‘git init’, and hit enter! now, git knows about your folder, but it doesn’t know what to pay attention to yet. that’s the next step.
also every time you want to make a commit. this is the thing that tells git a) what to pay attention to or b) what changes to include, if it’s already paying attention. there are three stages to git. •in the working directory (tracked/untracked) •staged •all up in the repo
if they are untracked fi les, the whole fi le is a change. if they are tracked fi les, git knows what the change is. in the working directory you have added your changes so that you can commit them. staged you have committed these changes... so they’re not changes anymore. all up in the repo
a few different options here. actually, there are a lot, but lets just cover the common ones. add by path git add Source/Whatever/ fi le.thinger this adds the changes in this fi le, to the staging area. remember, if it was previously untracked, the changes will be the entire fi le.
mean?! .gitignore this is where you put stuff that you don’t want in version control. for example, .DS_Store, or a top secret fi le of top secrets keys and secrets! you never need or want that in version control.
add -p this lets you add your changes in patch mode, which will essentially break up the changes in each fi le into smaller chunks for separate commits. this is particularly useful if you just implemented like 8 things without committing at all, and want to be a good person and make 8 separate small related commits instead of one big scary one.
are before you commit them. you can see these changes in either your working directory or your staged area. you can also see the changes in just one fi le, and any of these options without whitespace.
into your local repository. again, there are lots of options, but let’s go with the most common ones. commit with no options git commit commits your staged changes and opens whatever text editor is default on your machine for you to enter a commit message.
is the commit msg!’ commits your staged changes and uses the given message for your commit. commit all tracked changes with message inline git commit -am ‘this is the commit msg!’ the only way this differs from the previous is that it also adds all tracked changes in your working directory. remember, the danger zone!
all staged changes, as well as allows you to add changes from your working directory in patch mode, similar to add -p. still opens an editor to add your commit message.
remove the fi le as well as remove it from git.... that’s about it. you can also use git rm —cached to remove things from the git index without _actually_ deleting them.
checkout a branch, it checks out the latest commit on that branch. you can also use this as a cheat to make a new branch checkout a branch git checkout branch-face-mcgee checks out the latest commit on that branch. if you have any local changes, they will be carried over with you as local changes. if you have local changes that don’t merge nice, git will tell you to fi gure it out before you can switch branches.
that commit in ‘detached head state’. i know, sounds spooky, but that just means you can’t commit, because you’re not on a branch and git would not know what history to put that in. luckily, you can
checkout -b some-pants now you have a new branch called some-pants that is based on whatever branch you were in. you can do this from a branch, or detached head state, whatevs. git checkout
them git restore path/to/the/thing.rb checkout changes that are already staged to remove them from staging but keep them in your working directory git restore —staged path/to/the/thing.rb
a new branch based on the current one git branch the-new-branch keep in mind, you’ll still have to checkout this branch, since we didn’t do the super awesome mega checkout -b combo.
like the repos on github or heroku. add a remote repo to track git remote add origin [email protected]:crebma/git- demo.git remove a remote git remote remove origin use a remote git pull origin main git push origin main git reset --hard origin/main
changes git reset remove staged AND working directory changes git reset --hard remove all of the above and reset your history to a known good place, like upstream main git reset --hard origin/main
the untracked fi les hanging around, like those pesky .orig or diff fi les from merge con fl icts. remove untracked fi les git clean -f remove directories git clean -df you need all those f’s because they mean “force”, and as a safety feature, git by default requires force on a clean. you can change that setting if you’re feeling wild.
current branch. merge a local branch git merge main merge a remote branch git merge upstream main if you run into merge con fl icts, you can resolve them however you like. personally, i set up p4merge with git so that i can just use ‘git mergetool’. escaping when everything goes wrong git merge --abort
fetch and a merge to pull the latest change into your current branch. it’s smart to name the remote and upstream as opposed to just doing a blind pull, though there are settings you can set to have a default. i prefer to be explicit. pull a remote branch git pull origin main this will have the same deal as merge; you may run into con fl icts and resolve them.
local repository and pushes them to a remote and branch you specify. again, it is smart to always name the remote and branch, though you can do the same settings thing as with pull. you can also push to remote HEAD which will push to the same branch on a given remote as your current branch. push explicitly git push origin main push using head git push origin HEAD
you do a push, so that you wouldn’t have to name them again in the future. push with a set upstream git push -u origin main subsequent pushes will go to origin/main git push
the latest one released in some environment. once you create a tag, you push it or fetch it just like a branch. list all tags git tag -l push a tag git push v1.5.7 make a tag git tag -a v1.5.7 checkout a tag git checkout v.1.5.7
rearranger of histories. it takes all of your divergent commits in a branch and stashes them, applies all divergent commits from the branch you’re rebasing to and puts those on the history stack, and then applies all of your divergent commits one by one. there is a lot of room for trouble here, like merge con fl icts or messing up everybody else. the basic rule is, if you have ever pushed this history to a remote repository, don’t rebase it. if git ever tells you you need to do a force push and you are using a shared repo, don’t do it. undo what you did, and just do a merge.
you’re fi ne! just checkout a new branch from your current main, go back to main, and reset —hard to origin/main, then head back to your branch to continue: git checkout -b feature git checkout main git reset —hard origin/main git checkout feature
to your branch. say you have this feature branch and you fi x a bug in a commit on it. suddenly you realize the bug exists in main! just take a look at your log to fi nd the SHA of the commit, say its abc123, checkout main, and then cherry-pick that commit onto main! git log git checkout main git cherry-pick abc123
have no idea when it was introduced. git bisect does a binary search through your commits, allowing you to pinpoint the commit in which the bug was introduced without too much hassle. this is a super good time to be reminded about only making working commits.