FAQ - git
Commonly used git operations

How do I undo 'git add' before commit?

git reset

Reset local repository branch to be just like remote repository HEAD

git reset HEAD

How do I checkout a remote Git branch

git fetch j143 <branch-name>

How do I delete a Git branch locally and remotely?

git branch -d -r origin/branch-name

How to change URI for a remote git repository

git remote set-url --add <name> <new-url>

How do I push a new local branch to a remote git repository and track it?

git push --set-upstream origin new-local-branch

How do I revert a git repository to previous commit?

git reset commit-SHA --hard

How can I add empty directory to a git repository?

We cannot add empty directory. But, we need to keep a file .gitkeep or .gitignore or just .keep file with empty contents. For example, touch .keep would create a .keep file.
specifying git add <dir> would add dir/file1 and dir/file2.

Unstage files?

1
> git status
2
On branch master
3
Changes to be committed:
4
(use "git restore --staged <file>..." to unstage)
5
modified: install.md
6
modified: MultiLogReg.dml
7
new file: MLogRegTest.java
8
9
> git restore --staged MultiLogReg.dml MLogregTest.java
10
11
> git status
12
On branch master
13
Changes to be committed:
14
(use "git restore --staged <file>..." to unstage)
15
modified: install.md
16
17
Changes not staged for commit:
18
(use "git add <file>..." to update what will be committed)
19
(use "git restore <file>..." to discard changes in working directory)
20
modified: MultiLogReg.dml
21
22
Untracked files:
23
(use "git add <file>..." to include in what will be committed)
24
MLogregTest.java
25
26
> git stash
27
28
> git commit -m "update only install.md file"
Copied!
Last modified 8mo ago
Copy link