Git
Contents
About Git
Git is an open-source distributed version control system widely used for tracking changes in source code during software development. Git allows the creation of multiple branches within a project, enabling developers to work on features, fix bugs, or experiment in isolated environments. Branches can be merged back into the main branch once the work is complete.
People who use Git usually use it via GitHub - a web-based platform that provides hosting for software development and version control using Git. It offers a range of features and functionalities that support collaboration among developers.
This page just focuses on git commands and principles.
Some Git Principles
Git has three different environments:
- Working Files - can make edit to files.
- Staging Area - A holding pen or "index" until files are ready to commit.
- Commit - Entered into history books.
- You have to add files before they are "tracked".
- You can create a .gitignore in your main repo to say which files git should ignore (eg: files with secret passwords etc).
Common Workflow of Git Commands
Initialize Git With A New Local Repo
$ git config --global user.name "Andrew Brett"
$ git config --global user.email "andrew.brett@something.io"
$ git config --global init.default branch main
$ cd c:/users/me/code_folder
$ git init
Track Files
$ git status
# Shows "untracked files".
$ git add changed_file.html
$ git status
$ git diff
To remove a file..:
$ git rm --cached changed_file.html
Then you probably want to create a ".gitignore" file in your main repo with the content like:
# Read all the syntax via https://git-scm.com/docs/gitignore
confidendial_info.txt
Commit Changes
$ git commit -m "first commit"
$ git add index.htm
$ git restore --staged index.htm
$ git commit -a -m "update text skip staging and commit".
Rename a File
$ git mv "my_logo.png" "primary_logo.png"
$ git commit -m "change file name of an image"
See Historical Commits
$ git log
For an abridged one-line version: `git log --oneline` ... and to see all the details `git log -p`.
NOTE: There is a nice '--amend' option to change a description.
Jump Back to Previous Commit
$ git reset fk489c8 <COMMIT_HASH_ID>
And for the more advanced option `git rebase` (can merge commits etc).
Fetch the Latest and Create a New Branch Off "staging"
Let say we want to update (fetch) the latest changes to "staging" branch then branch off staging:
git fetch origin
git checkout staging
git pull origin staging
git checkout -b staging-new-branch-name
To Revert Changes - To Go Back to A Previous Commit.
Type in "git log" to see the hash of the commit you want to revert to... say `6a31a9204fa8230a83dd7e89546eda66c60f6d27`... then run:
git reset --hard 6a31a9204fa8230a83dd7e89546eda66c60f6d27
git push --force origin staging-noske-preference-ui
The FORCE is dangerous because you will lose all commits after this... so maybe make a local back up of files first.
Misc
Sometimes you might need to rebase, which is a long process... you have to go:
$ git rebase staging
# Then it will show you conflicts and "repo git:(9131c6b) ✗ git status" -> go to your code editor and for each commit you may have to "Accept Incoming"... and finally that is done. Then you might need this command:
$ git push origin staging-noske-preference-ui --force-with-lease
Create a New Branch
$ git branch FixTemp
... to see what branches we have.. then switch to the next one:
$ git branch
$ git switch FixTemp
... make change to a file. now we want to merge them back in.
$ git switch main
$ git merge -m "Merge fixtemp back to main" FixTemp
... and now we can delete the old branch:
$ git branch -d FixTemp
... If you get a conflict you can go into the file and see the conflict regions.
See Also
- GitHub - Pretty much essential for working with teams on git.
Links
- Adobe LiveDocs - Working with file upload and download - has a nice long explanation of how AC3 and HTTP POST request work.