computing:git:new-repo-from-scratch
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| computing:git:new-repo-from-scratch [2026/08/28 08:22] – created - external edit 127.0.0.1 | computing:git:new-repo-from-scratch [2026/08/30 12:45] (current) – [Upload source tree] oscar | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== GIT ====== | + | ====== |
| ---- | ---- | ||
| - | ==== Getting Started | + | ==== Create Forgejo Repo ==== |
| - | ^Task^Command^ | + | Create the new repository |
| - | |Start a new repo|git init| | + | ==== Upload source tree ==== |
| - | |Clone an existing repo|git clone <url> \\ git clone http:// | + | Now we will prepare and upload |
| - | ==== Configure Git ==== | + | On the local computer enter the directory |
| - | ^Task^Command^ | + | === .gitignore |
| - | |Set a config option |git config user.name 'Your Name' | + | Create in the root of the source tree a .gitignore file and add all the files and folders that need to be excluded from the upload |
| - | |Set a config option |git config user.email 'Your email' | + | === Setup user references |
| - | |Set option system wide in / | + | |
| - | |Set option globally in ~/ | + | |
| - | |Set option locally in the repository | + | === Upload |
| - | |Add an alias|git config alias.st status| | + | |
| - | |See all possible config options| man git-config| | + | |
| - | + | ||
| - | ==== Prepare | + | |
| - | ^Task^Command^ | + | |
| - | |Add untracked file or unstaged changes|git add < | + | |
| - | |Add all untracked files and unstaged changes|git add .| | + | |
| - | |Choose which parts of a file to stage |git add -p| | + | |
| - | |Move file|git mv <old> < | + | |
| - | |Delete file|git rm < | + | |
| - | |Tell Git to forget about a file without deleting it|git rm --cached < | + | |
| - | |Unstage one file|git reset < | + | |
| - | |Unstage everything|git reset| | + | |
| - | |Check what you added|git status| | + | |
| - | |Check what you added, short format|git status -s| | + | |
| - | + | ||
| - | ==== Make Commits ==== | + | |
| - | ^Task^Command^ | + | |
| - | |Make a commit | + | |
| - | |Make a commit|git commit -m ' | + | |
| - | |Commit all unstaged changes|git commit -am ' | + | |
| - | + | ||
| - | ==== Branches ==== | + | |
| - | ^Task^Command^ | + | |
| - | |List branches|git branch| | + | |
| - | |List branches by most recently committed to|git branch --sort=-committerdate| | + | |
| - | |Switch branches|git switch < | + | |
| - | |Create a branch|git branch < | + | |
| - | |Delete a branch|git branch -d < | + | |
| - | |Force delete a branch|git branch -D < | + | |
| - | + | ||
| - | ==== Diff Staged/ | + | |
| - | ^Task^Command^ | + | |
| - | |Diff all staged and unstaged changes|git diff HEAD| | + | |
| - | |Diff just staged changes|git diff --staged| | + | |
| - | |Diff just unstaged changes|git diff| | + | |
| - | + | ||
| - | ==== Diff Commits ==== | + | |
| - | ^Task^Command^ | + | |
| - | |Show diff between a commit and its parent|git show < | + | |
| - | |Diff two commits|git diff < | + | |
| - | |Diff one file since a commit|git diff < | + | |
| - | |Show a summary of a diff|git diff < | + | |
| - | + | ||
| - | ==== Discard Your Changes ==== | + | |
| - | ^Task^Command^ | + | |
| - | |Delete unstaged changes to one file|git restore < | + | |
| - | |Delete all staged and unstaged changes to one file| git restore --staged --worktree < | + | |
| - | |Delete all staged and unstaged changes|git reset --hard| | + | |
| - | |Delete untracked files|git clean| | + | |
| - | |' | + | |
| - | + | ||
| - | ==== Edit History ==== | + | |
| - | ^Task^Command^ | + | |
| - | |" | + | |
| - | |Squash the last 5 commits into one|git rebase -i HEAD~6 \\ Then change " | + | |
| - | |Undo a failed rebase|git reflog BRANCHNAME \\ Then manually find the right commit ID in the reflog, then run: \\ git reset --hard < | + | |
| - | |Change a commit message (or add a file you forgot)|git commit --amend| | + | |
| - | + | ||
| - | ==== Code Archaeology ==== | + | |
| - | ^Task^Command^ | + | |
| - | |Show the files under control|git ls-files| | + | |
| - | |Look at a branch' | + | |
| - | |Show every commit that modified a file| git log < | + | |
| - | |Show every commit that modified a file, including before it was renamed| git log --follow < | + | |
| - | |Find every commit that added or removed some text|git log -G banana| | + | |
| - | |Show who last changed each line of a file|git blame < | + | |
| - | + | ||
| - | ==== Combine Diverged Branches ==== | + | |
| - | ^Task^Command^ | + | |
| - | |Abort a merge|git merge --abort| | + | |
| - | |Combine with Rebase|git switch banana \\ git rebase main \\ \\ Before \\ {{ : | + | |
| - | |Combine with Merge|git switch main \\ git merge banana \\ \\ Before \\ {{ : | + | |
| - | |Combine with Square Merge|git switch main \\ git merge --squash banana \\ git commit \\ \\ Before \\ {{ : | + | |
| - | |Bring a branch up to date with another branch \\ (aka "fast-forward merge")| git switch main \\ git merge banana \\ git commit \\ \\ Before \\ {{ : | + | |
| - | |Copy one commit onto the current branch| git cherry-pick < | + | |
| - | + | ||
| - | ==== Restore an Old File ==== | + | |
| - | ^Task^Command^ | + | |
| - | |Get the version of a file from another commit|git checkout < | + | |
| - | + | ||
| - | ==== Remote Repositories ==== | + | |
| - | ^Task^Command^ | + | |
| - | |Add a Remote|git remote add < | + | |
| - | |Push the main branch | + | |
| - | |Push the current branch to its remote " | + | |
| - | |Push a branch that you've never pushed before|git push -u origin < | + | |
| - | |Force push|git push --force-with-lease| | + | |
| - | |Push tags|git push --tags| | + | |
| - | |Pull changes (but don't change any of your local branches)|git fetch origin main| | + | |
| - | |Pull changes and then rebase your current branch|git pull --rebase| | + | |
| - | |Pull changes and then merge them into your current branch|git pull origin main OR \\ git pull| | + | |
| - | + | ||
| - | ==== Submodules ==== | + | |
| - | === Adding a Submodule === | + | |
| - | Navigate to the root directory | + | |
| - | git submodule add < | + | |
| - | This command adds ‘mysubmodule’ as a submodule to ‘main-project’. It clones ‘mysubmodule’ into a subdirectory of ‘main-project’ | + | |
| - | + | ||
| - | After adding the submodule, you need to commit this change to your repository: | + | |
| - | git commit -m "Added util-methods submodule" | + | |
| - | + | ||
| - | Now, you can import and use function from ' | + | |
| - | + | ||
| - | === Updating a Submodule | + | |
| - | Navigate to the ‘mysubmodule’ directory within ‘main-project’ and pull the latest changes: | + | |
| - | cd mysubmodule | + | |
| - | git pull origin master | + | |
| - | + | ||
| - | After pulling the latest changes, navigate back to the root directory | + | |
| - | cd .. | + | |
| - | git commit -m " | + | |
| - | + | ||
| - | Alternatively, | + | |
| - | + | ||
| - | === Submodule Caveats === | + | |
| - | There are some caveats | + | |
| - | + | ||
| - | * Updating: Submodules do not automatically stay in sync with the remote repository. You need to go into each submodule | + | |
| - | * Committing: If you make changes inside a submodule, you need to commit those changes within the submodule first, then go to the main repository and commit the change to the submodule. This is because the main repository tracks the submodule’s commit. | + | |
| - | * Cloning: When you clone a repository, the submodules will not be cloned along with it. You need to use git submodule update --init --recursive or git clone --recursive < | + | |
| - | * Pulling: Similarly, when you pull your main repository, it will not pull the new commits of populated submodules. Use the --recurse-submodules option with the git pull to update all the submodules as well. | + | |
| - | + | ||
| - | ==== Important Files ==== | + | |
| - | ^Item^Location^ | + | |
| - | |Local | + | |
| - | |Global git config|~/ | + | |
| - | |List of files to ignore|.gitignore| | + | |
| - | + | ||
| - | ==== Merge Conflicts ==== | + | |
| - | Common merge error: | + | |
| - | Auto-merging test.c | + | |
| - | CONFLICT (content): Merge conflict in test.c | + | |
| - | Automatic merge failed; fix conflicts and then commit the result. | + | |
| - | Run command: | + | |
| - | git log --merge --oneline | + | |
| - | ---------------- | + | |
| - | 1d91b52 (bugfix1) 2e bugfix | + | |
| - | 91d17b9 (HEAD -> master) New featurein Master | + | |
| - | 578b59d bugfix in bugfix branch | + | |
| - | + | ||
| - | + | ||
| - | git status | + | |
| - | ------------ | + | |
| - | On branch master | + | |
| - | You have unmerged paths. | + | |
| - | (fix conflicts and run "git commit") | + | |
| - | (use "git merge --abort" to abort the merge) | + | |
| - | Unmerged paths: | + | |
| - | (use "git add < | + | |
| - | both modified: | + | |
| - | Untracked files: | + | |
| - | (use "git add < | + | |
| - | test_BACKUP_29859.c | + | |
| - | test_BASE_29859.c | + | |
| - | test_LOCAL_29859.c | + | |
| - | test_REMOTE_29859.c | + | |
| - | + | ||
| - | Edit the test.c file to resolve the conflict: | + | |
| < | < | ||
| - | nano test.c | + | git init |
| - | ----------- | + | git switch |
| - | + | git add . | |
| - | first version test.c file | + | git commit -m "first commit" |
| - | <<<<<<< | + | |
| - | New Feature added in Master branch | + | |
| - | ======= | + | |
| - | Bugfix1 in bugfix branch | + | |
| - | + | ||
| - | Bugfix2 in bugfix branch | + | |
| - | >>>>>>> | + | |
| </ | </ | ||
| - | + | * git init: will prepare local repository by creating the .git directory | |
| - | + | * git switch: will create the main branch and switches the local repo to it. | |
| - | ==== Links ==== | + | * git add . : will state all files to the local staging area |
| - | * [[https://git-scm.com/cheat-sheet|Cheat Sheet]] | + | * git commit: will commit all staged file in the local repo |
| + | === Upload to remote repo === | ||
| + | < | ||
| + | git remote add origin http:// | ||
| + | git push -u origin main | ||
| + | </ | ||
| + | * git remote add: will add and connect a remote server/repository to our local git. The will be called and references as: " | ||
| + | * git push: will push all committed files to the remote called " | ||
| + | === Authorization === | ||
| + | The "git push -u origin main" command will prompt for a username/ | ||
computing/git/new-repo-from-scratch.1787905323.txt.gz · Last modified: by 127.0.0.1
