computing:git
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| computing:git [2026/04/26 17:43] – [Code Archaeology] oscar | computing:git [2026/04/27 18:54] (current) – oscar | ||
|---|---|---|---|
| Line 5: | Line 5: | ||
| ^Task^Command^ | ^Task^Command^ | ||
| |Start a new repo|git init| | |Start a new repo|git init| | ||
| - | |Clone an existing repo|git clone < | + | |Clone an existing repo|git clone <url> \\ git clone http:// |
| ==== Configure Git ==== | ==== Configure Git ==== | ||
| ^Task^Command^ | ^Task^Command^ | ||
| Line 27: | Line 27: | ||
| |Unstage everything|git reset| | |Unstage everything|git reset| | ||
| |Check what you added|git status| | |Check what you added|git status| | ||
| + | |Check what you added, short format|git status -s| | ||
| ==== Make Commits ==== | ==== Make Commits ==== | ||
| Line 34: | Line 35: | ||
| |Commit all unstaged changes|git commit -am ' | |Commit all unstaged changes|git commit -am ' | ||
| - | ==== Move Between | + | ==== Branches ==== |
| ^Task^Command^ | ^Task^Command^ | ||
| - | |Switch branches|git switch < | ||
| - | |Create a branch|git switch -c < | ||
| |List branches|git branch| | |List branches|git branch| | ||
| |List branches by most recently committed to|git branch --sort=-committerdate| | |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 < | |Delete a branch|git branch -d < | ||
| |Force delete a branch|git branch -D < | |Force delete a branch|git branch -D < | ||
| Line 74: | Line 75: | ||
| ^Task^Command^ | ^Task^Command^ | ||
| |Show the files under control|git ls-files| | |Show the files under control|git ls-files| | ||
| - | |Look at a branch' | + | |Look at a branch' |
| |Show every commit that modified a file| git log < | |Show every commit that modified a file| git log < | ||
| |Show every commit that modified a file, including before it was renamed| git log --follow < | |Show every commit that modified a file, including before it was renamed| git log --follow < | ||
| Line 82: | Line 83: | ||
| ==== Combine Diverged Branches ==== | ==== Combine Diverged Branches ==== | ||
| ^Task^Command^ | ^Task^Command^ | ||
| + | |Abort a merge|git merge --abort| | ||
| |Combine with Rebase|git switch banana \\ git rebase main \\ \\ Before \\ {{ : | |Combine with Rebase|git switch banana \\ git rebase main \\ \\ Before \\ {{ : | ||
| |Combine with Merge|git switch main \\ git merge banana \\ \\ Before \\ {{ : | |Combine with Merge|git switch main \\ git merge banana \\ \\ Before \\ {{ : | ||
| Line 103: | Line 105: | ||
| |Pull changes and then rebase your current branch|git pull --rebase| | |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| | |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 of ‘main-project’ and run the following command: | ||
| + | git submodule add < | ||
| + | This command adds ‘mysubmodule’ as a submodule to ‘main-project’. It clones ‘mysubmodule’ into a subdirectory of ‘main-project’ and creates a special file called **.gitmodules**. This file tracks the mapping between the project URL and the local subdirectory you've pulled it into. | ||
| + | |||
| + | 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 of ‘main-project’ and commit this update: | ||
| + | cd .. | ||
| + | git commit -m " | ||
| + | |||
| + | Alternatively, | ||
| + | |||
| + | === Submodule Caveats === | ||
| + | There are some caveats and things to keep in mind when using Git submodules: | ||
| + | |||
| + | * Updating: Submodules do not automatically stay in sync with the remote repository. You need to go into each submodule and pull the changes manually. | ||
| + | * 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 ==== | ==== Important Files ==== | ||
| Line 109: | Line 141: | ||
| |Global git config|~/ | |Global git config|~/ | ||
| |List of files to ignore|.gitignore| | |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" | ||
| + | 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 | ||
| + | ----------- | ||
| + | |||
| + | first version test.c file | ||
| + | <<<<<<< | ||
| + | New Feature added in Master branch | ||
| + | ======= | ||
| + | Bugfix1 in bugfix branch | ||
| + | |||
| + | Bugfix2 in bugfix branch | ||
| + | >>>>>>> | ||
| + | </ | ||
| + | |||
| ==== Links ==== | ==== Links ==== | ||
| * [[https:// | * [[https:// | ||
computing/git.1777225412.txt.gz · Last modified: by oscar
