Git review.
This is a review of Git commands.
Git commands review
Basics
-
Configuration:
- View user name / email:
git config user.name/git config user.email - Set user / email globally:
git config --global user.name "My name"/git config --global user.email "myemail@providermail.com"
- View user name / email:
-
View the current status:
git status -
Initialize a repository:
git init -
Basic workflow
- 1: add
- Add specific files to the staged area:
git add file1.txt file2.txt - Or add all:
git add .
- Add specific files to the staged area:
- 2: Commit:
- Commit with a message:
git commit -m "start creating two sample files"
- Commit with a message:
- We can also add everything and commit with a message in one command:
git commit -am "first commit"
- 1: add
-
View log:
- Display a list of the commits:
git log - In one line
git log --oneline
- Display a list of the commits:
-
Amend:
- Amend without editing the message
git commit --amend --no-edit - Amend the message
git commit --amend -m "my new message"
- Amend without editing the message
-
Branches:
-
View all branches:
git branch -
Create / Switch:
- Create a branch:
git branch bugfix - Switch to a branch:
git switch bugfix - Create and switch to a branch:
git switch -c bugfix - Old command:
git checkout -b bugfix
- Create a branch:
-
Delete a branch:
- 1: Move to other branch:
git switch main - 2: Delete the branch:
git branch -d bugfix
- 1: Move to other branch:
-
Rename a branch:
- 1: Switch to the branch:
git switch bugfix - 2: Rename it:
git branch -m errorfix
- 1: Switch to the branch:
-
Merge a branch:
- 1: Go to the receiving branch:
git switch main - 2: Merge the desired branch into the actual branch:
git merge bugfix
- 1: Go to the receiving branch:
-
-
See changes:
- In the working directory, changes that are not staged:
git diff - Since the last commit (regardless of whether it is staged or not):
git diff HEAD - For the staged:
git diff --staged - We can add a filename to the previous commands to limit to a specific file:
git diff HEAD colors.txt
- List the changes between the tip of branch1 and branch2:
git diff branch1 branch2 - List the changes between two commits:
git diff 45bdd51 f3eaa49
- In the working directory, changes that are not staged:
-
Stash:
- To save:
git stash - Restore and remove from the stash:
git stash pop - Other stash commands that are less used:
- Restore but keep a copy in the stash (for example, to apply the stash to multiple branches):
git stash apply - If we keep more than one stashed thing:
git stash list - Apply a specific stash:
git stash apply stash@{1} - Delete a specific stash:
git stash drop stash@{1} - Delete all the stash:
git stash clear
- Restore but keep a copy in the stash (for example, to apply the stash to multiple branches):
- To save:
-
Navigating the commit history:
-
1: move HEAD to a specific commit
git checkout <commit-hash> -
2: then create a branch from this commit, and we can make changes in the new branch
-
Another way to move HEAD:
- to move HEAD one commit back:
git checkout HEAD~1 - … two commits back:
git checkout HEAD~2 - … three commits back:
git checkout HEAD~3 - …
- to move HEAD one commit back:
-
After moving HEAD to a specific commit, we can return to the last commit of the branch using:
git switch - -
Discarding modified changes:
git checkout HEAD file1.txt- other method:
git checkout -- file1.txt
-
-
Restoring things:
- Git restore (same to the previous git commands)
git restore file1.txt
- To restore a file to what it was in a specific commit:
- 1: we can do (we can put the commit hash instead of HEAD~1):
git restore --source HEAD~1 file1.txt - 2: then we can restore the file to the latest version:
git restore file1.txt
- 1: we can do (we can put the commit hash instead of HEAD~1):
- Unstaging files
git restore --staged file1.txt
- Git reset
- remove the commits but not the changes:
git reset <commit-hash>- This is useful when we have made changes on the wrong branch. First we delete the commits without losing the changes, and then we create a new branch.
- remove the commits and the changes:
git reset --hard <commit-hash>
- remove the commits but not the changes:
- Git revert
- It is similar to reset, but it creates a new commit without removing any previous commits.
- It is good because it does not alter the Git history, especially when we are collaborating with other people.
- It can produce conflicts, and then you have to resolve them to make the new commit.
git revert <bad-commit-hash-that-we-want-to-revert>
- Git restore (same to the previous git commands)
-
Working with the remote
- Git remote:
- View the current remotes:
git remote -v - Add a remote with the name origin:
git remote add origin <repo-url> - Rename the branch where we are to:
git branch -M main - Push a branch:
git push origin main - Set the upstream:
- 1: remember that the main branch of my local repo is connected with the main branch of the origin:
git push -u origin main - 2: then we can do
git push
- 1: remember that the main branch of my local repo is connected with the main branch of the origin:
- View remote branches:
git branch -r - To get locally a branch that is in remote but not in local (if the branch name exists in remote it will get the branch):
git switch branchname
- View the current remotes:
- Fetch:
- Download the latest changes of a branch without affecting the working directory:
git fetchorgit fetch origin - You can indicate a specific branch:
git fetch origin/responsivegui - Then we can travel to it:
git checkout origin/responsivegui
- Download the latest changes of a branch without affecting the working directory:
- Pull (it is like a fetch and then a merge):
git pull origin responsivegui- A typical workflow when a pull causes conflicts is: resolve the conflicts, and then push the fixed conflicts.
- Short syntax:
git pull(by default it uses origin and the current branch)
- Git remote:
GUIs
- VSCode:
- Git Graph extension
- GitKraken
Other interesting links
Other common scenarios
When you want to ignore a file that was already committed
Source: <https://stackoverflow.com/questions/7527982/applying-gitignore-to-committed-files>
After editing .gitignore to match the ignored files, you can do git ls-files -ci --exclude-standard to see the files that are included in the exclude lists; you can then do
Linux/MacOS:
git ls-files -ci --exclude-standard -z | xargs -0 git rm --cached
Windows (PowerShell):
git ls-files -ci --exclude-standard | % { git rm --cached "$_" }
Windows (cmd.exe):
for /F "tokens=*" %a in ('git ls-files -ci --exclude-standard') do @git rm --cached "%a"
to remove them from the repository (without deleting them from disk).
Workflow with GitHub Issues
Example Workflow:
- Create an issue: You open a new issue titled “Add form validation” and assign it the number #10.
- Work on the task: You create a new branch in your local repository to work on the task: git checkout -b add-form-validation.
- Make commits: As you progress, you make commits with messages referencing the issue: git commit -m “Add basic form validation #10”.
- Complete the task: Once the task is finished, you make a final commit that closes the issue: git commit -m “Complete form validation, closes #10”.
- Merge changes: You merge the branch into the main branch, and issue #10 is automatically closed on GitHub.