Git | GitHub
|
|
Local repo | |
git init | Initialize git repository |
git add . | Track all | git rm --cached file1 | Untrack file1 |
git status | Check status | git commit -m "Message" | "-am" to bypass stage and commit directly |
vi file1 | Edit |
git diff | Check diff before staged |
git add file1 | Stage changes to commit | git diff --cached | Check diff after staged |
git restore --staged file1 | Take off from stage and keep editing | git restore file1 | Discard changes and restore as it was. |
git reset [COMMIT] | This will take us to the specific commit Stage area (Past or Future). Then we can restore our file version from there using 'git restore file1' | git revert [LastCommit] --no-edit | This will undoes the changes made by the [LastCommit]. By default git revert opens the editor to let you modify commit message, so use --no-edit to skip. |
Config |
git config --global user.name "your_name" |
git config --global user.email "your_email" |
git config --global init.default branch main |
git config --list |
Branch | |
git branch NewBranch | Create a branch |
git checkout [branch] | Switch branch |
OR, git checkout -b branch | Create and switch |
git branch | List branches. '-a' includes remote branches |
git branch -M NewName | Raname current branch |
git merge [branch] | Merge branch with the current branch |
git branch -d [branch] | Delete branch. '-D' for force delete. |
Remote | |
git remote add origin [URL] | Link remote repo with Local as 'origin' |
git remote set-url origin https://[TOKEN]@[URL] | Authenticate remote repo with token |
git push -u origin | Push changes to remote repo |
git push origin | pull changes from remote repo |
git fetch origin | Fetch changes made on remote origin to local metada. To merge that changes in local repo use 'git pull origin' or 'git merge origin/main' |
git clone [REPO / URL] | Clone / Download a repository onto local machine. |
git fetch --prune origin | This fetches changes from the remote repo and prunes any stale/deleted branches on the remore. |
git remote -v | View remote repositories |
git remote rm [origin] | Remove remote repo name |
Log & View | |
git log | git log --oneline | git log -p | git log -2 | git log --grep <keyword> | git reflog | |
git log --all | List all commits from all branches |
git show <COMMIT>, <object>, <tag>, or <keyword> | |
git clean -n -f -x | (-n) check any unnecessary files in current dir. (-f) delete them. (-x) clean .ignore |
Help | |
git help COMMAND | Show the command manual |
git COMMAND -h | Show Options and usage of the Command |
Other | |
git tag -a important -m "Message" ID | Tag as important |
git tag -d important | Delete tag |
Notes | |
cat > .gitignore | File names mention inside the .gitignore will not be exposed to staging and remain secret. |
There're Three different environments: 1. WorkSpace (Working directory) 2. Staging (Index) 3. Repository (Commit) | |
Methodes to authenticate remote repository: 1. SSH Keys 2. Personal Access Token (PAT) for HTTPs authentication 3. Two-Factor Authenticaiton |