Common commands
the standard workflow is as follows:
- make some changes to one or more files in the working directory
- select one or more changes, put them in the staging area
- save the staging area as a commit.
standard flow
stage files: working-directory to index
git add adds one or more files to the staging area. It affects both untracked files and modified files, turning them into staged files.
git add .
git add -u
We may exclude untracked files from staging with -u. A more permanent solution is to add them to .gitignore.
commit: index to commit
Save the staging area as a new commit. It has no effect over the working directory:
git commit -m "..."
We may edit the last commit message with --amend. This does not create a new commit.
git commit --amend -m "an updated commit message"
Note: the -am flag is not a shortcut for both git add . and git commit -m '', since it does not add untracked files.
git commit -am "adding and committing modified files"
reverting to the last commit
The last commit is commonly represented by HEAD.
commit to working-directory: reset the working-directory.
we use restore and set the --source to HEAD.
git restore --source=HEAD --worktree .
git restore --source=HEAD .
old syntax
git checkout HEAD -- . # the last commit
git checkout v1.2.3 -- . # tag v1.2.3
git checkout stable -- file # stable branch
git checkout origin/master -- file # upstream master
git checkout HEAD^ -- file # the version before the last commit
commit to working-directory AND index.
We aim to revert both the index and the working directory to the commit version.
It does not affect or remove untracked files, since they are ignored by git.
git restore --source=HEAD --staged --worktree .
git reset --hard
commit to index: reset the staging area
We may revert the staging area to the clean state of the previous commit. The working tree is unaffected.
We use the restore command, with the staged flag.
git restore --staged . # tested
git reset .
If we are before the first commit, we may remove content from the staging area:
git rm --cached file
git reset
reverting to the index version
index to working-directory: go back to the index checkpoint
We may restore the worktree from the index. This may restore a checkpoint or the last commit, depending on the content of the staging area.
The restore and checkout command implicitly restore the worktree from the staged area. We may make it more explicit by setting flags.
git restore .
git restore --worktree . #same
git restore file
git checkout .
git checkout main.js
reverting to older commits
revert the current branch to a previous commit
Restaurer la version de travail, la version staged et la version sauvegardée depuis la sauvegarde précédente nommée explicitement ou relativement par le nombre de commit depuis cette sauvegarde.
revenir au commit spécifié explicitement.
git reset --hard 2bje9dlhash
revenir au commit à une distance de N par rapport au dernier commit, ici l'avant dernier commit.
git reset --hard HEAD^1
other commands
remove untracked files
delete untracked files
git clean -fd
delete untracked files and files present but ignored by git
git clean -fdx
dry run
git clean -fdn
git clean -fdxn