User Tools

Site Tools


computing:git

This is an old revision of the document!


GIT


Getting Started

TaskCommand
Start a new repogit init
Clone an existing repogit clone <url>

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'

Move Between Branches

TaskCommand
Switch branchesgit switch <name> OR
git checkout <name>
Create a branchgit branch <name> OR
git switch -c <name> OR
git checkout -b <name>
List branchesgit branch
List branches by most recently committed togit branch –sort=-committerdate
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 –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

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.1777230783.txt.gz · Last modified: by oscar