@rkenmi - Git the Essentials

Git the Essentials


A basic cheat-sheet of everyday Git commands.

Git the Essentials


Back to Top

Updated on May 28, 2019

Checkouts and Commits

Scenario: I want to check out some branch B

git checkout <branch B>

Scenario: I want to add some files that I updated and commit them

git add -i
... (Add your files interactively)
git commit

Scenario: I created some brand new files and need to commit them.

git add <file path>
git commit

Scenario: I drank a 24 oz. can of Red Bull and decided to make 4 small commits that are more-or-less pretty related. Even the commit messages sound similar. When people look at the git log history, they're going to be in a world of pain. Oh how I wish I can just bundle these commits up into one.

git rebase -i HEAD~4

Four commits will be shown in the git editor, with the bottom commit being the most recent.
Each commit will start with "pick" by default

pick 01d1124 Set up contract
pick 6340aaa Rename contract
pick ebfd367 Spit on contract
pick 30e0ccb Polish contract

I want to keep the first commit's message, "Set up contract", and just bundle the 3 commits after it into the first one. This can be done by replacing the text where it says "pick" with "squash". Any consecutive "squash" will apply only to the last preceding "pick".

pick 01d1124 Set up contract
squash 6340aaa Rename contract
squash ebfd367 Spit on contract
squash 30e0ccb Polish contract

Save and quit with :wq.

Merging

Scenario: I want to merge this remote branch R into my current branch.

git merge <branch R>

Scenario: I got some merge conflicts after I tried to merge.

... (Fix the merge files on your favorite editor/IDE)
git add -i // Add the files you fixed
git commit

Note: By default, your commit message will be something along the lines of "Merge branch 'some repo/some branch' into branch B"

Scenario: I want to merge this up-to-date branch Z with this really old branch O of mines. There's gonna be a crap ton of merge conflicts, and I want branch Z's changes over branch O.

git checkout <branch O>
git merge -X theirs <branch Z>

Note: -X stands for strategy-option, and theirs is a strategy option.

Pushing

Scenario: Alright, I'm done with my commits! I want to push branch X to the origin repository.

git push origin <branch X>

Deleting

Scenario: I have a local branch A that I want to delete.

git branch -D <branch A>

Scenario: I have a remote branch A that I want to delete.

git push origin --delete <branch A>

Note: git push is misleading here, but the --delete gives us some sense into the right direction. It might help to think that we are pushing a request to delete a branch on the remote repository (origin, in this case).

Pulling

Scenario: I am currently on branch G, which exists in my remote repository called origin. Susan just pushed her latest changes to branch G in origin. How do I easily pull her changes so that my branch G is up-to-date with origin?

git pull origin <branch G>

Scenario: Carl just created a new branch J on my remote repository called origin. I'm on branch X and branch X is super different than branch J. But I do want to take a peek at his branch and check it out, because he said the branch has pizza in it.

git fetch origin <branch J>
git checkout origin/<branch J>

Scenario: Carl created a new branch E on his own repository called catz. I want to take a peek at his branch E, because he said the branch has cats in it.

git fetch catz <branch E>
git checkout catz/<branch E>

Scenario: I pulled a branch with sub-directories that point to other Github repositories. The parent directory has the updated changes, but the trackers aren't pointed to the latest commit, even though I just pulled.

git submodule update --recursive --remote  

Note: The sub-directories are just submodules, which are tracked

Scenario: I cloned a branch with submodules, but the submodules don't look right.

git clone --recursive <project url>  

Article Tags:
gitversion controlwiki