Created
June 7, 2019 08:26
-
-
Save meesont/a0676bef9f633097a5afe02df8626c98 to your computer and use it in GitHub Desktop.
Notes for Git and GitHub command line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Right click on folder and select 'Git Bash' | |
git init --> initialises repository in current folder, can be init in new and current | |
Even though you're working under change control, it isn't a backup | |
Folder can be zipped, and thus repo accessed by anyone zip it is sent to | |
git status --> shows standing at any given time | |
git add <file_name> --> stages file for commit | |
When you make a change to a previously staged (tracked) file, you need to stage the new changes | |
git add . --> stages everything | |
git add *.html --> specified wildcard pattern, e.g. stage all files with html extensions | |
.gitignore --> add directories to be ignored in staging/commits (make sure to stage and commit this file) | |
git commit --> ends up in VIM-like screen | |
press 'I', puts you in 'insert' mode | |
enter commit message | |
hit 'Esc' key to exit 'insert' mode | |
enter ':wq' to rewrite current file and exit, causing Git to finish commit | |
git commit -m 'change 3' --> avoids VIM screen for simple commit message | |
git commit -a -m 'change 6' --> skips staging step, any changes to tracked files are automatically staged (untracked files need to be manually added, git add) | |
git log --> view commit history, useful when working with other developers | |
enter q to quit git log | |
branching allows you to work on a copy of the code in the main line without affecting the main line directly | |
e.g. when working on bug fix/new feature, with all benefits of source control | |
git branch <branch_name> --> creates new branch | |
git checkout <branch_name> --> switches to branch | |
make changes, stage and commit changes | |
git checkout master --> switches to main branch | |
ensure you are on destination branch, git status to view current branch | |
git merge <source_branch_name> --> merges changes from <branch_name> | |
merge conflict - if you make a new branch, and then make some changes on the master branch, as well as different changes on the new branch | |
as they are at different stages of development, when you attempt to merge your changes back into the main line you will have a conflict | |
<<<<< HEAD --> version in current branch | |
>>>>> master --> version of source branch | |
you must manually handle the conflict | |
to avoid complexity in bigger project: git mergetool --> launches tool to make fixing merge conflicts a lot easier, must be set up | |
if you're in a branch and make changes but don't commit them, you must stash them temporarily | |
otherwise changes will persist during branch change - major issue | |
git stash --> takes dirty state of branch including tracked modifications and staged changes and saves on stack of unfinished changes that can be reapplied at any time | |
git stash apply --> re-applies changes when you return to continue work | |
### remote repositories ### | |
even though you're interacting with remote repository, everything is still happening locally | |
you must manually retreive/push changes to remote repository | |
git remote --> view list of existing remote repositories you have | |
any remote repos added will only apply to current local repo | |
git clone <repo_url> --> clones repo, pulling entire repo including commit history, putting it into own folder | |
cd <clone_name> | |
git remote --> will now show remote repo within cloned repo file | |
git remote -v --> shows urls - use alias shown when issuing git commands as opposed to using the url | |
## keep repo up-to-date when making changes ## | |
git fetch <repo_name> --> goes out to server and gets any changes made since you last cloned/fetched | |
pulls data into local repo, but will not merge into your work - you will need to merge manually | |
git pull <repo_name> --> automatically fetches and merges changes from remote branch into current branch | |
when you're ready to submit your changes, commit your work, then... | |
git push <repo_name> <branch_name> --> tells Git to push changes to remote repo known to us as <repo_name>, and commit to <branch_name> | |
*will be prompted for credentials* | |
git remote add <repo_name> <repo_url> --> adds additional remote repo | |
NOTE TO SELF: ### added local repo to online repo? ### | |
### TOM UPDATES | |
### This is useful for first setup with remote repo, simplified without definitions | |
git init | |
git add . | |
git commit -m "first commit" | |
git remote add origin https://github.com/username/project.git | |
git push -u origin master | |
### For merging branches | |
git checkout <branch_to_merge_too> | |
git merge <branch_to_merge_from> | |
git push | |
### Common usage | |
git fetch && git pull --> fetch and pull from a remote repo at the same time | |
git rm --cached <file_to_remove> --> remove a file from the remote git repository | |
GITHUB: | |
git status (to view if there's anything needing to be committed) | |
git add . | |
git commit -a -m '{commit name}' | |
git remote (to view remote repositories) | |
git push pp master (pp is the remote repo name ('Phishing Project')) | |
(IF YOU WANT TO UNDO A RECENT COMMIT SO LONG AS IT HASN'T BEEN PUSHED) | |
git log (view commit history) PRESS Q TO QUIT LOG | |
git reset HEAD~ | |
*make edits, or re-commit as shown in instructions above* |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment