Whether through a training or via hands on experience, this section will provide clear shortcuts to using git efficiently.
- Create a repo
- Get the HTTPS URL
- Using git, navigate to the folder you wish to clone it into
- Run this command where URL is the URL from GitHub:
git clone URL
This article explains it beautifully, but basically ...
- After you clone your repo, change into the directory.
- If you run
git branch
you may be missing some. To see all run:git branch -a
- To peek at upstream branch, run
git checkout origin/BRANCH
... But if you want to work in it run:git checkout BRANCHNAME
This will now be on your local machine.
This section will be used to cover working with branches
- Using your local git, navigate to the folder you need to be in. (Assuming you have already set up your remote repo.)
- Run the following command:
git branch BRANCHNAME
(where BRANCHNAME is whatever branch you want to call it) - Run the following command:
git checkout BRANCHNAME
Or if you know you'll be going straight into this new branch?
Then all you need to do for 2 & 3 is run: git branch -b BRANCHNAME
You will now be inside your new branch! Need to get back?
git checkout master
PS - there are other options you can do with this. Be sure to checkout documentation.
Just run: git branch
Just run: git checkout BRANCHNAME
Run this command: git push -u REMOTENAME BRANCHNAME
If it's your repo, default REMOTENAME is origin.
PS - If you do this, then the subsequent pushes don't need the -u
anymore:
git push REMOTENAME BRANCHNAME
If you try to run git pull
from a branch that does not have an upstream, you will get an error similar to this:
Kat (thank-you) cheatsheets $ git pull
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.
git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=origin/<branch> thank-you
In this instance, it would look like: git branch --set-upstream-to=origin/thank-you thank-you
What this does is that it allows you to pull down changes made directly to your branch on GitHub.
Or you can just follow the above suggestion.
If you have your upstream setup, then just run: git push REMOTENAME BRANCHNAME
Where REMOTENAME is the name given to your remote location (default is origin)... And BRANCHNAME is the name of your branch.
When in the working branch you wish to update, run: git pull
This basically is a git fetch
and git merge
in one command.
This was an excellent explanation for both local and remote.
Just run: git branch -d BRANCHNAME
Just run: git push --delete REMOTENAME BRANCHNAME
OR even: git push <remote_name> :<branch_name>
When in the working branch you wish to update, run: git pull
This basically is a git fetch
and git merge
in one command.
This is where you add changes to your staging area before committing your changes. This simply means git knows about the change but it is not a permanent change in your repo.
To add a single file: git add FILENAME
To add a folder: git add FOLDERNAME
To add all changes: git add .
Just ru the following to remove all changes: git reset HEAD~
After you've added files to your staging, you now need to commit the changes.
Be sure to check out this styleguide from Udacity.
Run this command: git commit
If set up properly, it will open your code editor for you to write a message.
Otherwise, run: git commit -m "Some message here"
If you have not yet pushed to your server, you can follow these commands as per this StackOverflow:
git reset HEAD~
You can now make changes, stash, etc.
This refers to your remote repo - like GitHub.
This did a great job of explaining.
Assuming referential integrity broken (or you just want the URL): git config --get remote.origin.url
Assuming referential integrity is in tact or you require full output: git remote show origin
When using git clone
the default name for source is origin and using the following command will display info about this remote name: git remote show
This show command will let you know the names of all the remotes you have for the current branch.
The following will show all for your specified branch (like origin): git remote show BRANCHNAME
git stash
allows you to put whatever you're doing on hold. This is really effective for when let's say your boss wants you to drop what you're doing to fix a bug. You stash your current state of a file, change branch, do your work and all the updates, then get back your state!
- Run this command in git:
git stash
Congrats! It's stashed.
Run this git command: git stash list
Run this command: git stash pop
Please see a more detailed explanation here.
When you use git status
you are able to see which files are changed in the working directory, but not the actual changes. That's where git diff
comes in. It will show you how or what changes were made.
This will show all current changes in working directory that have not yet been staged.
Just run: git diff --staged
Run git log
to see overview of recent commits. You can add the -p flag to get the detailed "patches" (changes) of each commit.
git log -p
Normally if you run git fetch
you can get the remote's data without integration into your current code. You can then run git merge
...
Or you can simply run git fetch
which does both for you.
Please see a more detailed explanation here.
Just run: git diff BRANCH1..BRANCH2
Just run: git diff VERSION1..VERSION2
You can get this info from running: git log
Sometimes you get a long list of changes when running git diff
or git log
.
So how do you quit? Just press: q
Got this from here.
To ignore a file, set of files or folder you will need to add them to the .gitignore
file.
This needs to be in the root of your project folder.
To specify a particular file: FILENAME.extension
To specify all types of files: *.extension
To specify a folder & it's contents: FOLDERNAME\
When you add those lines to the file, they will no longer show up when running: git status
However, if they were already part of the project?
It doesn't remove them from there and they will not be ignored.