User Tools

Site Tools


computing:git

GIT


Getting Started

TaskCommand
Start a new repogit init
Clone an existing repogit clone <url>
git clone http://192.168.178.96:3000/<Repository Name>

Configure Git

TaskCommand
Set a config option git config user.name 'Your Name'
Set a config option git config user.email 'Your email'
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 aliasgit config alias.st status
See all possible config options man git-config

Prepare to Commit

TaskCommand
Add untracked file or unstaged changesgit add <file>
Add all untracked files and unstaged changesgit add .
Choose which parts of a file to stage git add -p
Move filegit mv <old> <new>
Delete filegit rm <file>
Tell Git to forget about a file without deleting itgit rm –cached <file>
Unstage one filegit reset <file>
Unstage everythinggit reset
Check what you addedgit status
Check what you added, short formatgit status -s

Make Commits

TaskCommand
Make a commit (and open text editor to write message)git commit
Make a commitgit commit -m 'message'
Commit all unstaged changesgit commit -am 'message'

Branches

TaskCommand
List branchesgit branch
List branches by most recently committed togit branch –sort=-committerdate
Switch branchesgit switch <name> OR
git checkout <name>
Create a branchgit branch <name> OR
git switch -c <name> OR
git checkout -b <name>
Delete a branchgit branch -d <name>
Force delete a branchgit branch -D <name>

Diff Staged/Unstaged Changes

TaskCommand
Diff all staged and unstaged changesgit diff HEAD
Diff just staged changesgit diff –staged
Diff just unstaged changesgit diff

Diff Commits

TaskCommand
Show diff between a commit and its parentgit show <commit>
Diff two commitsgit diff <commit> <commit>
Diff one file since a commitgit diff <commit> <file>
Show a summary of a diffgit diff <commit> –stat
git show <commit> –stat

Discard Your Changes

TaskCommand
Delete unstaged changes to one filegit restore <file> OR
git checkout <file>
Delete all staged and unstaged changes to one file git restore –staged –worktree <file> OR
git checkout HEAD <file>
Delete all staged and unstaged changesgit reset –hard
Delete untracked filesgit clean
'Stash' all staged and unstaged changesgit stash

Edit History

TaskCommand
“Undo” the most recent commit (keep your working directory the same)git reset HEAD^
Squash the last 5 commits into onegit rebase -i HEAD~6
Then change “pick” to “fixup” for any commit you want to combine with the previous one
Undo a failed rebasegit reflog BRANCHNAME
Then manually find the right commit ID in the reflog, then run:
git reset –hard <commit>
Change a commit message (or add a file you forgot)git commit –amend

Code Archaeology

TaskCommand
Show the files under controlgit 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, including before it was renamed git log –follow <file>
Find every commit that added or removed some textgit log -G banana
Show who last changed each line of a filegit blame <file>

Combine Diverged Branches

TaskCommand
Abort a mergegit merge –abort
Combine with Rebasegit switch banana
git rebase main

Before

After
Combine with Mergegit switch main
git merge banana

Before

After
Combine with Square Mergegit switch main
git merge –squash banana
git commit

Before

After
Bring a branch up to date with another branch
(aka “fast-forward merge”)
git switch main
git merge banana
git commit

Before

After
Copy one commit onto the current branch git cherry-pick <commit>

Before

After

Restore an Old File

TaskCommand
Get the version of a file from another commitgit checkout <commit> <file> OR
git restore <file> –source <commit>

Remote Repositories

TaskCommand
Add a Remotegit remote add <name> <url>
Push the main branch to the remote origingit push origin main
Push the current branch to its remote “tracking branch”git push
Push a branch that you've never pushed beforegit push -u origin <name>
Force pushgit push –force-with-lease
Push tagsgit push –tags
Pull changes (but don't change any of your local branches)git fetch origin main
Pull changes and then rebase your current branchgit pull –rebase
Pull changes and then merge them into your current branchgit 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

ItemLocation
Local git config.git/config
Global git config~/.gitconfig
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:

nano test.c
-----------

first version test.c file
<<<<<<< HEAD
New Feature added in Master branch
=======
Bugfix1 in bugfix branch

Bugfix2 in bugfix branch
>>>>>>> bugfix1
computing/git.txt · Last modified: by oscar