Hello! I'll add command line tips (not only git) to this page as I come up with them. I hope it will be a useful resource for anyone trying to make stuff work while using the command line.
If you want to reach out with ideas, suggestions, or whatever else, please do! There are links on my personal website that you can use to reach me.
I'll be updating this frequently as I learn. Tools that are missing or incompletely covered will be added/updated over time.
When using vim, you can either be in one of several modes. Here, I'll explain "insert" and "normal" modes. (I'll add others as I learn about them.)
Insert mode is similar to the way you’d normally interact with a text editor.
Normal mode means that you can use a variety of commands to navigate the file INCLUDING some commands that actually change the content of the file.
To enter insert mode while viewing a file using vim: type i
(which stands for "insert").
To exit insert mode and re-enter normal mode: type esc
(the actual escape button).
Now, on to some important vi
keystrokes:
:w
: Save.
:q
: Quit.
:x
: Save and quit.
D
: Delete an entire line while in view mode.
A
: Move to the end of line and enter “insert” mode (A stands for Append).
If you want to set your command line to use vi keystrokes (which could drive you nuts, could make your life easier, or could be a way of forcing yourself to learn those keystrokes), you can do so with this command: set -o vi to activate it (and set -o emacs to revert).
Because even if you never use vim voluntarily, it may pop up while you’re using git and the command line.
If vim pops up when you’re viewing a commit message: You can use the default
commit message by typing :w
and then :q
.
If vim pops up when you’re viewing logs: You can get back to the command
line by typing q
(no colon).
If you’re viewing a commit message: You can use the default commit message by typing :x
which means "save and quit."
If you’re viewing logs: You can get back to the command line by typing q
(no colon).
Now, on to git.
With all the details:
git log
Without all that noise:
git log --oneline
How to reorder last two commits in git?
git rebase -i HEAD~2
Next, change the order of the commits in the prompt.
pick f4648aee My first commit
pick 00adf09a My second commit
to
pick 00adf09a My second commit
pick f4648aee My first commit
git push origin HEAD^:name-of-branch
Replace <commit-hash>
with a commit hash that you obtain
by viewing the git log where that commit can be found.
git cherry-pick <commit-hash>
While on a branch, comparing to a branch named main
:
git diff --name-status main
This can be useful if you don't remember what you've done, and
you want to write a good commit message on the first go. "staged
but not committed" means you've already executed git add
but
not git commit
.
git diff --staged
git diff --name-only --diff-filter=U
I like to do this in two steps so I can see what’s happening and change my mind at the last minute if needed.
Move the changes from your last commit from "committed" to "unstaged." This moves the head of your current branch back one commit:
git reset HEAD~1
Then, discard changes in your working directory that are not staged:
git checkout -- .
git switch <name-of-remote-branch>
Note: each repo will have their own contributing guidelines, and you should read the README and/or CONTRIBUTING page in their github repo.
- Go to the repo you want to contribute to.
- Create a fork of that repo.
- Clone the fork to your local machine.
- The way I organize this is:
- A main folder called
git
that includes all my git repos. - A folder within that
git
folder calledforked_repos
which contains any repos that I have forked.
- A main folder called
- The way I organize this is:
- Set the main repo (not your fork) as the upstream remote to your fork, following these instructions. This enables you to pull changes from the main repo to your fork. Most likely, you will want to do this from the main branch of your local repo.
git checkout main
git remote add upstream <https://github.com/some-org/some-repo.git>
git fetch upstream
You can then merge those changes that you pulled from upstream into whatever you're working on locally:
git pull
git checkout your-wip-branch
git merge main
- Make a change in your fork, and then push to GitHub. GitHub will then allow you to open a PR to merge your changes into the main repo.
See this helpful tutorial for more details on git forks and upstreams.
- Stack Overflow: A good answer about reordering commits
- Stack Overflow: Undoing recent local commits
- Cherry-picking commits
- Rewriting history using git (amending commits and messages; reordering commits)
Tool for changing the access permissions and the special mode flags of file system objects.
List a file’s permissions:
ls -l /path/to/file.sh
Give everyone who has access to a file execution permissions:
chmod a+x /path/to/file.sh
a
means "all" users, groups, and other.a
could be substituted forugo
(user, group, others).x
means "execute" permissions (permission to run a file).
Give all users who have access execution permissions. (This is sufficient to allow root
to execute a file.)
chmod u+x /path/to/file.sh