Compiled by Sumanta Bose
Email: sumanta001@e.ntu.edu.sg
This is a playground to learn and practice git commands and implementation details. Here we will learn how Git works.
$ git config --list (to check username & email in local repo)
$ git config --local user.name "username" (local repo)
$ git config --local user.email "useremail@domain.com" (local repo)
$ git config --global user.name "username" (global systemwide)
$ git config --global user.email "useremail@domain.com" (global systemwide)
$ git config credential.username "new_username" (local repo)
$ git config credential.username --global "new_username" (global systemwide)
$ git config --get remote.origin.url (to get remote URL)
$ git remote add origin https://{url} (to set a new remote 'origin')
$ git remote -v (to verify the new remote)
$ git remote set-url origin https://{url} (to set remote URL)
$ git clone <URL> or git init
$ git diff
$ git status
$ git add <filenames> (to undo use git reset <filenames>)
$ git commit -m “message” or git commit
$ git push
$ git pull
<work on files>
$ git status
… repeat
$ git push <remote-name> --all
<remote-name>
is usually origin
.
$ git push <remote-name> <local-branch-name>:<remote-branch-name>
This pushes <local-branch-name>
to <remote-name>
, but renames it to <remote-branch-name>
.
$ git clean -df (d for directory and f for file)
$ git branch
$ git branch <branchname>
$ git checkout <branchname>
<work on files>
$ git diff
$ git status
$ git add <filenames>
$ git commit -m “message” or git commit
$ git checkout master
$ git pull
$ git checkout <branchname>
$ git merge master
$ git push (git push --set-upstream origin <branchname>)
send a pull request to other members
after someone accepts the pull request, you may or may not delete the branch
$ git branch
$ git branch <branchname>
$ git checkout <branchname>
<work on files>
$ git diff
$ git status
$ git add <filenames>
$ git commit -m "message" or git commit
$ git push origin <branchname>
$ git checkout master
$ git pull origin master (to get updated with the current code before merging/pushing)
$ git branch —-merged (shows branches that have been merged so far)
$ git branch —-no-merged (shows branches that have not been merged so far)
$ git merge <branchname>
$ git push origin master
$ git remote -v
$ git branch -a (to see all branches)
$ git branch -r (to see remote branches)
$ git branch —-merged (shows branches that have been merged so far)
$ git branch —-no-merged (shows branches that have not been merged so far)
$ git fetch or git fetch origin (will fetch all of the remote branches)
$ git branch -v -a (see all branches verbose)
$ git checkout test [will NOT work in if you have multiple remotes]
$ git checkout -b <name of local repo> <name of remote>/<name of remote repo>
[usually <name of remote> = origin]
$ git pull origin master
$ git push origin master
$ git push -u origin <branchname> (and git push in future)
$ git branch -—merged
$ git branch -d <branchname>
$ git branch -a
$ git push origin —delete <branchname>
$ git diff : gives the difference in file contents between the last committed (or pushed idk) and the current version of the files
$ git checkout <filename> : goes back to the last committed (or pushed idk) version of the file
$ git commit --amend -m "New Commit Message" : overwrites the last commit message to "New Commit Message".
This changes the commit hash and git log history.
$ git commit --amend + (Esc+:wq) : this will commit any newly added files as part of the last commit
$ git log —stat : this will show a list of the files that have been changed in the commit
stay in the <from-branch>
$ git log & copy the first few characters of the commit you want to copy
$ git checkout <to-branch>
$ git checkout <to-branch>
$ git cherry-pick <hash> : this copies the commit from the <from-branch> to the <to-branch>, does not delete the commit in the <from-branch>
$ git checkout <to-branch> : move back to the <to-branch>
now we need to use git reset to remove the commit in the <from-branch>
- soft reset : $ git reset --soft <hash> : resets back to the <hash> but keeps changes in the staging area. See git status
- mixed reset (default) : $ git reset <hash> : resets back to the <hash> but does not keep changes in the staging area, rather keeps them in working area. See git status
- git reset hard : $ git reset -—hard <hash> : resets back to the <hash> and keeps no changes in the staging or working area. See git status
$ git reflog
$ git checkout <hash-before-reset>
$ git log (and check)
$ git branch <backup-branch> (this creates a new branch with the <hash-before-reset> state)
$ git checkout master (back to master)
$ git branch -a (you have recovered the <backup-branch> if you need it)
$ git log
$ git revert <hash-of-commit-to-revert>
$ git log
$ git diff <hash-of-reverted-commit> <hash-of-new-commit-after-reverting>
$ git stash save "message" : to stash any recent changes made and go back to last committed(?) version
$ git stash list : shows a list of stashed versions
$ git stash apply stash@{#} : choose # from the list. this will go back to the stashed version and keep the stashed version
$ git stash pop : this will go back to the 1st version in the stash list and drop the stash (will not keep it)
$ git stash drop stash@{#} : drops stash # from the list
$ git stash clear : drop all stashes from the list
Free to clone, distribute and reuse.