Get Started with Git

Get Started with Git

So you have git installed, and you have a repository created! Well done!

Now you need your Swiss Army Knife! The top 10 commands that are sufficient for you to interact with any git-based project!

1. Look at the "remote" versions of the repository

git remote -v

2. "Fetch" the updated state in the remote repository (presuming your remote is called origin)

git fetch origin

3. Create (and "checkout") a new branch

# a new branch
git checkout -b my-new-branch

# an existing branch
git checkout my-old-branch

4. Look at local (and remote) branches

# local
git branch ls

# all branches (including remote)
git branch ls -a

5. "Commit" / save your local changes

git add my-file.txt

# this opens your local terminal editor
git commit

6. Push to the remote copy of your branch

git push -u origin my-new-branch

# and then afterwards
git push

7. Pull in changes from the remote

git pull

8. Pull in changes from the remote (on a different branch, let's say main)

git fetch origin
git merge origin/main

NOTE: there is a "scary" part of git that takes some getting used to. This is called a merge conflict. It takes some getting used to, and can definitely feel scary at first! It happens when your local changes and your "remote" changes have a conflict (i.e. you both change the same line of the same file).

Not to fear! We will walk through this later!

9. Look at a log of changes

git log

# or a concise version
git log --oneline

10. Look at a diff between branches

# unsaved changes
git diff

# staged changes
git diff --cached

# change against a particular branch
git diff origin/main

Onward and Upward!

So there you go!! Give those a commands a shot. If you get comfortable with those couple of commands, you will be comfortable with git before you know it!