Common Git Commands
Useful Git Commands: | |
git config --global user.name "Emmanuel Rosani" | |
git config --global user.email "manters2000@cicct.usjr.edu.ph" | |
//Ignore files globally | |
//Create .gitignore_global file in the user directory containing files/folders you wish to ignore | |
git config --global core.excludesfile Users\Rosani\.gitignore_global | |
git add . | |
git commit -m "My message" | |
git status | |
//show commit logs | |
git log | |
// show difference between files | |
git diff | |
// Delete File and adds it to staging index | |
git rm --filename | |
//Rename file and adds it to staging index | |
// moving and renaming are synonymous | |
git mv first_file secondary_file | |
// Useful only for modified files. Not for deleted files. | |
git commit -am "Commit Message" | |
// Undo changes in file | |
// can be used for branches and files | |
// -- indicates that we're not checking out on branch, | |
// we're checking out file in the current branch | |
git checkout -- index.html // undos changes in index.html file | |
//Unstage changes in file | |
git reset HEAD filename.txt | |
// Amending commits (Make changes to recent commit) | |
git commit --amend m "New commit message" | |
// See changes in staged file | |
git diff --staged | |
// RESET IS LIKE REWINDING | |
// Reverting changes to commit | |
// Undo changes in commit | |
git revert ed5f91c77f52b3c9ff | |
// Soft Reset | |
git reset --soft 7f988b5956661ea8 | |
// Mixed Reset | |
git reset --mixed 7f988b5956661ea8 | |
// Hard Reset | |
git reset --hard 7f988b5956661ea8 | |
// Delete untracked files | |
git clean -f | |
// Sample .ignore file | |
# comment | |
tempfile.txt | |
*.zip | |
*.gz | |
log/*.log.[0-9] | |
assets/photoshop/ | |
assets/videos/ | |
# Do not ignore videos in this directory | |
!assets/videos/tour_*.mp4 | |
// Ignore tracked files and not remove the file from the directory | |
git rm --cached tempfile2.txt | |
// Track empty directories | |
add .gitkeep in it | |
// Create new branch | |
git branch name_of_branch | |
// Create a new branch and checkout on it | |
git checkout -b name_of_branch | |
// Compare changes between two branches | |
git diff master..name_of_branch | |
// checks if commits are merged in current branch | |
git branch --merged | |
// Rename a branch | |
git branch -m name_of_branch name_of_new_branch | |
// Branch to delete | |
git branch -d branch_to_delete | |
// set command prompt to show directory and current branch currently working i.e(explore_california(master) > ) | |
// must be added to .bash_profile file in user/home directory | |
export PS1='\W$(__git_ps1 "(%s)") > ' | |
// Merge current branch to master branch | |
// Firstly, checkout to branch you want it to be merged ie (git checkout master) | |
// branch_to_merge will be merged to master branch | |
git merge branch_to_merge | |
// show changes on commits | |
git show 58b3bfc | |
// Saving changes in stash | |
git stash save "Message" | |
// Show stash list | |
git stash list | |
// Show edits in stash | |
git stash show -p stash@{0} | |
// Apply changes on a particular stash and delete it | |
git stash pop stash@{0} | |
// Apply changes on stash but not delete the stash | |
git stash apply | |
// Delete/drop particular stash | |
git stash drop stash@{0} | |
// Clear all stashes | |
git stash clear | |
// Add remote repository and push it in remote server | |
// Track remote branches | |
git remote add origin https://github.com/manters2000/lynda-explore-california-git-training.git | |
git push -u origin master | |
// show remote and local branches | |
git branch -a | |
// FETCH GUIDELINES: | |
// fetch from remote repository for synchronization | |
// - always fetch before work | |
// - fetch before you push | |
// - fetch often | |
git fetch | |
// Merge from origin master to master branch | |
// git pull = git fetch + git merge | |
git pull origin master | |
// Delete a remote repository | |
// prepend a colon to the remote branch name | |
git push origin :non_tracking | |
git push origin --delete non_tracking | |
SAMPLE COLLABORATION WORKFLOW | |
- git checkout master (Should be on Master branch) | |
- git fetch (Find out what new commits has been changed) | |
- git merge origin/master (merge changes from local master branch to remote master branch to be totally in synced) | |
- git checkout -b feedback_form (creates feedback form branch as a new feature for the new site) | |
- git add feedback.html (Add for staging area) | |
- git commit -m "Add customer feedback form" | |
- git fetch (for any changes added to another collaborates) | |
- git push -u origin feedback_form |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment