Common workflows
standard workflow
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.
working directory to staged
this adds both untracked files and modified files to the index. They become staged files.
git add .
We may exclude untracked files, that is, keep untracked files untracked.
git add -u
This is mostly when we do not control the .gitignore and we have local files that we do not want to delete because they serve a purpose, but also do not want to be tracked by git because they are personal or not accepted by the maintainer.
commit to staged: cleanup 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
staged to working directory
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 implictly 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
commit to working directory
we may use the restore
command and set the --source
flag to HEAD.
git restore --worktree --source=HEAD .
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
committed to staged AND working directory
this reverts both the index and the working directory to the commit version.
It does not affect untracked files, which is a good seperation of concern, and allows to keep them outside of the git ecosystem.
git reset --hard
git restore --source=HEAD --staged --worktree .
staged to commit
save the staging area as a new commit. It has no effect over the working directory.
git commit -m "..."
we may add modified files and commit them in one command. This has not effect over untracked files, which is different from the usual git add .
git commit -am "initial commit"
we may change the message of the last commit - also change the hash
git commit --amend -m "an updated commit message"
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
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