Git is a complex system. You could easily spend days trying to learn all of the helpful commands you can use to fix mistakes and work with teams more efficiently. I’m currently teaching a beginner-to-intermediate git class. There seem to be a few common consfusions that beginners tend to come across:
- Should I use git push -u, just git push, or git push origin master?
- What is the difference between git fetch and git pull?
- Should I make the repo in GitHub or on the command line?
- Why won’t git diff work after I’ve added files?
- I commited my changes, but it says nothing was changed. What happened?
Let’s go through each one.
Should I use git push -u, just git push, or git push origin master?
Here’s the difference:
$git push: pushes all of the branches that exist on your machine to all of the branches with the same names in your origin (i.e., github repository)
$git push origin
$git push -u origin master: this sets upstream tracking on the master branch. This will allow you to only use git push or git pull without additional arguments and git will “know” that you want to pull or push from the master branch.
What is the difference between git fetch and git pull?
$git pull: This is what you would do to bring a local branch up-to-date with its remote version, while also updating your other remote-tracking branches. git pull automatically merges the pulled commits with the branch you are currently working on.
$git fetch: This gathers any commits from the target branch that do not exist in your current branch and stores them in your local repository. However, it does not merge them with your current branch. You would need to use git merge after fetch to merge files.
Should I make the repo in GitHub or on the command line?
You need to create repository in GitHub to store your code on a remote server (in case you want to share it or just make sure you don’t lose it). You also need to initialize a git repository at the command line ($git init) in the directory that you want to track. This will add all of the necessary git files to the directory so you can start using version control.
Why won’t git diff work after I’ve added files?
$git diff: This shows you the differences between files you’ve staged for commit and the pervious versions of these files. However, once you commit those files you can longer use gif diff. Instead you need to use…
$git diff –cached: This allows you to see the differences between your staged files and their previosuly committed versions.
I committed my changes, but it says nothing was changed. What happened?
This means you probably didn’t add your changes to staging or you didn’t make the changes within your git repository to be tracked. You should reset your commit and do git status to see if you have any untracked files or files ready for staging. Then you’ll want to add all of these files to staging ($git add –all) and then commit.