Git¶
Git Shortcuts¶
On *nix
:
Command | Description |
---|---|
git init |
initialize a repository |
git add * |
add all files in a directory |
git add . |
add the whole directory & subdirectories |
git rm foo |
stage the file for deletion (even a file already deleted) |
git rm --cached foo |
stage the file for deletion without deleting it from the file system |
git add -u |
This will stage all the changes in files that are already tracked, whether deleted or just modified. This helps when you delete multiple files and don't want to stage them individually. |
git add -A . |
will add all changes |
git commit -a |
This will automatically take these changes (and any others) and commit them. |
git config -l |
display configuration for current repo |
git log |
show log of commits |
git log -p |
show log of commits with additional details (diff of files) 'p' stands for patch |
git log --stat |
show log of commits with additional details (diff of files) |
git show <COMMIT_ID> |
displays details of a specific commmit |
git diff |
same as the shell diff -u command for unstaged files. can pass one file as parameter to only see diffs for that file |
git diff --staged |
same as git diff but for staged files |
git add -p |
display changes before adding and gives option to commit or not |
git rm FILENAME |
stops a file from being tracked by git & removes it from the git directory |
git mv OLD_NAME NEW_NAME |
rename or move a file |
git checkout <FILE> |
restore a file/directory to a previous state (from staging area) |
git restore <FILE> |
restore a file to a previous state (from staging area) |
git reset HEAD <FILE> |
unstage a file by comparing to the current working branch HEAD |
git reset -p |
get git to ask you which changes you want to reset |
git commit --amend |
overwrites the previous commit (e.g. with a new commit message) NOTE only use on local commits, never on the server |
git revert HEAD |
rollback the last commit in the current branch (HEAD alias) |
git revert <COMMIT_ID> |
roll back / revert the commit with id specified (even if it is not the most recent one) |
git branch |
list branches |
git branch <BRANCH_NAME> |
switch to branch called BRANCH_NAME |
git branch -b <BRANCH_NAME> |
create a branch called BRANCH_NAME and switch to it |
git branch -d <BRANCH_NAME> |
delete branch |
git branch -r |
display remote branches that git is currently tracking |
git merge <BRANCH_NAME> |
merge BRANCH_NAME into the current branch |
git merge origin/master |
this will merge the "master" branch of the "origin" repo (remote repo) into the local branch |
git clone <URL> |
clone a remote repository into a local workspace |
git push |
push commits from your local repo to a remote repo |
git pull |
fetch the newest updates from a remote repository |
git fetch |
copies the commits done in the report repository to the remote branches, so that we can see what has been committed in those |
git remote show origin |
dispay info about the remote repo |
git init --separate-git-dir=/path |
initialize git repo in the directory specified can also be used to move the .git directory to another |
NOTE: git fetch
vs git pull
:
- git fetch
fetches remote updates but doesn't merge
- git pull
fetches remote updates and merges.
GitHub cli and 2FA¶
FROM : Using command-line git with GitHub 2FA
Create a Personal Access Token¶
- Go to https://github.com/settings/tokens
- click “Generate new token”.
- Provide a Token description, i.e. “git @my-linux-laptop”
- Select scopes. Select “repo” to have full control of private repositories
- Click “Generate token”
cli¶
- do a github push from the cli
- when asked for username, enter u/n normally
- instead of the password, paste in the generated token
to avoid having to having to paste the token every time¶
Do either of:
- Enable Git credentials cache (recommended)
- Alternative: Enable Git credentials store (NOTE: this stores the credentials unencrypted in your home directory)