computing:git
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| computing:git [2026/04/26 09:56] – created 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^ | ||
| |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 / |
| + | |Set option globally | ||
| + | |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| | ||
| + | |||
| + | ==== Prepare to Commit ==== | ||
| + | ^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 (and open text editor to write message)|git 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 " | ||
| + | |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 to the remote origin|git push origin main| | ||
| + | |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 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 ==== | ||
| + | ^Item^Location^ | ||
| + | |Local git config|.git/ | ||
| + | |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" | ||
| + | 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 ==== | ||
| + | * [[https:// | ||
computing/git.1777197390.txt.gz · Last modified: by oscar
