git config --global user.email [email protected]
git config --global user.name username
git config --global --edit
On the root of your project:
git config --edit
Git aliases are great to shorten long git commands. This is a standard format on how to make a git alias:
git config --global alias.<command_alias_here> <git_command_expression>
Examples:
git config --global alias.unstage 'reset HEAD --'
git config --global alias.logp 'log --graph --decorate --pretty'
git config --global alias.lastc 'log --graph --decorate --pretty -1 HEAD'
You may edit them by Editing the Git Global Config if you choose to do so.
Running Git aliases are the same as running them as standard git commands. From the previous examples, they can be executed as follows:
git unstage # Equivalent to `git reset HEAD --`
git logp # Equivalent to `git log --graph --decorate --pretty`
git lastc # Equivalent to `git log --graph --decorate --pretty -1 HEAD`
On the root of your empty project:
git init
git clone <uri>.git
- Untracked/Unstaged Phase -- All new files and newly modified files are located here, ready for staging.
- Staging Phase -- All files that are staged are ready to commit.
- Snapshot Phase -- Finalised commits that are located in a branch or in multiple branches.
For all untracked/modified files:
git add .
For specific files:
git add ./relative/path/to/file.ext
For all tracked files:
git reset .
git restore --staged .
For specific files:
git reset ./relative/path/to/file.txt
git restore --staged ./relative/path/to/file.txt
For Multiline commit:
NOTE: Must set default text editor, either CLI or VSCode
git commit
Shorthand/Single Line Commit:
git commit -m "<commit message here>"
git reset --soft HEAD~1
git log
NOTE: additional flags may be used to enhance the output of
git log
. Typical flags may be--pretty
,--graph
, or--decorate
.
git remote add <remote_alias> <remote_uri>
git remote
or
git remote ls
or
git remote -v
for verbose mode (it will show the git remote URI)
git remote rm <remote_alias>
git remote rename <old_alias> <new_alias>
Push from default upstream branch (default is master/main, depending on the current branch name set. If you don't know what current branch you're on, refer to Show All Current Branches.)
git push
Push and set default upstream branch
git push -u <remote_name> <new_upstream_branch_name>
Push specific branch
git push <remote_name> <branch_name>
Pull from default upstream branch, or current branch. (it may prompt you to set a default upstream branch if you do this command.)
git pull
Pull from specific branch from remote repo
git pull <remote_name> <branch_name>
Pull and set default upstream branch
git pull -u <remote_name> <branch_name>
git branch <new_branch_name>
git checkout <new_branch_name>
git checkout -b <new_branch_name>
git branch
or
git branch ls
git branch -d <branch_name>
if there are unmerged changes in current branch, above command won't work, use this instead:
git branch -D <branch_name>
NOTE: you must be on the current branch you want to rename.
git branch -m <new_branch_name>
NOTE: you must be on the current branch you want to merge to.
git merge <branch_name>
- Always add a
.gitignore
file if you want git to skip file/s or folder/s. Here's an example.gitignore
file template for a Node.js project:# Logs logs *.log npm-debug.log* yarn-debug.log* yarn-error.log* lerna-debug.log* .pnpm-debug.log* # Diagnostic reports (https://nodejs.org/api/report.html) report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json # Runtime data pids *.pid *.seed *.pid.lock # Directory for instrumented libs generated by jscoverage/JSCover lib-cov # Coverage directory used by tools like istanbul coverage *.lcov # nyc test coverage .nyc_output # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) .grunt # Bower dependency directory (https://bower.io/) bower_components # node-waf configuration .lock-wscript # Compiled binary addons (https://nodejs.org/api/addons.html) build/Release # Dependency directories node_modules/ jspm_packages/ # Snowpack dependency directory (https://snowpack.dev/) web_modules/ # TypeScript cache *.tsbuildinfo # Optional npm cache directory .npm # Optional eslint cache .eslintcache # Optional stylelint cache .stylelintcache # Microbundle cache .rpt2_cache/ .rts2_cache_cjs/ .rts2_cache_es/ .rts2_cache_umd/ # Optional REPL history .node_repl_history # Output of 'npm pack' *.tgz # Yarn Integrity file .yarn-integrity # dotenv environment variable files .env .env.development.local .env.test.local .env.production.local .env.local # parcel-bundler cache (https://parceljs.org/) .cache .parcel-cache # Next.js build output .next out # Nuxt.js build / generate output .nuxt dist # Gatsby files .cache/ # Comment in the public line in if your project uses Gatsby and not Next.js # https://nextjs.org/blog/next-9-1#public-directory-support # public # vuepress build output .vuepress/dist # vuepress v2.x temp and cache directory .temp .cache # Docusaurus cache and generated files .docusaurus # Serverless directories .serverless/ # FuseBox cache .fusebox/ # DynamoDB Local files .dynamodb/ # TernJS port file .tern-port # Stores VSCode versions used for testing VSCode extensions .vscode-test # yarn v2 .yarn/cache .yarn/unplugged .yarn/build-state.yml .yarn/install-state.gz .pnp.*
- If you want to keep empty folders, add a
.gitkeep
file inside the folder so that git can see and track changes from that folder even though it is empty.