Git Common Commands
To work well with GitHub open source projects, you need to understand Git. So here I’m documenting some commands I’ve used in practice, to help myself remember them and hopefully assist some friends as well. Remember the main commands for convenient operation, and just look up the rest when needed. Using practical examples to illustrate some commands I’ve used in actual development.
Getting and Creating Projects
# Create an empty Git repository, or reinitialize an existing repository
git init
# Configuration
git config [--global] user.name "alanhg"
git config [--global] user.email "i@xx.x"
git config --global --add push.default current
Clone
# Clone repository
git clone -b source https://github.com/alanhe421/alfred-workflows
# Only pull specific branch
--single-branch
# Modify local folder mapping after pulling
git clone -b source https://github.com/alanhe421/alfred-workflows heqiang422
Basic Snapshotting
Add
git add .
Commit
git commit -m 'message'
git push
# Modify latest commit
Reset
# Undo staging area commit, revert one version
git reset HEAD^
# Restore to a known state
# git reset --hard
rm
Sometimes after adding certain files to git add for version tracking, you regret it. In this case, rm can solve the problem.
# Stop tracking specified file, but the file remains in the working directory
git rm --cached [file]
# Delete working directory file and put this deletion into the staging area
git rm [file1] [file2] ...
restore
# Restore from local repository, but keep modifications
git restore --staged [file]
Branching and Merging
Branch
# View local branches
git branch
# Delete local branch dev
git branch -d dev
# Create new branch based on previous commit
git branch <branchName> <sha1-of-commit>
# Establish tracking relationship between existing branch and specified remote branch
git branch --set-upstream [branch] [remote-branch]
# Modify corresponding remote branch
git branch -u origin/dev
# Rename local branch
git branch (-m | -M) [<oldbranch>] <newbranch>
Checkout
# Switch local branch
git checkout <branchName>
# Switch branch and modify remote branch to dev
git checkout dev --track origin/dev
Sharing and Updating Projects
pull
# Pull latest code
git pull
push
# Delete remote branch
git push origin --delete <branchName>
# Push to main branch
git push origin <branchName>
remote
# List remote repository information, including URLs
git remote -v
# Add remote repository, supports multiple remote repository addresses
git remote add [shortname] [url]
# Modify URL corresponding to remote repository
git remote set-url origin git@github.com:username/repo.git
Common Issues
Git: fatal: Pathspec is in submodule
Solution:
git rm --cached directory
git add directory
You have not concluded your merge (MERGE_HEAD exists).
git reset --merge
Prompted for username and password every time push
# Check remote repository protocol
git remote -v
git remote rm origin
git remote add origin git@github.com:alanhg/alanhe421.github.io.git
Pull is not possible because you have unmerged files.
This happens because when pulling new code from upstream, it performs a merge. If there are unmerged files, it will show the above message. Either handle the conflict with git add -u, git commit
, or abandon local file modifications and execute git reset --hard FETCH_HEAD
.
Import partial commits from one branch to another
Sometimes we need to import some commits from one branch to another [these commits may not be the latest consecutive commits in that branch]. In this case, we can use cherry pick to solve it. We record the commitID we want to import, checkout to the target branch, and execute ‘git cherry-pick commitID’. Note that there may be conflicts, handle them the same way as MR.
Note that if we want to import N commits, after cherry picking, it will still be N commits.
unable to write sha1 filename Permission denied
Recently, our team used Modelio to edit UML, and when executing git add operation, it reported an error.
The error message indicated permission issues, so the first solution that came to mind was to run the terminal as administrator, or modify file permissions in the .git directory with chmod 777. But neither method worked. Trying to add files individually, I found some worked, so I inferred that some files might be special. Could it be a file editing state issue? So I closed the running modelio that was being edited, and indeed it worked.
I inferred that when software is editing files, some files get locked. The add operation would cause some file modifications, and these files cannot be edited, hence the error.
You have unstaged changes
This error means there are modifications that haven’t been committed or stashed. What needs to be done is to git add, commit, or stash these modifications.
Cannot pull with rebase: You have unstaged changes.↵Please commit or stash them.
When executing git pull --rebase --autostash
, the above error is reported because there are some modifications that need to be committed or stashed, making it impossible to pull code normally.
Git:Merging status
In IDEA, for example, when merging branches [merge sprint with featue/a], conflicts may occur. After handling conflicts, commit fails, and the branch status shows as Git:Merging sprit. At this point, executing git status
in the terminal shows the current status as sprint | merge
. What does this status mean? Actually, it means that after MR conflict resolution, automatic commit failed
. At this point, manual modification and resolution are needed, then re-execute commit. After successful submission, it will return to sprint
. Of course, you can also abandon MR with git merge --abort
.
the RSA host key for ‘github.com’ differs from the key for the IP address
Warning: the RSA host key for ‘github.com’ differs from the key for the IP address ‘198.18.1.89’ Offending key for IP in /Users/xx/.ssh/known_hosts:27 Matching host key in /Users/xx/.ssh/known_hosts:1 Are you sure you want to continue connecting (yes/no)? ^C
When submitting, this message always appears. The solution is to open known_hosts and delete the target line content. Then resubmit.
Important Details
- MR itself is a commit, and the role of commit is to submit local changes to the staging area.
- Commit failure during MR only means commit failed. The MR code merge modifications are already completed. At this point, make modifications locally based on the commit error, then commit again.
- The final success marker of an MR is the successful submission of the post-MR commit, not including push. Push only pushes this MR submission upstream.
- Don’t easily modify commit messages, because Git recognizes this as an MR commit based on the header format of the commit message.
shell request failed on channel 0
vi /etc/security/limits.d/20-nproc.conf
Change 4096
to a larger value, such as 65535
, or set to unlimited
if no limit is desired.
git clone prompts for username and password
When cloning a repository using git protocol, and ssh -T test is normal
but still prompts for username and password, it’s because the communication protocol has been forcibly redirected
.
Solution
vi ~/.gitconfig, comment out url.https://git.xxx.com/.insteadof=git@xxx:
fatal: fetch-pack: invalid index-pack output
The reason is the repository is too large, so directly limit history to only pull the latest commit. –depth implies –single-branch, meaning the following command will only pull one branch.
git clone git@github.com:nodejs/node.git --depth=1
If you later want to pull the complete history, you need to execute
git fetch --unshallow