Git-for-Dummies
Git-for-Dummies copied to clipboard
Git commands cheat sheet to make that shift from GUI to terminal!
README
Git for Dummies
what means what?
- remote - remote means server like github, bitbucket
- local - your repo stored in PC
- remote repository - your repo available on github, bitbucket
- origin - origin is remote from where you have did
git clone
- upstream - upstream is their main repo from which you have forked, useful to get latest changes from their repo.releases
- tag - used for software releases
- master - it's a branch named master (default created branch for new repo)
- pull - get latest changes, remote branches and then move HEAD to latest commit (
git pull
=git fetch
+git merge
) - HEAD - head always refers to the latest commit on your current branch.
- fetch - just download latest changes in separate path and do not integrate with your repo.
git merge
is required to integrate these changes
Stage & Commit
git add . (stage ALL new,modified files)
git add -A (stage ALL new,modified,deleted files)
git add file1.txt file2.txt file3.txt
git add -i (interactive add/revert)
git commit -m 'fixed this and that'
Git append (commit using last commit msg)
git commit --reuse-message=HEAD
_(make shortcut: add,append,push; run once in terminal)_
git config --global alias.append '!f() { git add -A && git commit --reuse-message=HEAD && git push; }; f' (run once)
git append (use it like this)
Git add+commit (1 line, if there's no new file created)
git commit -am "fixed this and that"
Git add+commit (1 line, add newly created files also)
git add -A ; git commit -m "Your Message" (powershell)
git add -A && git commit -m "Your Message" (bash)
Git add+commit+push (1 line, stage newly created files also, run it once to create alias in .gitconfig)
git config --global alias.lazy '!f() { git add -A && git commit -m "$@" && git push; }; f' (run once)
git lazy "fixed bugs" (use it like this)
Push/Pull (Get latest changes)
git push origin
git push upstream
git push upstream/some_branch
git push origin HEAD (push local changes to remote branch with same name)
git pull origin master (pull latest changes from remote master branch into local dev branch)
untrack & remove pushed files
- add those to .gitignore
- git rm -r --cached .
- git add .
- git commit -am "Remove ignored files"
- git push
remove untracked files from local and server
(which persist even after adding to .gitignore)
git rm -r --cached . && git add . && git commit -am "Remove ignored files" && git push
Branch
-
create local branch (code will be copied from current branch ~master)
git checkout -b feature_x
-
push newly created branch to remote (branch is not available to github.com unless you push it separately)
git push --set-upstream origin feature_x
-
Add Remote branch (origin is your repo on github.com)
git remote add origin https://github.com/user/repo.git
-
Add upstream (upstream is their main repo from which you have forked)
git remote add upstream https://github.com/their_user/repo.git
-
Create new local version branch of an upstream branch
git checkout -b feature_x upstream/master (master is the branch name)
-
Switch branch
git checkout feature_x
-
delete the branch
git branch -d feature_x
-
show origin (repo/branch URL on github)
git remote show origin
-
change origin (link to different repo URL on github)
git remote set-url origin new.git.url
-
Clone uncommited changes to new branch make copy of changes in current A branch stack
git stash -u
git stash apply
carry these changes to new branch and do your work
git checkout -b new_branch
To see uncommited changes on previous A branch
git checkout A
git stash apply // or if you've made stash in some other branch: `git stash list` and then use correct number: `git stash apply 2`
Status of remote - local - origin branches tracking
git remote -v
git branch -d feature_x
Status of remote - local - origin branches tracking
git remote -v
git remote show origin
git status
Create Pull Request
-
for can-menifest:
hub pull-request -b release -r ankit-shukla
hub pull-request -b pre-release -r ankit-shukla
-
for can-cntral:
hub pull-request -b bixby-2 -r ankit-shukla
Repo Status
git status
git add -i
-
show latest commits (to exit type q)
git log
-
Display current branch
git branch
Git GUI
use fork app on windows
Built-in git GUI
gitk
Git interactive commands
git add -i
use colorful git output
git config color.ui true
I f**ked up
Resolve Merge Conflicts:
revert all our changes and pull latest from upstream (their repo):
git reset --hard HEAD
git pull -s recursive -X theirs upstream branch_remote
Revert last commit and keep the changes (local)
git reset HEAD^
Revert last remote commit (from remote, untracable)
git pull #to get that commit to local
git reset HEAD^ #remove commit locally
git push origin +HEAD #force-push the last HEAD commit to remote
Revert all local changes and local commits (local)
(fetch the latest history from the server and point your local master branch at it )
git fetch origin git reset --hard origin/master (master is the branch name)
Revert everything I did & make repo sync with upstream
git reset --hard upstream/master (master is the branch name)
git pull upstream master
Clean up a fork and restart it from the upstream
git reset --hard upstream/master (master is the branch name of original repo)
git push origin my_branch --force (or git push origin HEAD --force)
Git pull without committing local changes
-
hide your local uncommitted changes temporarily
git stash --include-untracked // shorthand git stash -u
-
show all stashes
git stash list
-
-
get latest changes
git pull
-
now unhide your local uncommitted changes
(pop
will restore only latest stash)git stash pop
or
git stash apply
git stash pop
restore changes and also removes it from stack,git stash apply
restores it but still keeps it on stack for possible later reuse (or you can thengit stash drop
it). Sogit stash pop
isgit stash apply
&&git stash drop
git Users
Set
git config --local user.name "localuser"
git config --local user.email "[email protected]"
git config --global user.name "globaluser"
git config --global user.email "[email protected]"
Get
git config --local user.name
git config --local user.email
git config --global user.name
git config --global user.email
git config --list
Remember Me
Remember username & password
1) Secured Way (Store globally)
git config --global credential.helper manager //secured way for Windows
git push http://example.com/repo.git
Username: <type your username once>
Password: <type your password once>
2) Unsecured way (Store globally)
git config credential.helper store //username & password stored in plain-text in "%UserProfile%\.git-credentials"
git push http://example.com/repo.git
Username: <type your username once>
Password: <type your password once>
3) Unsecured way (Store locally per repo)
//saved in file 'cred' inside repo .git folder. Need to manually delete this file.
git config credential.helper 'store --file=.git/cred'
4) Secured Way (Store in Cache)
git config credential.helper 'cache --timeout=864000' // 10 days expiry
git credential-cache exit // remove it from cache before timeout
Remove credentials
git config --unset credential.helper
git config --local --unset credential.helper
git config --global --unset credential.helper
git config --system --unset credential.helper
//Windows: delete from Control Panel\User Accounts\Credential Manager
Download big repository on poor bandwidth:
HTTPS Clone repo vs SSH Clone repo
- HTTPS Clone - easier, works through firewalls & proxies, ask password everytime for push-pull (but password can be saved using
git config credential.helper store
) - SSH Clone - manually create SSH key & add it to github, password not required for push-pull. Remove key from github to revoke authorization for that PC.