User Tools

Site Tools


computing:git

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
computing:git [2026/04/26 10:48] oscarcomputing: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 <url>|+|Clone an existing repo|git clone <url> \\ git clone http://192.168.178.96:3000/<Repository Name>|
 ==== Configure Git ==== ==== Configure Git ====
 ^Task^Command^ ^Task^Command^
 |Set a config option |git config user.name 'Your Name'| |Set a config option |git config user.name 'Your Name'|
 |Set a config option |git config user.email 'Your email'| |Set a config option |git config user.email 'Your email'|
-|Set option globally |git config --global ... |+|Set option system wide in /etc/gitconfig |git config --system ... | 
 +|Set option globally in ~/.gitconfig |git config --global ... | 
 +|Set option locally in the repository .git/config file |git config --local ... |
 |Add an alias|git config alias.st status| |Add an alias|git config alias.st status|
 |See all possible config options| man git-config| |See all possible config options| man git-config|
Line 25: 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 32: Line 35:
 |Commit all unstaged changes|git commit -am 'message'| |Commit all unstaged changes|git commit -am 'message'|
  
-==== Move Between Branches ====+==== Branches ====
 ^Task^Command^ ^Task^Command^
-|Switch branches|git switch <name> OR git checkout <name>| 
-|Create a branch|git switch -c <name> OR git checkout -b <name>| 
 |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 <name> OR \\ git checkout <name>|
 +|Create a branch|git branch <name> OR \\ git switch -c <name> OR \\ git checkout -b <name>|
 |Delete a branch|git branch -d <name>| |Delete a branch|git branch -d <name>|
 |Force delete a branch|git branch -D <name>| |Force delete a branch|git branch -D <name>|
Line 71: Line 74:
 ==== Code Archaeology ==== ==== Code Archaeology ====
 ^Task^Command^ ^Task^Command^
-|Look at a branch's history| git log main \\ git log --graph main \\ git log --oneline|+|Show the files under control|git ls-files| 
 +|Look at a branch's history| git log main \\ git log --all --graph \\ git log --graph <main\\ git log --oneline|
 |Show every commit that modified a file| git log <file>| |Show every commit that modified a file| git log <file>|
 |Show every commit that modified a file, including before it was renamed| git log --follow <file>| |Show every commit that modified a file, including before it was renamed| git log --follow <file>|
Line 79: 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 \\ {{ :computing:git-combine-rebase-before.svg?200 |}} \\ After \\ {{ :computing:git-combine-rebase-after.svg?200 |}}| |Combine with Rebase|git switch banana \\ git rebase main \\ \\ Before \\ {{ :computing:git-combine-rebase-before.svg?200 |}} \\ After \\ {{ :computing:git-combine-rebase-after.svg?200 |}}|
 |Combine with Merge|git switch main \\ git merge banana \\ \\ Before \\ {{ :computing:git-combine-merge-before.svg?200 |}} \\ After \\ {{ :computing:git-combine-merge-after.svg?200 |}}| |Combine with Merge|git switch main \\ git merge banana \\ \\ Before \\ {{ :computing:git-combine-merge-before.svg?200 |}} \\ After \\ {{ :computing:git-combine-merge-after.svg?200 |}}|
Line 100: 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 <mysubmodule-url>
 +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 'mysubmodule' in your 'main-project' just like you would with any local module.
 +
 +=== 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 "Updated mysubmodule submodule"
 +
 +Alternatively, you can use the git submodule update --remote command from the root directory of 'main-project' to update all submodules to their latest commiton their master branches. Remember to commit after running this command as well.
 +
 +=== 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 <repository> to clone the repository and its submodules.
 +  * 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 106: Line 141:
 |Global git config|~/.gitconfig| |Global git config|~/.gitconfig|
 |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" to abort the merge)
 +  Unmerged paths:
 +    (use "git add <file>..." to mark resolution)
 +   both modified:   test.c
 +  Untracked files:
 +    (use "git add <file>..." to include in what will be committed)
 +   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:
 +<code>
 +nano test.c
 +-----------
 +
 +first version test.c file
 +<<<<<<< HEAD
 +New Feature added in Master branch
 +=======
 +Bugfix1 in bugfix branch
 +
 +Bugfix2 in bugfix branch
 +>>>>>>> bugfix1
 +</code>
 +
  
 ==== Links ==== ==== Links ====
   * [[https://git-scm.com/cheat-sheet|Cheat Sheet]]   * [[https://git-scm.com/cheat-sheet|Cheat Sheet]]
computing/git.1777200492.txt.gz · Last modified: by oscar